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_abo rt (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 ($imagestoproce ss)) {
$counter++;
echo $counter."<br>" ;
// sleep(3);
$imagename = $row[33]; //get name of main image
$imagelocation = $salessourceima gedir[$_GET['office']]."/".$imagenam e;
//work out image name and path
//echo "im:-".$imagelocatio n."-";
$imagedestinati on = $salesimagedir. "/".$imagenam e; //work out image name
and path
if (file_exists($i magelocation)) { //see if file exists
$thumbdestinati on = $salesimagedir. "/thumb".$imagena me; //work out image
name and path
echo "<br>resizi ng ".$imagelocatio n." to ".$imagedestina tion;
resizejpeg
($imagelocation ,$imagedestinat ion,$proplargei mageh,$proplarg eimagew,'70','0 ');
echo " done<br>resizin g ".$imagedestina tion." to ".$thumbdestina tion;
resizejpeg
($imagedestinat ion,$thumbdesti nation,$propthu mbimageh,$propt humbimagew,'70' ,'0');
echo " done<br>";
}
function resizejpeg($ima ge,$newimage,$m ax_height,$max_ width,$image_qu ality){
/* $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 = ImageCreateFrom Jpeg($image);
$orig_x = ImageSX($src_im g);
$orig_y = ImageSY($src_im g);
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 = ImageCreateTrue Color($new_x,$n ew_y);
ImageCopyResamp led($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x,
$orig_y);
/*
if ($addborder == 1) {
// Add border
$black = ImageColorAlloc ate($dst_img, 0, 0, 0);
ImageSetThickne ss($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($s rc_img);
ImageDestroy($d st_img);
echo "e";
}
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_abo rt (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 ($imagestoproce ss)) {
$counter++;
echo $counter."<br>" ;
// sleep(3);
$imagename = $row[33]; //get name of main image
$imagelocation = $salessourceima gedir[$_GET['office']]."/".$imagenam e;
//work out image name and path
//echo "im:-".$imagelocatio n."-";
$imagedestinati on = $salesimagedir. "/".$imagenam e; //work out image name
and path
if (file_exists($i magelocation)) { //see if file exists
$thumbdestinati on = $salesimagedir. "/thumb".$imagena me; //work out image
name and path
echo "<br>resizi ng ".$imagelocatio n." to ".$imagedestina tion;
resizejpeg
($imagelocation ,$imagedestinat ion,$proplargei mageh,$proplarg eimagew,'70','0 ');
echo " done<br>resizin g ".$imagedestina tion." to ".$thumbdestina tion;
resizejpeg
($imagedestinat ion,$thumbdesti nation,$propthu mbimageh,$propt humbimagew,'70' ,'0');
echo " done<br>";
}
function resizejpeg($ima ge,$newimage,$m ax_height,$max_ width,$image_qu ality){
/* $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 = ImageCreateFrom Jpeg($image);
$orig_x = ImageSX($src_im g);
$orig_y = ImageSY($src_im g);
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 = ImageCreateTrue Color($new_x,$n ew_y);
ImageCopyResamp led($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x,
$orig_y);
/*
if ($addborder == 1) {
// Add border
$black = ImageColorAlloc ate($dst_img, 0, 0, 0);
ImageSetThickne ss($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($s rc_img);
ImageDestroy($d st_img);
echo "e";
}
Comment