Image Resizing
| From: | Nicholas Mercier | Date: | Thu, 25 Jul 2002 08:09:36 +0000 |
| Subject: | Image Resizing | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-109442@lists.php.net to get a copy of this message | ||
Okay. Unless I failed a math course in high school (which is possible) I think my formulas are sound. However I'm having issues with this code.
The Intended result: Take any image larger then 180x200 (WxH) and resize it proportionally so it fits within 200x180.
What I'm getting, exactly what I want, except images come back deformed or miscreated. I've attached 2 jpegs that are produced by this script. If anyone can help out it would be greatly appreciated
$lastnumQry = "SELECT imageID FROM images ORDER BY imageID DESC";
$lastnumRslt = mysql_query($lastnumQry, $conn);
$lastnum = mysql_fetch_array($lastnumRslt);
$newnum = $lastnum['imageID']+1;
//Set image dir
$image_root = $DOCUMENT_ROOT.'/images/';
//Create a temp image file from uploaded jpeg
$tempimage = imagecreatefromjpeg("$f_image");
$temp_h = ImageSX($tempimage);
$temp_w = ImageSY($tempimage);
//Checks that the image is not too small
if($temp_h < 50 && $temp_w <50)
{
echo "ERROR: Image is less then 50x50.";
exit;
}
//set file name
$filename = $newnum.'.jpg';
//save image
if(!ImageJpeg($tempimage, $image_root.$filename))
{
echo "ERROR: Image unable to upload!";
exit;
}
$smallfilename = $newnum.'_t.jpg';
if($temp_h < 200 && $temp_w <180)
{
//Save thumbnail
if(!ImageJpeg($tempimage, $image_root.$smallfilename))
{
echo "ERROR: Thumbnail Image unable to upload!";
exit;
}
}
else
{
echo "Resize beginning.<BR>";
if(($temp_h*1.8) > ($temp_w*2))
{
$ratio = $temp_h/$temp_w;
$dest_w = 200*$ratio;
$dest_h = 200;
echo "ONE - $dest_w - $dest_h";
}
if(($temp_h*1.8) < ($temp_w*2))
{
$ratio = $temp_w/$temp_h;
echo "Ratio - $ratio <br>";
$dest_w = 180;
$dest_h = 180*$ratio;
echo "TWO - $dest_w - $dest_h";
}
if(($temp_h*1.8) == ($temp_w*2))
{
$dest_w = 180;
$dest_h = 200;
}
$image_t = imagecreatetruecolor($dest_w, $dest_h);
imagecopyresampled($image_t, $tempimage, 0, 0, 0, 0, $dest_w, $dest_h, $temp_w, $temp_h);
if(!ImageJpeg($image_t, $image_root.$smallfilename))
{
echo "ERROR: Thumbnail Image unable to Upload!";
exit;
}
}