Apache Eating up ram using imagecreatefrom

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Extremest@extremest.com

    #1

    Apache Eating up ram using imagecreatefrom

    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($filen ame,0,2);
    $srcsize = getimagesize($d ir."/".$filename );
    switch($srcsize['mime'])
    {
    case "image/jpeg":
    $src_img = imagecreatefrom jpeg($dir."/".
    $filename);
    break;
    case "image/png":
    $src_img = imagecreatefrom png($dir."/".
    $filename);
    break;
    case "image/gif":
    $src_img = imagecreatefrom gif($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 = imagecreatetrue color($dest_w, $dest_h);
    imagecopyresamp led($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_i mg);
    break;
    case "image/gif":
    header("content-type: image/gif");
    imagegif($dst_i mg);
    break;
    }

    // Destroy images
    imagedestroy($s rc_img);
    imagedestroy($d st_img);
    }

  • =?iso-8859-1?Q?=C1lvaro?= G. Vicario

    #2
    Re: Apache Eating up ram using imagecreatefrom

    *** 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
    --

    Comment

    • Extremest@extremest.com

      #3
      Re: Apache Eating up ram using imagecreatefrom

      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.

      Comment

      Working...