note 15036 added to function.getimagesize.php
| From: | altherda at email dot uc dot edu | Date: | Sat, 25 Aug 2001 06:56:14 +0000 |
| Subject: | note 15036 added to function.getimagesize.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-17109@lists.php.net to get a copy of this message | ||
A little function useful for scaling an image to restricted thumbnail dimensions while retaining the
original aspect ratio (within a pixel, given rounding error):
/* resize image dimensions for thumbnails */
function thumbnail_dims($width,$height){
/* max thumbnail dim */
$max_width = 150;
$max_height = 200;
/* scale to max width, if needed */
if($width>$max_width){
$scale = (float)$max_width/(float)$width;
$width = (int) $width*$scale;
$height = (int) $height*$scale;
}
/* scale to max height, if needed */
if($height>$max_height){
$scale = (float)$max_height/(float)$height;
$width = (int) $width*$scale;
$height = (int) $height*$scale;
}
/* ensure min 2px dims */
if($width<2){
$width = 2;
}
if($height<2){
$height = 2;
}
return array($width,$height);
}
<br>called like so:
list($image_width,$image_height) =
GetImageSize("/path/file.jpg");
list($thumb_width,$thumb_height) =
thumbnail_dims($image_width,$image_height);
http://www.php.net/manual/en/function.getimagesize.php