note 20028 rejected from function.imagecopyresized by sander
| From: | sander@php.net | Date: | Sat, 06 Apr 2002 19:22:05 +0000 |
| Subject: | note 20028 rejected from function.imagecopyresized by sander | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-28775@lists.php.net to get a copy of this message | ||
Here is a function that will resize and move an uploaded image. The resize will not deform the
image. See the other notes for a similar but for GIF.
function resizeJPEG($imagefile, $newName, $limitWidth, $limitHeight) {
$src_img = imagecreatefromjpeg($imagefile);
$imageInfo = getimagesize($imagefile);
$width = $imageInfo[0];
$height = $imageInfo[1];
$checkSize = $width / $height;
//
//If the image is wider than heigh
//$checkSize > 1
if ($checkSize > 1) {
$newWidth = $limitWidth;
$newHeight = ($limitWidth / $width) * $height;
}
//
//If the image is heigher than wide
//$checkSize < 1
if ($checkSize < 1) {
$newHeight = $limitHeight;
$newWidth = ($limitHeight / $height) * $width;
}
//
//If the image is same width as height
//$checkSize == 1
if ($checkSize == 1) {
$newWidth = $limitWidth;
$newHeight = $limitHeight;
}
$dst_img = imagecreate($newWidth,$newHeight);
imagecopyresized($dst_img,$src_img,0,0,0,0,$newWidth,
(same line) $newHeight, imagesx($src_img),imagesy($src_img));
//
//Output the image
//
imagejpeg($dst_img,"./dynamicImages/$newName");
//
//cleanup
//
ImageDestroy($src_img);
ImageDestroy($dst_img);
}