Page 1 of 1

Image resize script reaches memory limit

Posted: Sun Jul 20, 2008 6:12 pm
by Trenchant
I had a script working fine to search through an upload folder and sort a bunch of images. Recently I added a function which would resize all the images to be a certain size before copying them. I've always had a counter on the script so it would only work with so many images and then you would have to reload the page. No matter what the script seems to cancel now saying its reached its memory limit. Here's the error:
Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 8192 bytes) in C:\AppServ\www\Projects\todd_berger\admin.php on line 58

Code: Select all

function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile )
{
    $fw = $forcedwidth;
    $fh = $forcedheight;
    $is = getimagesize( $sourcefile );
    if( $is[0] >= $is[1] )
    {
        $orientation = 0;
    }
    else
    {
        $orientation = 1;
        $fw = $forcedheight;
        $fh = $forcedwidth;
    }
    if ( $is[0] > $fw || $is[1] > $fh )
    {
        if( ( $is[0] - $fw ) >= ( $is[1] - $fh ) )
        {
            $iw = $fw;
            $ih = ( $fw / $is[0] ) * $is[1];
        }
        else
        {
            $ih = $fh;
            $iw = ( $ih / $is[1] ) * $is[0];
        }
        $t = 1;
    }
    else
    {
        $iw = $is[0];
        $ih = $is[1];
        $t = 2;
    }
    if ( $t == 1 )
    {
        $img_src = imagecreatefromjpeg( $sourcefile ); // LINE 58
        $img_dst = imagecreatetruecolor( $iw, $ih );
        imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
        if( !imagejpeg( $img_dst, $destfile, 90 ) )
        {
            return false;
        }
    }
    else if ( $t == 2 )
    {
        copy( $sourcefile, $destfile );
        return true;
    }
}
Even if I limit the script to work with 3 images the same error still comes up and it doesn't even finish working with the first image. Any idea's?

Re: Image resize script reaches memory limit

Posted: Sun Jul 20, 2008 6:40 pm
by manixrock
use the php function imagedestroy() to free memory used by it. Check http://www.php.net/imagedestroy for details.

Re: Image resize script reaches memory limit

Posted: Sun Jul 20, 2008 11:52 pm
by omniuni
ini_set("memory_limit","40M");

put that in your php at the very top of the file, that should do the trick.

This is my code to sizer.php which I suspect does something similar to what you are aiming for, if it helps. I think this is taken from some mutilated tutorial somewhere.

Code: Select all

<?php
ini_set("memory_limit","40M");
//Take Input From Filename:
//   $image_loc = Location of Image
//   $image_dim = Image Size (Dimension)
//PHP File to generate a Thumbnail
$image_loc = $_GET['source'];
$strip_image_loc = stripslashes($image_loc);
$image_dim = $_GET['size'];
 
// $srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
 $srcImg = imagecreatefromjpeg("$strip_image_loc");
 $origWidth = imagesx($srcImg);
 $origHeight = imagesy($srcImg);
 
 $newHeight = $origHeight * ($image_dim / $origWidth);
 
 $newImg = imagecreatetruecolor($image_dim, $newHeight);
 imagecopyresized($newImg, $srcImg, 0, 0, 0, 0, $image_dim, $newHeight, $origWidth, $origHeight);
 
header("Content-type: image/jpeg");
ImageJpeg($newImg, null, -1);
ImageDestroy($srcImg);
ImageDestroy($thumbImg);
?>

Re: Image resize script reaches memory limit

Posted: Mon Jul 21, 2008 4:43 am
by padduweb
Here is the code to create Thumb nail image using PHP