By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,889 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,889 IT Pros & Developers. It's quick & easy.

problem resizing multiple images

P: n/a
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,$proplargeimageh ,$proplargeimagew,'70','0');
echo " done<br>resizing ".$imagedestination." to ".$thumbdestination;
resizejpeg
($imagedestination,$thumbdestination,$propthumbima geh,$propthumbimagew,'70','0');
echo " done<br>";
}

function resizejpeg($image,$newimage,$max_height,$max_width ,$image_quality){
/* $image = "/home/web/images/original.jpg";
$newimage = "/home/web/images/new.jpg";
$image_quality = 80;
$addborder = 1;
$max_height = 200;
$max_width = 300;
*/
// Main code
echo "aa";
$src_img = ImageCreateFromJpeg($image);
$orig_x = ImageSX($src_img);
$orig_y = ImageSY($src_img);
echo "a";
$new_y = $max_height;
$new_x = $orig_x/($orig_y/$max_height);
echo "b";

if ($new_x > $max_width) {
$new_x = $max_width;
$new_y = $orig_y/($orig_x/$max_width);
}
echo "c";

$dst_img = ImageCreateTrueColor($new_x,$new_y);
ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x,
$orig_y);
/*
if ($addborder == 1) {
// Add border
$black = ImageColorAllocate($dst_img, 0, 0, 0);
ImageSetThickness($dst_img, 1);
ImageLine($dst_img, 0, 0, $new_x, 0, $black);
ImageLine($dst_img, 0, 0, 0, $new_y, $black);
ImageLine($dst_img, $new_x-1, 0, $new_x-1, $new_y, $black);
ImageLine($dst_img, 0, $new_y-1, $new_x, $new_y-1, $black);
}
*/
echo "d";
ImageJpeg($dst_img, $newimage, $image_quality);
ImageDestroy($src_img);
ImageDestroy($dst_img);
echo "e";

}

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
NC
Alex Hopson wrote:

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.

The combination of low-level error reporting and running PHP in the
safe mode can produce the same effect. set_time_limit() has no effect
when PHP is running in safe mode. Low-level error reporting can
suppress the error message, so the script will time out without you
knowing it.

Also, your HTTP server may be imposing an execution time limit.
Apache, for example, has a configuration directive called Timeout.
Try running your script from the command line. If it completes,
the problem obviously lies with the HTTP server.

Cheers,
NC

Jul 17 '05 #2

P: n/a
I recently had a similar problem.

I had to resize a large number of images using imagemagick.
While the initial image size was about 140Mb, each resize operation
took at least 6-10 secs.

I partialy solved the problem of timeout by redirecting to the same
script [ Header("Location: ...") ], after every 5 or 6 resizes.

But that approch produces another problem:
The script stopped redirecting after 20 or so times and produced a
"Javascript-alert-type" message box informing that the maximum number
of redirects was reached. I initialy thought that was a web browser
setting but i could not come across a way to change it. (By the way,
the message was produced while using either Firefox or IE)

Depending on the number of images you are resizing each time, my
solution might work for you, thought I have no clue concerning tha
redirect limit problem.
Vasilis

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.