Welcome Guest, Not a member yet? Register   Sign In
image resize - memory exhausted
#1

[eluser]velti[/eluser]
hi,
i want to resize images which where uploaded.
I used this function in another website i coded completely by myself and all worked fine with jpg pictures up to 1MB

yesterday i used nearly the same function in a CI controller and files with 200kb+ caused a memory exhaust.

Code:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 10240 bytes) in /Applications/MAMP/htdocs/projekte/hph/system/helpers/image_helper.php on line 80


Quote:function ImageResize($sourcefile, $thumbziel, $new_w, $logo = NULL) {
global $data;
$data['imagememory']['image1'] = memory_get_usage(true);
$sourceinfo = getimagesize($sourcefile);
if($sourceinfo === false) return FALSE;
$data['imagememory']['image2'] = memory_get_usage(true);

if($sourceinfo[2] == 2) $img = imagecreatefromjpeg($sourcefile);
if($sourceinfo[2] == 3) $img = imagecreatefrompng($sourcefile);

$data['imagememory']['image3'] = memory_get_usage(true);
$ow = $sourceinfo[0]; //imagesx($img);
$oh = $sourceinfo[1]; //imagesy($img);

// Set the new width and height
$check_pic = $ow/$oh;
if($check_pic < 1) {
// wenn das bild hochkant ist
$nh = $new_w;
$nw = ceil(($ow/$oh)*$nh);
} else {
$nw = $new_w;
$nh = ceil(($oh/$ow)*$nw);
}
$data['imagememory']['image4'] = memory_get_usage(true);
// Create the new, resized image
$newimg = imagecreatetruecolor($nw,$nh);
imagecopyresampled($newimg,$img,0,0,0,0,$nw,$nh,$ow,$oh);
$data['imagememory']['image5'] = memory_get_usage(true);

// Merge a logo to the picture
if(isset($logo)) {
$logo_img = imageCreateFromGif($logo);
$bwidth = $nw;
$bheight = $nh;
$lwidth = imagesx($logo_img);
$lheight = imagesy($logo_img);
$dst_x = $bwidth - ($lwidth + 10); //zielangaben des logos, hier unten rechts
$dst_y = $bheight - ($lheight + 10);
// ImageAlphaBlending($thumbziel, true);
ImageCopyMerge($newimg,$logo_img,$dst_x,$dst_y,0,0,$lwidth,$lheight, 100);
}

imagejpeg($newimg, $thumbziel);
$data['imagememory']['image6'] = memory_get_usage(true);
imagedestroy($img);
imagedestroy($newimg);
$data['imagememory']['image7'] = memory_get_usage(true);
return $data['imagememory'];
}


what can i do to fix this?

btw.: the same occurs with the image_lib->resize function.
#2

[eluser]Michael Wales[/eluser]
That indicates that your scripts needed more memory than PHP was allowed to give it.

Increase PHP's memory limit, either by adding:
Quote: * memory_limit = 12M to your php.ini file (recommended, if you have access)
* ini_set('memory_limit', '12M');
* php_value memory_limit 12M in your .htaccess file in the root

Some hosts allow a PHP.ini in the root of your site.
#3

[eluser]velti[/eluser]
ini_set('memory_limit... is not callable on every host, right?

but it works, thanks for the help.




Theme © iAndrew 2016 - Forum software by © MyBB