Image resize script reaches memory limit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Image resize script reaches memory limit

Post 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?
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: Image resize script reaches memory limit

Post by manixrock »

use the php function imagedestroy() to free memory used by it. Check http://www.php.net/imagedestroy for details.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Image resize script reaches memory limit

Post 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);
?>
padduweb
Forum Newbie
Posts: 7
Joined: Mon Jul 21, 2008 2:27 am

Re: Image resize script reaches memory limit

Post by padduweb »

Here is the code to create Thumb nail image using PHP
Post Reply