Resizing multiple images
Posted: Tue Aug 26, 2008 8:08 pm
I dont usually post, as I have always been able to find a solution to any code problems I have online. However I have been working for a very long time at this piece of code that uploads multiple images and resizes them. The script calls imageresizer.php several times during a loop to resize images as thumbnails and "fullsize". The only notices/errors I get are for a few variables that dont get used. The problem is the file that are generated are corrupted, meaning that they wont display. I suspect the script does not finish writing the images. I am not sure if this may be a memory issue, I have tried messing with a few parameters in the php.ini file.
code for the main script:
this is the image resizer:
Thanks alot
code for the main script:
Code: Select all
if ($task==2){
include('../connect.php');
$query="INSERT INTO grc_art VALUES ('','$title','$descrip','$cat')";
mysql_query($query);
$query="SELECT * FROM grc_art ORDER BY id DESC";
$alldump=mysql_query($query);
$lastid=mysql_result($alldump,0,"id");
$timestamp=date('m_d_y');
for ($i=1; $i<=$max_no_img; $i++){
$t=$i-1;
if ($_FILES['images']['tmp_name'][$t]=='')
break 1;
echo "out : $mainimage[$t]";
$imagename = $timestamp . '_' . 'art' . '_' . $lastid . '_' . $i . '.jpg';
copy($_FILES['images']['tmp_name'][$t], $fulldir . $imagename);
$w=550;
$img=$fulldir . $imagename;
include("imageresizer.php");
copy($_FILES['images']['tmp_name'][$t], $thumbdir . $imagename);
$w=100;
$img=$thumbdir . $imagename;
include("imageresizer.php");
$ismain=$mainimage[$t];
$query="INSERT INTO grc_images VALUES ('','$lastid','$imagename','$ismain')";
mysql_query($query);
}
mysql_close();
echo phpinfo();
}Code: Select all
$constrain=0;
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
//echo "$sw";
// image height
$sh = $x[1];
//echo "$sh";
if ($percent > 0) {
// calculate resized height and width if percent is defined
$percent = $percent * 0.01;
$w = $sw * $percent;
$h = $sh * $percent;
} else {
if (isset ($w) AND !isset ($h)) {
// autocompute height if only width is set
$h = (100 / ($sw / $w)) * .01;
$h = @round ($sh * $h);
} elseif (isset ($h) AND !isset ($w)) {
// autocompute width if only height is set
$w = (100 / ($sh / $h)) * .01;
$w = @round ($sw * $w);
} elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
// get the smaller resulting image dimension if both height
// and width are set and $constrain is also set
$hx = (100 / ($sw / $w)) * .01;
$hx = @round ($sh * $hx);
$wx = (100 / ($sh / $h)) * .01;
$wx = @round ($sw * $wx);
if ($hx < $h) {
$h = (100 / ($sw / $w)) * .01;
$h = @round ($sh * $h);
} else {
$w = (100 / ($sh / $h)) * .01;
$w = @round ($sw * $w);
}
}
}
$im = @ImageCreateFromJPEG ($img) or
$im = @ImageCreateFromPNG ($img) or
$im = @ImageCreateFromGIF ($img) or
$im = false;
if (!$im) {
readfile ($img);
echo 'PROBLEM';
} else {
$thumb = @ImageCreateTrueColor ($w, $h);
@ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
@ImageJPEG ($thumb, $img);
}
Thanks alot