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.
Imagick::scaleImage
(PECL imagick 2.0.0)
Imagick::scaleImage — 画像のサイズを変更する
説明
bool Imagick::scaleImage
( int
$cols
, int $rows
[, bool $bestfit = false
] )画像のサイズを指定した大きさに変更します。 パラメータに 0 を指定すると、そのパラメータを自動的に計算します。
注意: パラメータ
bestfitの挙動は Imagick 3.0.0 で変わりました。 これより前のバージョンでは、200x150 の画像に対して 400x400 を指定した場合は何もせずそのままになっていました。 Imagick 3.0.0 以降では、この画像は 400x300 に拡大されます。これが、 指定したサイズに対して「ベストフィット」する大きさだからです。bestfitを使う場合は、幅と高さの両方を指定しなければなりません。
パラメータ
-
cols -
-
rows -
-
bestfit -
返り値
成功した場合に TRUE を返します。
エラー / 例外
エラー時に ImagickException をスローします。
変更履歴
| バージョン | 説明 |
|---|---|
| 2.1.0 | オプションのパラメータ fit が追加され、 このメソッドは、比例形式の拡大・縮小をサポートするようになりました。 どちらかのパラメータにゼロを渡すと比例形式の拡大・縮小を行います。 |
peter at icb dot at
22-Sep-2009 05:25
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);
}
