Imagick
在线手册:中文 英文
PHP手册

Imagick::thumbnailImage

(PECL imagick 2.0.0)

Imagick::thumbnailImageChanges the size of an image

说明

bool Imagick::thumbnailImage ( int $columns , int $rows [, bool $bestfit = false [, bool $fill = false ]] )

Changes the size of an image to the given dimensions and removes any associated profiles. The goal is to produce small low cost thumbnail images suited for display on the Web. If TRUE is given as a third parameter then columns and rows parameters are used as maximums for each side. Both sides will be scaled down until the match or are smaller than the parameter given for the side.

Note: 参数bestfit的特性在 Imagick 3.0.0 中已改变. 在此版本以前, 当目标尺寸为400x400,尺寸为200x150的源图像将不会被改变. 自 Imagick 3.0.0 起, 源图像将会被放大到 400x300 因为这将更好的适合目标尺寸. 当使用参数 bestfit 时,必须同时给出宽度和高度

参数

columns

Image width

rows

Image height

bestfit

Whether to force maximum values

返回值

成功时返回 TRUE .

错误/异常

错误时抛出 ImagickException .


Imagick
在线手册:中文 英文
PHP手册
PHP手册 - N: Changes the size of an image

用户评论:

web at johnbaldock dot com (22-Mar-2012 03:25)

When shrinking a jpg you can get more then double the performance if you use <?php $image->setOption('jpeg:size', '800x532') ?>, exchanging 800x532 to the resolution you want the final image to be. For instance instead of this:

<?php
$image
= new Imagick('foo.jpg');
?>

You would use this:

<?php
$image
= new Imagick();
$image->setOption('jpeg:size', '800x532');
$image->readImage('foo.jpg');
?>

Alex (04-Mar-2012 08:35)

In response to Jarrod:

One thing to note is that the stripImage() function will remove all meta data from the image *including the colour profile*. This might make the resulting image look washed out or just a bit crap.

thumbnailImage() does strip meta data from the image *but leaves the colour profile untouched*. Usually resulting in better looking images, but a very slightly larger file. This might be the reason you were finding the files from your method slightly smaller.

If you want the smallest file size available use Jarrod's method. If you want the image to look the same as the original, use thumbnailImage().

jarrod at jarrodchristman dot com (29-Oct-2011 04:31)

Even though thumbnailImage is meant to produce the smallest file size image possible, i found it didn't. I put together this code and bordering different compression settings, found it produced the smallest file size:

<?php
// Max vert or horiz resolution
$maxsize=550;

// create new Imagick object
$image = new Imagick('input_image_filename_and_location');

// Resizes to whichever is larger, width or height
if($image->getImageHeight() <= $image->getImageWidth())
{
// Resize image using the lanczos resampling algorithm based on width
$image->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
}
else
{
// Resize image using the lanczos resampling algorithm based on height
$image->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
}

// Set to use jpeg compression
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
// Set compression level (1 lowest quality, 100 highest quality)
$image->setImageCompressionQuality(75);
// Strip out unneeded meta data
$image->stripImage();
// Writes resultant image to output directory
$image->writeImage('output_image_filename_and_location');
// Destroys Imagick object, freeing allocated resources in the process
$image->destroy();

?>

I found setCompression to not function at all and had to use setImageCompression. The stripImage call is needed and strips out unneeded meta data. You can choose whatever filter you want, but i found lanczos to be the best for image reduction, though it is more computationally heavy.

Anonymous (02-Jul-2008 12:15)

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);
?>

n-sw-bit at ya dot ru (09-Jun-2008 05:48)

If you want to resize your picture to fit smallest parameter:

$fitbyWidth = (($maxWidth/$w)<($maxHeight/$h)) ?true:false;

if($fitbyWidth){
    $im->thumbnailImage($maxWidth, 0, false);
}else{
    $im->thumbnailImage(0, $maxHeight, false);
}

sgarner at expio dot co dot nz (17-Oct-2007 06:11)

With $fit == true, the image is resized proportionally so that its _smallest_ dimension matches the width or height specified, NOT both.

For example, if you say thumbnailImage(400, 400, true), on an image of 1600x800, it will be resized to 800x400, NOT 400x200 as you might expect.

The solution is to compare the original image's dimensions to the specified dimensions, and substitute zero for the smaller dimension, and set $fit = false.

i.e.: thumbnailImage(400, 0, false) would resize that 1600x800 image to 400x200.

raybdbomb . gmail (11-Sep-2007 12:18)

As noted here
http://php.net/manual/en/ref.imagick.php
With either of the params as 0, the aspect ratio is maintained.