ellandrd
asked on
cannot upload images - allowed memory size exhausted
Ive created a upload script, but each time I try uploading images i get this message:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 10304 bytes) in W:\Apache\plan-2\subdomain \htdocs\pr operties\f unctions\u pload-phot o.php on line 56
I my PHP.ini file I have the following set:
memory_limit = 10M
post_max_size = 10M
upload_max_filesize = 10M
However the largest file size of any of the images im trying to upload is 647KB so i'm not sure what is wrong. I'm running Apache 2.2.3 with PHP 5.2.3 on a Windows box.
Here is my upload script:
function fcnUploadPhoto($sourse,$qu ality,$wid th,$direct ory,$filen ame)
{
$params = array();
$params = getimagesize($sourse);
if($params)
{
if($params[0] < $width)
{
copy($sourse,$directory.$f ilename);
}
else
{
$height = round(($width * $params[1]) / $params[0]);
$tmp = imagecreatetruecolor($widt h,$height) ;
switch($params[2])
{
case '1':
$src = imagecreatefromgif($sourse );
$textcolor = imagecolorat($src,0,0);
$tmp = imagecreate($width,$height );
imagepalettecopy($tmp,$src );
imagecopyresized($tmp,$src ,0,0,0,0,$ width,$hei ght,$param s[0],$para ms[1]);
$pixel_over_black = imagecolorat($tmp,0,0);
$backgroundcolor = imagecolorallocate($tmp,25 5,255,255) ;
imagefilledrectangle($tmp, 0,0,$width ,$height,$ background color);
imagecopyresampled($tmp,$s rc,0,0,0,0 ,$width,$h eight,$par ams[0],$pa rams[1]);
$pixel_over_white = imagecolorat($tmp,0,0);
if ($pixel_over_black != $pixel_over_white)
{
imagefilledrectangle($tmp, 0,0,$width ,$height,$ textcolor) ;
imagecopyresampled($tmp,$s rc,0,0,0,0 ,$width,$h eight,$par ams[0],$pa rams[1]);
imagecolortransparent($tmp ,$textcolo r);
}
imagegif($tmp,$directory.$ filename,$ quality);
break ;
case '2' :
$src = imagecreatefromjpeg($sours e);
imagecopyresampled($tmp,$s rc,0,0,0,0 ,$width,$h eight,$par ams[0],$pa rams[1]);
imagejpeg($tmp,$directory. $filename, $quality);
break ;
case '3' :
$src = imagecreatefrompng($sourse );
imagecopyresampled($tmp,$s rc,0,0,0,0 ,$width,$h eight,$par ams[0],$pa rams[1]) ;
imagesavealpha($tmp,true);
imagepng($tmp,$directory.$ filename);
break ;
default :
$src = imagecreatefromjpeg($sours e);
imagecopyresampled($tmp,$s rc,0,0,0,0 ,$width,$h eight,$par ams[0],$pa rams[1]);
imagejpeg($tmp,$directory. $filename, $quality);
break ;
imagedestroy($src);
imagedestroy($tmp);
}
}
}
return true;
}
$photoextensions = array('image/gif','image/j pg','image /jpeg','im age/png',' image/pjpe g');
foreach($_FILES as $file_name => $userfile)
{
if(in_array($userfile['typ e'],$photo extensions ))
{
fcnUploadPhoto($userfile[' tmp_name'] ,70,159,$p hotothumbn ailpath,$u serfile['n ame']);
}
}
ellandrd
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 10304 bytes) in W:\Apache\plan-2\subdomain
I my PHP.ini file I have the following set:
memory_limit = 10M
post_max_size = 10M
upload_max_filesize = 10M
However the largest file size of any of the images im trying to upload is 647KB so i'm not sure what is wrong. I'm running Apache 2.2.3 with PHP 5.2.3 on a Windows box.
Here is my upload script:
function fcnUploadPhoto($sourse,$qu
{
$params = array();
$params = getimagesize($sourse);
if($params)
{
if($params[0] < $width)
{
copy($sourse,$directory.$f
}
else
{
$height = round(($width * $params[1]) / $params[0]);
$tmp = imagecreatetruecolor($widt
switch($params[2])
{
case '1':
$src = imagecreatefromgif($sourse
$textcolor = imagecolorat($src,0,0);
$tmp = imagecreate($width,$height
imagepalettecopy($tmp,$src
imagecopyresized($tmp,$src
$pixel_over_black = imagecolorat($tmp,0,0);
$backgroundcolor = imagecolorallocate($tmp,25
imagefilledrectangle($tmp,
imagecopyresampled($tmp,$s
$pixel_over_white = imagecolorat($tmp,0,0);
if ($pixel_over_black != $pixel_over_white)
{
imagefilledrectangle($tmp,
imagecopyresampled($tmp,$s
imagecolortransparent($tmp
}
imagegif($tmp,$directory.$
break ;
case '2' :
$src = imagecreatefromjpeg($sours
imagecopyresampled($tmp,$s
imagejpeg($tmp,$directory.
break ;
case '3' :
$src = imagecreatefrompng($sourse
imagecopyresampled($tmp,$s
imagesavealpha($tmp,true);
imagepng($tmp,$directory.$
break ;
default :
$src = imagecreatefromjpeg($sours
imagecopyresampled($tmp,$s
imagejpeg($tmp,$directory.
break ;
imagedestroy($src);
imagedestroy($tmp);
}
}
}
return true;
}
$photoextensions = array('image/gif','image/j
foreach($_FILES as $file_name => $userfile)
{
if(in_array($userfile['typ
{
fcnUploadPhoto($userfile['
}
}
ellandrd
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>And please note that different platforms has different memory management (also, software bugs and different software versions can produce the issue too). The same script could work perfectly on FreeBSD with the same php settings.
OK thanks for this great advise. I do remember, the other script runs on a older version of PHP - PHP4 where now im running PHP5...
Im going to check if there is any bugs listed. I will also try bumping the memory up to 32MB on both my localhost and live server.
ellandrd
OK thanks for this great advise. I do remember, the other script runs on a older version of PHP - PHP4 where now im running PHP5...
Im going to check if there is any bugs listed. I will also try bumping the memory up to 32MB on both my localhost and live server.
ellandrd
ASKER
OK i found out that this only happens with certain JPEGS and especially if they come straight from a camera which mine have done.
I tested a image that didn't come from a camera and it was worked fine.
The PHP bugs site also listed this as not a bug, that users should increase memory up to 20-30MB for image processing to avoid this problems...
I also read that memory is freed if you unset used variables. So im going to clear all variables after i don't need them any more via unset($variable).
thanks for the help
ellandrd
I tested a image that didn't come from a camera and it was worked fine.
The PHP bugs site also listed this as not a bug, that users should increase memory up to 20-30MB for image processing to avoid this problems...
I also read that memory is freed if you unset used variables. So im going to clear all variables after i don't need them any more via unset($variable).
thanks for the help
ellandrd
ASKER
memory_limit 10M 10M
post_max_size 10M 10M
upload_max_filesize 10M 10M
so i think apache has picked up on my php.ini settings.
>>Don't forget that JPEG files are compressed, but once you start resampling or doing something with imagejpg they become unpacked and the resources that store image information can very easily be 10-15 times the size of the original JPEG.
OK i didnt know this, but im using the same upload script i build for another site over 12 months ago that also allows users upload images including JPEG files and i didnt have this problem.
ellandrd