I am (attempting) to use the following code that I got (and altered) from the PHP manual posts at: http://www.php.net/manual/en/function.imagecopyresized.php
<?php
$filename = "/images/lessons/1.JPG";
$maxwidth == "240";
$maxheight == "400";
// Resize an image if it exceeds a predefined size.
if (is_file($filename)) { /* check if the file is really one */
$fd = @fopen($filename,"r");
$image_string = fread($fd,filesize($filename));
fclose($fd);
$im = ImageCreateFromString($image_string);
$currwidth = ImageSX($im);
$currheight = ImageSY($im);
/* now we have to check if the image
is higher than wider and larger
than the defined size, then
calculte it and resize it proportional */
if ($currwidth > $currheight && $currwidth > $maxwidth) {
$percent = ($maxwidth * 100) / $currwidth;
$nwidth = $maxwidth;
$nheight = ($percent * $currheight) / 100;
} elseif ($currwidth < $currheight && $currheight > $maxheight) {
$percent = ($maxheight * 100) / $currheight;
$nheight = $maxheight;
$nwidth = ($percent * $currwidth) / 100;
}
$nwidth = intval($nwidth);
$nheight = intval($nheight);
/* ok. let's create the new smaller image
with calculated size... */
$newim = imagecreate ($nwidth, $nheight);
/* ...and put the lager image into the
smaller one. */
imagecopyresized($newim,$im,0,0,0,0,$nwidth,$nheight,$width,$height);
$add = "x";
$newfilename = "$add$filename";
unlink($filename); /* we need to delete the file,
because we want to replace
the original file with the
resized one */
/* now save to file and clear up the memory */
imagepng($newim,$newfilename);
ImageDestroy($im);
ImageDestroy($newim);
/* if you want, use the returned HTML-Code
and insert it into an <img>-tag */
return "width=\"".$nwidth."\" height=\"".$nheight."\"";
print "Success!!";
} else {
/* if the file wasn't a file return false */
return false;
print "Problem";
}
?>
This should open the image, check and see if it's larger than allowed, resize, and save. Right now, all it does is delete the original file and provide me with the following errors:
Warning: imagecreate(): Invalid image dimensions in /resize.php on line 36
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /resize.php on line 40
Warning: imagepng(): supplied argument is not a valid Image resource in /resize.php on line 51
Warning: imagedestroy(): supplied argument is not a valid Image resource in /resize.php on line 53
Anyone know what the problem is? I have PHP4.3. I downloaded and installed GD today, but my php.ini file does not have a line for GD, only a line for the windows extension for GD. But other than that, the phrase 'gd' doesn't exist anywhere in my php.ini file. Could this be the problem? Does imagecopyresized() require GD?