Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1554 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  [PHP] Fatal error: Allowed memory size exhausted - script à optimiser?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[PHP] Fatal error: Allowed memory size exhausted - script à optimiser?

n°2150242
Dawa
www.shootmeagain.com
Posté le 22-07-2012 à 09:06:39  profilanswer
 

Bonjour à tous,
 
J'ai sur mon site web une page où l'utilisateur peut uploader une image et derrière tout ça un script est censé la redimensionner. Mais ce qui coince c'est que si l'image dépasse les 600ko environ, ça plante et je reçois un joli
 
 

Citation :

Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 270001 bytes) in /mnt/sites/shootmeagain.com/web/thumb.php on line 64


 
Et voici le code :  
 

Code :
  1. <?php
  2. function resize($file, $tofname, $maxWidth, $maxHeight, $path, $fonction, $disclaimer=0, $user=0){
  3.  
  4. ini_set("memory_limit","32M" );
  5. //ini_set("max_execution_time","60" );
  6. $prod=$maxWidth*$maxHeight;
  7. //Créé une image à partir de $file
  8. $img = ImageCreateFromJpeg($file);
  9. //Dimensions de l'image
  10. $imgWidth = imagesx($img);
  11. $imgHeight = imagesy($img);
  12. //Facteur largeur/hauteur des dimensions max
  13. $whFact = $maxWidth/$maxHeight;
  14. //Facteur largeur/hauteur de l'original
  15. $imgWhFact = $imgWidth/$imgHeight;
  16. //fixe les dimensions du thumb
  17. if($imgWidth <= $maxWidth && $imgHeight <= $maxHeight)
  18. {
  19. $thumbWidth=$imgWidth;
  20. $thumbHeight=$imgHeight;
  21. }
  22. else
  23. {     
  24. if($imgWidth > $imgHeight)
  25. {
  26.  //Si largeur déterminante
  27.  $thumbWidth  = $maxWidth;
  28.  $thumbHeight = $thumbWidth/$imgWhFact;
  29.  if($thumbHeight>$maxHeight)
  30.  {
  31.   $redimfact=$thumbHeight/$maxHeight;
  32.   $thumbHeight=$maxHeight;
  33.   $thumbWidth=$thumbWidth/$redimfact;
  34.  }
  35. }
  36. else
  37. {
  38.  //Si hauteur déterminante
  39.  $thumbHeight = $maxHeight;
  40.  $thumbWidth = $thumbHeight*$imgWhFact;
  41.  if($thumbWidth>$maxWidth)
  42.  {
  43.   $redimfact=$thumbWidth/$maxWidth;
  44.   $thumbWidth=$maxWidth;
  45.   $thumbHeight=$thumbHeight/$redimfact;
  46.  }
  47. }
  48. }
  49. //Créé le thumb (image réduite)
  50. if($fonction=="photo" )
  51. {
  52. if($disclaimer==1)
  53.  $thumbHeight2=$thumbHeight+35;
  54. else
  55.  $thumbHeight2=$thumbHeight+20;
  56. }
  57. else
  58. {
  59. $thumbHeight2=$thumbHeight;
  60. }
  61. $imgThumb = ImageCreateTruecolor($thumbWidth, $thumbHeight2);
  62. $white = imagecolorallocate($imgThumb,255,255,255);
  63. $black = imagecolorallocate($imgThumb,0,0,0);
  64. imagefill($imgThumb,0,0,$white);
  65. //Insère l'image de base redimensionnée
  66. ImageCopyResampled($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight);
  67. //Nom du fichier thumb
  68. //list($titretof,$ext,$temp) = explode(".",$file);
  69. $imgThumbName = $path.$tofname;
  70. //Créé le fichier thumb
  71. //  $fp = fopen($imgThumbName, "wb" );
  72. //   fclose($fp);
  73. // fflush($fp);
  74. //Renvoie le thumb créé
  75. ImageJpeg($imgThumb, $imgThumbName,96);
  76. imagedestroy($imgThumb);
  77. imagedestroy($img);
  78. return $imgThumbName;
  79. }


 
La ligne 64, indiquée dans le code d'erreur, c'est donc simplement ceci :  
 

Code :
  1. imagefill($imgThumb,0,0,$white);


 
Je ne vois pas comment un simple imagefill() peut être si gourmand ? Ou alors c'est les lignes précédentes qui surchargent le serveur ? :??: Mais rien ne me semble exagéré...
 
Merci de votre aide !!!


---------------
SHOOT ME AGAIN WEBZINE
mood
Publicité
Posté le 22-07-2012 à 09:06:39  profilanswer
 

n°2150276
fabien
Vive la super 5 !
Posté le 22-07-2012 à 20:40:30  profilanswer
 

elle fait quelle résolution ton image? en general les image trop grande pose probleme.
 
Sinon, essaye de modifier:
 
ini_set("memory_limit","32M" );
 
en mettant plus, mais si tu es sur un mutualisé, ca risque de ne pas etre modifiable.


---------------
Découvre le HFRcoin ✈ - smilies

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  [PHP] Fatal error: Allowed memory size exhausted - script à optimiser?

 

Sujets relatifs
[Application PHP] Discussion privée[JavaScript]Besoin d'aide sur ce script
Lancement de tâches cron automatiques depuis PHP[RESOLU] php script affichage fichier
Modifier clé registre par script[PHP]$_SESSION qui disparaissent sous IE
aide script .vbs "copier coller"[PHP] - Aide sur codes PHP pour Club de Plongée ;)
PHP - Caught une erreurTableau PHP & Fichier texte
Plus de sujets relatifs à : [PHP] Fatal error: Allowed memory size exhausted - script à optimiser?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR