note 50432 added to function.imagecopyresized
| From: | robby at planetargon dot com | Date: | Mon, 28 Feb 2005 15:56:13 +0000 |
| Subject: | note 50432 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85663@lists.php.net to get a copy of this message | ||
Most of the examples below don't keep the proportions properly. They keep using if/else for the
height/width..and forgetting that you might have a max height AND a max width, not one or the other.
/**
* Resize an image and keep the proportions
* @author Allison Beckwith <allison@planetargon.com>
* @param string $filename
* @param integer $max_width
* @param integer $max_height
* @return image
*/
function resizeImage($filename, $max_width, $max_height)
{
list($orig_width, $orig_height) = getimagesize($filename);
$width = $orig_width;
$height = $orig_height;
# taller
if ($height > $max_height) {
$width = ($max_height / $height) * $width;
$height = $max_height;
}
# wider
if ($width > $max_width) {
$height = ($max_width / $width) * $height;
$width = $max_width;
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0,
$width, $height, $orig_width, $orig_height);
return $image_p;
}
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50432
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50432&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50432&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50432&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50432&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50432&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50432&report=yes
Search -- http://master.php.net/manage/user-notes.php