Apache Eating up ram using imagecreatefrom

Apache Eating up ram using imagecreatefrom

am 05.04.2008 05:18:32 von Extremest (Extremest

I have a picture site. I save the original and a thumbnail of every
picture. Now if they click on a thumbnail then they go to the
viewer page where they then see an intermediate picture. I have php
opening the original and resizing it and sending the resized image
out. It works great and not much load. Better than having 1.7
million more pics. Problem I am having is that after a few hours
apache is like 700 megs in memory and will start messing up. Is
there a memory leak in this function or something where it cannot
unload the memory used from the pics? Here is my resize image code
that I have so far.

if(isset($_GET['image']))
{
$filename = $_GET['image'];
$dir = "pics/".substr($filename,0,2);
$srcsize = getimagesize($dir."/".$filename);
switch($srcsize['mime'])
{
case "image/jpeg":
$src_img = imagecreatefromjpeg($dir."/".
$filename);
break;
case "image/png":
$src_img = imagecreatefrompng($dir."/".
$filename);
break;
case "image/gif":
$src_img = imagecreatefromgif($dir."/".
$filename);
break;
}
if($srcsize[0] > $srcsize[1])
{
$dest_w = 420;
$dest_h = (420 / $srcsize[0]) * $srcsize[1];
}else{
$dest_w = (420 / $srcsize[1]) * $srcsize[0];
$dest_h = 420;
}
//echo $dest_w."--".$dest_h."--".$srcsize[1]."--".$srcsize
[0];
$dst_img = imagecreatetruecolor($dest_w, $dest_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_w,
$dest_h, $srcsize[0], $srcsize[1]);
switch($srcsize['mime'])
{
case "image/jpeg":
header("content-type: image/jpeg");
imagejpeg($dst_img);
break;
case "image/png":
header("content-type: image/png");
imagepng($dst_img);
break;
case "image/gif":
header("content-type: image/gif");
imagegif($dst_img);
break;
}

// Destroy images
imagedestroy($src_img);
imagedestroy($dst_img);
}

Re: Apache Eating up ram using imagecreatefrom

am 05.04.2008 13:03:35 von webmasterNOSPAMTHANKS

*** Extremest escribió/wrote (Fri, 04 Apr 2008 22:18:32 -0500):
> I have a picture site. I save the original and a thumbnail of every
> picture. Now if they click on a thumbnail then they go to the
> viewer page where they then see an intermediate picture. I have php
> opening the original and resizing it and sending the resized image
> out. It works great and not much load. Better than having 1.7
> million more pics. Problem I am having is that after a few hours
> apache is like 700 megs in memory and will start messing up. Is
> there a memory leak in this function or something where it cannot
> unload the memory used from the pics?

I can't tell you about memory leaks (though it seems there are some), but
resizing images is quite an intensive task and there's no need to do it
*every time* the image is requested. I'd suggest the following algorithm:

Keep two directories: pics for original pictures and thumbs for thumbnails
When a pic is requested, check whether there's a thumbnail already; you can
also use filemetime() to check if its updated
If thumb exist, send it. If not, generate it into disk and send it.

Alternatively, in the future you can simplify this generating the thumbnail
when the image is uploaded (though, given the number of pics, I suppose
they come from a DVD or something like that).



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--

Re: Apache Eating up ram using imagecreatefrom

am 05.04.2008 13:33:48 von Extremest (Extremest

The pics that are being generated are 420x420 with respect to ratio.
I don't have the space to save them. Big thing is that apache or
php one of them should be freeing this memory back up at the end but
it's not. I guess it's php since when I added that page is when I
started having the problems a couple days later. So I guess I'll
create a php bug.