Imagick::scaleImage

(PECL imagick 2.0.0)

Imagick::scaleImageScales the size of an image

说明

Imagick::scaleImage ( int $cols , int $rows [, bool $bestfit = FALSE [, bool $legacy = FALSE ]] ) : bool

Scales the size of an image to the given dimensions. The other parameter will be calculated if 0 is passed as either param.

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

参数

cols

rows

bestfit

返回值

成功时返回 TRUE

错误/异常

错误时抛出 ImagickException。

更新日志

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

范例

Example #1 Imagick::scaleImage()

<?php
function scaleImage($imagePath) {
    
$imagick = new \Imagick(realpath($imagePath));
    
$imagick->scaleImage(150150true);
    
header("Content-Type: image/jpg");
    echo 
$imagick->getImageBlob();
}

?>

User Contributed Notes

agamemnus at flyingsoft dot pw 15-Mar-2014 03:34
Warning: this will blur your edges in possibly unexpected ways. For better control, use resizeImage, instead.
clickconvert at gmail dot com 08-Nov-2012 09:50
Need to resize portrait and landscape images (and convert to 72ppi)? These will fit an area of 800x600 without distorting, no matter how tall or wide.

<?php
    $img
= new Imagick($img_loc.$file);
   
$img->setImageResolution(72,72);
   
$img->resampleImage(72,72,imagick::FILTER_UNDEFINED,1);
   
$img->scaleImage(800,0);
   
$d = $img->getImageGeometry();
   
$h = $d['height'];
    if(
$h > 600) {
   
$img->scaleImage(0,600);
   
$img->writeImage($resized_loc.$file);
    } else {
   
$img->writeImage($resized_loc.$file);
    }
   
$img->destroy();
?>
peter at icb dot at 22-Sep-2009 05:25
If using the fit-parameter this function sometimes seems not to work when one of the two sizes (width or height) is the same size as the image has. For example:

<?php
$image
= new Imagick('800x480.jpg');
$image->scaleImage(640, 480, true);

// $image is still 800x480
?>

You have to calculate the new sizes yourself and use false for $fit in this case.
octave at web dot de 23-Jul-2009 02:57
When using the "fit = true" option, the image will only scale down, but never scale up:

<?php
$im
= new Imagick('1600x1200.jpg');

$im->scaleImage(2000, 1500, true); // => 1600x1200

$im->scaleImage(1000, 500, true); // => 666x500
?>
benford at bluhelix dot com 16-Jun-2009 08:38
If anyone finds "The other parameter will be calculated if 0 is passed as either param. " to be a bit confusing, it means approximately this:

<?php
$im
= new Imagick('example.jpg');
$im->scaleImage(300, 0);
?>

This scales the image such that it is now 300 pixels wide, and automatically calculates the height to keep the image at the same aspect ratio.

<?php
$im
= new Imagick('example.jpg');
$im->scaleImage(0, 300);
?>

Similarly, this example scales the image to make it 300 pixels tall, and the method automatically recalculates the image's height to maintain the aspect ratio.
vincent dot hoen at gmail dot com 02-Aug-2007 06:37
Here is an easy way to resize an animated gif :

$picture = new Imagick('animated_gif.gif');

foreach($picture as $frame){
    $frame->scaleImage($width, $height);
}