On my site I allow users to upload 6 images max size of 5MB. They must be in format either gif,jpg/jpeg. Most images users seem to upload are about 2MB roughly (1006188 bytes) and height:1536 pixels by width:2048 pixels so quite big (especially if trying to resize as well).
I then have two directories (large_img and small_img) on server. In large_img I move the uploaded file into there. I then re-size that image from that large_img directory using GD library to a thumbnail sized (100 height by 100 width) and move that into small_img.
I have been getting an error occasionally
PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 14592 bytes)
maximum memory allowed on my apache server is 64MB . So theres some sort of problem with my code ie taking a few couple chunks of this 64MB and not releasing it for some reason.
Here is my code to take image and resize it. I have provided it for the first image out of the 6 and have not included error checks here as to much to copy and paste:
if(move_uploaded_file($_FILES['uploadedfile_0']['tmp_name'], $move_file_to_directoryname_large))
{
$n_width=100;$n_height=100; /*specify height/width of thumbnail image to be*/
if($_FILES["uploadedfile_0"]["type"]=="image/gif")
{
$im=imagecreatefromgif($move_file_to_directoryname_large);
if(!$im)
{
/*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
}
else
{
$width=imagesx($im); $height=imagesy($im);
$newsmallerimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$move_file_to_directoryname_small=$target_path_small.$newfilename;
if(function_exists("imagegif"))
{
imagegif($newsmallerimage,$move_file_to_directoryname_small);
$images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
}
/*frees image from memory */
imagedestroy($newsmallerimage);
}
}
if($_FILES["uploadedfile_0"]["type"]=="image/jpeg")
{
$im=imagecreatefromjpeg($move_file_to_directoryname_large); /*create from image stored in directory specified when moving from /tmp*/
if(!$im)
{
/*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
}
else
{
$width=imagesx($im);/*Original picture width is stored*/$height=imagesy($im);/*Original picture height is stored*/
$newsmallerimage=imagecreatetruecolor($n_width,$n_height);
$imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$move_file_to_directoryname_small=$target_path_small.$newfilename;
if(function_exists("imagejpeg"))
{
imagejpeg($newsmallerimage,$move_file_to_directoryname_small);
开发者_如何学Go $images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
}
/*frees image from memory */
imagedestroy($newsmallerimage);
}
}
}
Heres my php.ini settings:
upload_max_filesize = 30M
post_max_size = 30M
max_execution_time = 120
max_file_uploads = 6
memory_limit=128M
Something in script is eating the memory up. The error occurred mentioned above occurs at this part of code $im=imagecreatefromjpeg($move_file_to_directoryname_large);
if its jpeg photo or imagecreatefromgif() if its gif photo format
I am releasing memory by destroying image $imagedestroy($newsmallerimage);
Note this same code if() statement is repeated for other 5 images just named ['uploadedfile_1'] ...2 etc
Any suggestions perhaps read the files into memory using file_get_contents or the GD library. I know that GD holds the entire image uncompressed in memory which could be the problem. Thanks guys
You are not freeing $im
, maybe that's the problem.
Destroy the image you are creating via imagecreatefromjpeg()
in if()
condition add line,
imagedestroy($im);
like this,
if(!$im)
{
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
imagedestroy($im);
}
精彩评论