Hello everyone I am trying to resize an image of 700kb with imagecreatefromjpeg.
This gives the error: Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 9356 bytes)
First the memory_limit was 8M then we set this to 24 M but then it still gives the error. Is this a memory leak? What could have happened? We restarted apache already.
Here is some code for those who are interested:
function getResizedBackgroundImageURL($afb="",$params="") {
#1. Verwijder webroot
$filename=C_Settings::getFileRoot().str_replace(C_Settings::getWebRoot(),"",$afb);
$i=strrpos($filename,".");
if ($i!==FALSE) {
$ext=substr($filename,$i);
$basefilename=substr($filename,0,$i);
#Parse width/height params
$a=explode("&",$params);
foreach($a as $attr) {
$b=explode("=",$attr);
if ($b[0]=="w") {
$w=$b[1];
} elseif ($b[0]=="h") {
$h=$b[1];
}
}
if (!is_numeric($w)) {
die("Missing param w for getResizedBackgroundImageURL");
}
if (!is_numeric($h)) {
die("Missing param h for getResizedBackgroundImageURL");
}
#Compose new filename
$newFilename=$basefilename."_w".$w."_h".$h.$ext;
#See if the resized image exists
if (!file_exists($newFilename)) {
if (is_file($filename)) {
if($ext==".jpg" || $ext==".jpeg"){
$image_org=imagecreatefromjpeg($filename);
}
if($ext==".gif") {
$image_org=@imagecreatefromgif($filename);
}
if ($image_org) {
list($wp,$hp,$type,$attr)=getimagesize($filename);
$hfactor=$hp/$h;
$wfactor=$wp/$w;
if ($hfactor > $wfactor) {
$factor=$hfactor;
} else {
$factor=$wfactor;
}
if($wp > $w || $hp > $h) {
$image_w=$wp/$factor;
$image_h=$hp/$factor;
}else{
#if image fits the given boundaries, do not resize
$image_w=$wp;
$image_h=$hp;
}
//Note: PHP with GD2.0 required for imagecreatetruecolor
$img_copy=imagecreatetruecolor($image_w, $image_h);
imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $wp, $hp);
if (@imagejpeg($img_copy, $newFilename, 80)) {
chmod($newFilename,0777);
} else {
echo("<b>Error: </b>Unable to create image $newFilename. Check directory permissions.");
}
imagedestroy($image_org);
imagedestroy($img_copy);
}
}
}
$newURL=C_Settings::getWebRoot().str_replace(C_Settings::getFileRoot(),"",$newFilename);
return $newURL;
}
}