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

Imagick::resizeImage

(PECL imagick 2.0.0)

Imagick::resizeImageScales an image

说明

bool Imagick::resizeImage ( int $columns , int $rows , int $filter , float $blur [, bool $bestfit = false ] )

Scales an image to the desired dimensions with a filter.

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

参数

columns

Width of the image

rows

Height of the image

filter

Refer to the list of filter constants.

blur

The blur factor where > 1 is blurry, < 1 is sharp.

bestfit

Optional fit parameter.

返回值

成功时返回 TRUE .

更新日志

版本 说明
2.1.0 Added optional fit parameter. This method now supports proportional scaling. Pass zero as either parameter for proportional scaling.


Imagick
在线手册:中文 英文
PHP手册
PHP手册 - N: Scales an image

用户评论:

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

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

$image = new Imagick('foo.jpg');

You would use this:

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

Spitfires (04-Mar-2012 05:39)

To avoid upscaling when using the "bestfit" param, check the image dimensions before resizing:

<?php
    $image
= new Imagick( $filename );
   
$imageprops = $image->getImageGeometry();
    if (
$imageprops['width'] <= 200 && $imageprops['height'] <= 200) {
       
// don't upscale
   
} else {
       
$image->resizeImage(200,200, imagick::FILTER_LANCZOS, 0.9, true);
    }
?>

Spitfires (04-Mar-2012 05:27)

The changelog comment

  "2.1.0 Added optional fit parameter. This method now supports proportional scaling. Pass zero as either parameter for proportional scaling."

is poorly structured and therefore IMO misleading.  Yes for proportional scaling you pass 0 as either parameter... however this is *not* true if you use the optional fit param.  When bestfit == true you must specify a *non-zero* value for both columns and rows.  Note it WILL still scale proportionally e.g.

   Imagick::resizeImage ( 200, 200,  imagick::FILTER_LANCZOS, 1, TRUE)

will resize a 1000x750 image to 200x150

So for proportional resizing:
without "bestfit"
   Imagick::resizeImage ( 200, 0,  imagick::FILTER_LANCZOS, 1)

with "bestfit"
   Imagick::resizeImage ( 200, 200,  imagick::FILTER_LANCZOS, 1, TRUE)

Spitfires (03-Mar-2012 07:07)

@jdhawk

Yes it is true that using scaleImage is a lot faster than resizeImage but that's because it is using a very simple "chop" filter.  This will be fine for small thumbnails but for anything larger it may introduce aliasing artifacts which you don't want.

Thus it is not "more efficient" it is 'more simple'. 

If you want quality then you should use one of the slower (but better) filters.

c.f. http://www.imagemagick.org/Usage/resize/#scale

jdhawk at gmail dot com (15-Apr-2011 06:37)

In our linux environment, using resizeImage with any filter produced extremely high CPU Utilization (in the range of 40-50%) while doing batch resizing.

We switched to scaleImage, which produces similar results to FILTER_BOX, and CPU Utilization dropped to 2-3%.

Using XHProf to profile the two batch jobs showed amazing decreases in CPU Time, so if you're doing a lot of picture resizing, it might be beneficial to use scaleImage instead of resizeImage, as it seems to be much much more efficient.

michael dot heca at gmail dot com (16-Nov-2009 01:18)

Use setImageOpacity(1.0) before resizing, for proper handling of transparency in png and gif.

dennis at gofolo dot com (08-Nov-2009 02:32)

Having to do alot of resizing, i needed to know the speeds of the different resize filters.
This was how long it took to resize a 5906x5906 JPEG image to 1181x1181.

FILTER_POINT took: 0.334532976151 seconds
FILTER_BOX took: 0.777871131897 seconds
FILTER_TRIANGLE took: 1.3695909977 seconds
FILTER_HERMITE took: 1.35866093636 seconds
FILTER_HANNING took: 4.88722896576 seconds
FILTER_HAMMING took: 4.88665103912 seconds
FILTER_BLACKMAN took: 4.89026689529 seconds
FILTER_GAUSSIAN took: 1.93553304672 seconds
FILTER_QUADRATIC took: 1.93322920799 seconds
FILTER_CUBIC took: 2.58396601677 seconds
FILTER_CATROM took: 2.58508896828 seconds
FILTER_MITCHELL took: 2.58368492126 seconds
FILTER_LANCZOS took: 3.74232912064 seconds
FILTER_BESSEL took: 4.03305602074 seconds
FILTER_SINC took: 4.90098690987 seconds

I ended up choosing CATROM as it has a very similar result to LANCZOS, but is significantly faster.

billadoid [at ' at '] ['gmail' here] dot com (05-Nov-2008 07:50)

<?php
$height
=$thumb->getImageHeight();
$width=$thumb->getImageWidth();
if (
$height < $width)
 
$thumb->scaleImage(800,0);
 else
 
$thumb->scaleImage(0,600);
?>

Something like this will cause a fatal error when you try to create a thumbnail of an uploaded picture of.. 10x15000 resolution.

It will work nice only if you enable the 'fit':

<?php
$height
=$thumb->getImageHeight();
$width=$thumb->getImageWidth();
if (
$width > 800)
$thumb->scaleImage(800,600,true);
if (
$height > 600)
$thumb->scaleImage(800,600,true);
?>

Note: Maybe I misspelled something or though something wrong. i.e. you could think why would I create a thumbnail of 800x600.

Hope it will helps s/o

andrabr at gmail dot com (25-Aug-2007 12:08)

blur:  > 1 is blurry, < 1 is sharp

To create a nice thumbnail (LANCZOS is the slowest filter):

<?php

$thumb
= new Imagick();
$thumb->readImage('myimage.gif');    $thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->clear();
$thumb->destroy();

?>

Or, a shorter version of the same:

<?php

$thumb
= new Imagick('myimage.gif');

$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');

$thumb->destroy();

?>