note 84178 added to function.imagick-thumbnailimage
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 02 Jul 2008 11:15:03 +0000 |
| Subject: | note 84178 added to function.imagick-thumbnailimage | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141063@lists.php.net to get a copy of this message | ||
Here is a function to calculate the new dimensions of a thumbnail, to fit within the given
dimensions on both sides.
<?php
/**
* Calculate new image dimensions to new constraints
*
* @param Original X size in pixels
* @param Original Y size in pixels
* @return New X maximum size in pixels
* @return New Y maximum size in pixels
*/
function scaleImage($x,$y,$cx,$cy) {
//Set the default NEW values to be the old, in case it doesn't even need scaling
list($nx,$ny)=array($x,$y);
//If image is generally smaller, don't even bother
if ($x>=$cx || $y>=$cx) {
//Work out ratios
if ($x>0) $rx=$cx/$x;
if ($y>0) $ry=$cy/$y;
//Use the lowest ratio, to ensure we don't go over the wanted image size
if ($rx>$ry) {
$r=$ry;
} else {
$r=$rx;
}
//Calculate the new size based on the chosen ratio
$nx=intval($x*$r);
$ny=intval($y*$r);
}
//Return the results
return array($nx,$ny);
}
?>
Use it like this:
<?php
//Read original image and create Imagick object
$thumb=new Imagick($originalImageFilename);
//Work out new dimensions
list($newX,$newY)=scaleImage(
$thumb->getImageWidth(),
$thumb->getImageHeight(),
$newMaximumWidth,
$newMaximumHeight);
//Scale the image
$thumb->thumbnailImage($newX,$newY);
//Write the new image to a file
$thumb->writeImage($thumbnailFilename);
?>
----
Server IP: 92.48.100.16
Probable Submitter: 217.64.123.94
----
Manual Page -- http://www.php.net/manual/en/function.imagick-thumbnailimage.php
Edit -- https://master.php.net/note/edit/84178
Del: integrated -- https://master.php.net/note/delete/84178/integrated
Del: useless -- https://master.php.net/note/delete/84178/useless
Del: bad code -- https://master.php.net/note/delete/84178/bad+code
Del: spam -- https://master.php.net/note/delete/84178/spam
Del: non-english -- https://master.php.net/note/delete/84178/non-english
Del: in docs -- https://master.php.net/note/delete/84178/in+docs
Del: other reasons-- https://master.php.net/note/delete/84178
Reject -- https://master.php.net/note/reject/84178
Search -- https://master.php.net/manage/user-notes.php