For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2005 > script stops when processing images









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author script stops when processing images
Alex Hopson

2005-02-17, 4:00 pm

I'm using the code below to loop through some images and resize each image
twice, once to create a thumbnail and once to create a small image.

The page stops loading around the 38th image out of 40+. Not always the same
place but it's either 38th or 39th image, which I find strange as it's not
consistent.

At first I thought it was a timeout problem so I added - set_time_limit(0);
and ignore_user_abort (true); to prevent it timing out, there was no
'connection timeout' error or similar, there have been no printed errors.

I've tried running the same loop but commenting out one of the resize
functions, when I do this the page completes and all the images are resized
correctly, then I comment out the other resize function and again that
worked, so I don't believe that there is a problem with the images.

Which leaves a memory problem. In the function that resizes the image
(included below) I have used ImageDestroy to get rid of the two images from
memory.

Can anyone see what is going wrong or give me any pointers? Thanks
Alex


while ($row = mysql_fetch_row($imagestoprocess)) {
$counter++;
echo $counter."<br>";
// sleep(3);
$imagename = $row[33]; //get name of main image
$imagelocation = $salessourceimagedir[$_GET['office']]."/".$imagename;
//work out image name and path
//echo "im:-".$imagelocation."-";
$imagedestination = $salesimagedir."/".$imagename; //work out image name
and path
if (file_exists($imagelocation)) { //see if file exists
$thumbdestination = $salesimagedir."/thumb".$imagename; //work out image
name and path
echo "<br>resizing ".$imagelocation." to ".$imagedestination;
resizejpeg
($imagelocation,$imagedestination,$propl
argeimageh,$proplargeimagew,'70','0');
echo " done<br>resizing ".$imagedestination." to ".$thumbdestination;
resizejpeg
($imagedestination,$thumbdestination,$pr
opthumbimageh,$propthumbimagew,'70','0')
;
echo " done<br>";
}
}


function resizejpeg( $origfile,$newfile,$endh,$endw,$imagequa
l,$crop) {

$return_val = 1;
if (!$return_val = ( ($img = ImageCreateFromJPEG ( $origfile )) &&
$return_val == 1 ) ? "1" : "0" ) {
quitonerror ("Your picture is corrupt, please try resaving it or uploading
another picture.");
}

$origw = imagesx ($img); // Original image width
$origh = imagesy ($img); // Original image height

$ratiow = $origw / $endw; //get ratios of current dimension against min
dimension
$ratioh = $origh / $endh;

if ($ratiow == $ratioh) { //if image is already correctly proportioned
$neww = round($origw / $ratiow);
$newh = round($origh / $ratioh);
$offsetw = '0';
$offseth = '0';
}
elseif ($ratioh < $ratiow) { // if image is wide

if ($crop == '1')
{
$neww = round($origw / $ratioh); // this will be too wide
$newh = round($origh / $ratioh); // this will be perfect
}
else {
$neww = round($origw / $ratiow); // this will be perfect
$newh = round($origh / $ratiow); // this will be to short
}

$offseth = '0'; // as height perfect
$offsetw = round(($neww - $endw) / 2); // horizontally centred
}
else { // if image is tall
if ($crop == '1')
{
$neww = round($origw / $ratiow); // this will be perfect
$newh = round($origh / $ratiow); // this will be too tall
}
else
{
$neww = round($origw / $ratioh); // this will be too thin
$newh = round($origh / $ratioh); // this will be perfect

}
$offsetw = '0'; // as width perfect
$offseth = round(($newh - $endh) / 2); // vertically centred
}

if($crop != '1') // If original is smaller then don't resize at all, then
quality will be better
{ // ...unless of course we are cropping for a thumbnail, then we
want to be resized up ...
if (($origw < $neww) && ($origh < $newh))
{
$neww = $origw;
$newh = $origh;
}
}

$resized_id = ImageCreateTrueColor( $neww , $newh ); // create an image to
resize the image proportionally

ImageCopyResampled( $resized_id, $img, // resize image - no cropping, so
may be too big in one dimension
0,0, // dst x,y
0,0, // src LR,UD
$neww, $newh,
$origw, $origh );

if ($crop == '1')
{
$resized_cropped_id = ImageCreateTrueColor( $endw , $endh ); // create an
image to crop the oversized dimension

ImageCopyResampled( $resized_cropped_id, $resized_id, // crop image - so
right size
0,0, // dst x,y
$offsetw,$offseth, // src LR,UD
$endw, $endh,
$endw, $endh);

$return_val = ( $full = ImageJPEG( $resized_cropped_id,$newfile,
$imagequal ) // save jpeg to destination
&& $return_val == 1 ) ? "1" : "0";
ImageDestroy( $resized_cropped_id );
}
else
{
$return_val = ( $full = ImageJPEG( $resized_id, $newfile,$imagequal ) //
save jpeg to destination
&& $return_val == 1 ) ? "1" : "0";

}

ImageDestroy( $resized_id ); // wipe memory for temp images
ImageDestroy( $img ); // wipe memory for temp images

return ($return_val) ? TRUE : FALSE ;
}




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com