note 15036 deleted from function.getimagesize by jimw
| From: | jimw@php.net | Date: | Mon, 10 Dec 2001 23:32:56 +0000 |
| Subject: | note 15036 deleted from function.getimagesize by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22476@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);