Image resize script reaches memory limit
Posted: Sun Jul 20, 2008 6:12 pm
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:
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?
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;
}
}