Imagick::resizeImage

(PECL imagick 2, PECL imagick 3)

Imagick::resizeImage β€” ΠœΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅

ОписаниС

public function Imagick::resizeImage(
Β Β Β Β int $columns,
Β Β Β Β int $rows,
Β Β Β Β int $filter,
Β Β Β Β float $blur,
Β Β Β Β bool $bestfit = false,
Β Β Β Β bool $legacy = false
): bool

ΠœΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π΄ΠΎ ΠΆΠ΅Π»Π°Π΅ΠΌΡ‹Ρ… Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠ² с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ filter.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: ПовСдСниС ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° bestfit ΠΈΠ·ΠΌΠ΅Π½ΠΈΠ»ΠΈ Π² Imagick 3.0.0. Π”ΠΎ этой вСрсии ΠΏΡ€ΠΈ Ρ€Π°Π·ΠΌΠ΅Ρ€Π°Ρ… 400 x 300 Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ 200 x 150 Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΎΡΡ‚Π°Π²Π°Π»ΠΎΡΡŒ Π½Π΅Ρ‚Ρ€ΠΎΠ½ΡƒΡ‚Ρ‹ΠΌ. Π’ Imagick 3.0.0 ΠΈ Π±ΠΎΠ»Π΅Π΅ ΠΏΠΎΠ·Π΄Π½ΠΈΡ… вСрсиях ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒΡΡ Π΄ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° 400 x 300 Ρ‚ΠΎΡ‡Π΅ΠΊ, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ Ρ‚Π°ΠΊΠΎΠΉ Ρ€Π°Π·ΠΌΠ΅Ρ€ «большС ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹Ρ… соотвСтствуСт» Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€Π°ΠΌ. ΠŸΠΎΡ‚Ρ€Π΅Π±ΡƒΠ΅Ρ‚ΡΡ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ ΠΊΠ°ΠΊ ΡˆΠΈΡ€ΠΈΠ½Ρƒ, Ρ‚Π°ΠΊ ΠΈ высоту, Ссли Π·Π°Π΄Π°Π»ΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ bestfit.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

columns

Π¨ΠΈΡ€ΠΈΠ½Π° изобраТСния.

rows

Высота изобраТСния.

filter

ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ΡΡŒ ΠΊ списку констант FILTER.

blur

ΠšΠΎΡΡ„Ρ„ΠΈΡ†ΠΈΠ΅Π½Ρ‚ размытия, Π³Π΄Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ > 1 Π΄Π΅Π»Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π±ΠΎΠ»Π΅Π΅ Ρ€Π°Π·ΠΌΡ‹Ρ‚Ρ‹ΠΌ, Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ < 1 - Π±ΠΎΠ»Π΅Π΅ Ρ€Π΅Π·ΠΊΠΈΠΌ.

bestfit

ΠΠ΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΠΎΠ΄Π³ΠΎΠ½ΠΊΠΈ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

ΠŸΡ€ΠΈ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎΠΌ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ функция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true.

Бписок измСнСний

ВСрсия ОписаниС
PECL imagick 2.1.0 Π”ΠΎΠ±Π°Π²Π»Π΅Π½ Π½Π΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΠΎΠ΄Π³ΠΎΠ½ΠΊΠΈ. Π’Π΅ΠΏΠ΅Ρ€ΡŒ ΠΌΠ΅Ρ‚ΠΎΠ΄ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠΏΠΎΡ€Ρ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½ΠΎΠ΅ ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅. Для ΠΏΡ€ΠΎΠΏΠΎΡ€Ρ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½ΠΎΠ³ΠΎ ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΡ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ ноль Π² качСствС любого ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π°.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Imagick::resizeImage()

<?php
function resizeImage($imagePath, $width, $height, $filterType, $blur, $bestFit, $cropZoom) {
    //ΠšΠΎΡΡ„Ρ„ΠΈΡ†ΠΈΠ΅Π½Ρ‚ размытия, Π³Π΄Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ >  1 Π΄Π΅Π»Π°Π΅Ρ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π±ΠΎΠ»Π΅Π΅ Ρ€Π°Π·ΠΌΡ‹Ρ‚Ρ‹ΠΌ, Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ < 1 - Π±ΠΎΠ»Π΅Π΅ Ρ€Π΅Π·ΠΊΠΈΠΌ.
    $imagick = new \Imagick(realpath($imagePath));

    $imagick->resizeImage($width, $height, $filterType, $blur, $bestFit);

    $cropWidth = $imagick->getImageWidth();
    $cropHeight = $imagick->getImageHeight();

    if ($cropZoom) {
        $newWidth = $cropWidth / 2;
        $newHeight = $cropHeight / 2;

        $imagick->cropimage(
            $newWidth,
            $newHeight,
            ($cropWidth - $newWidth) / 2,
            ($cropHeight - $newHeight) / 2
        );

        $imagick->scaleimage(
            $imagick->getImageWidth() * 4,
            $imagick->getImageHeight() * 4
        );
    }


    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
}

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
103
dennis at gofolo dot com ΒΆ
16 years ago
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.
up
37
andrabr at gmail dot com ΒΆ
18 years ago
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(); 

?>
up
8
phper at krzysiu dot net ΒΆ
10 years ago
Some size, image and filter and blur combinations causes artifacts or even make image completely scrambled. As far, as I see, it happens with blur values smaller than 0.25 (sometimes less) and goes worse to the point of 0 - black image. Sometimes only some values gives artifacts, like Hanning with my test image: 0.0 blur is fine, 0.1 produces artefacts.

Affected filters are e.g. Catrom, sinc, cubic, quadratic, while unaffected are e.g. Lanczos and Hanning. The problem seems to be the domain of the filter algorithms, not IMagick implementation. This image shows three filters: http://i.imgur.com/HcdwoUS.jpg

Sometimes test image could look fine, but other not, so if you are using affected filters, the 0.5 value should be safe.

This test script takes example image (you can download and use http://i.imgur.com/KsTJpFr.jpg which is affected) and creates resized images in the same directory for every filter and one of four blur values, with naming like "test.jpg.sinc.0.1.jpg" for "test.jpg" input.

<?php
    $imgPath = 'imgtest/test.jpg'; // set your image file
    $testBlurs = [0, 0.1, 0.2, 0.5]; // test these blur values
    $im = new IMagick();
    $im->readImage($imgPath);
    
    foreach ((new ReflectionClass('IMagick'))->getConstants() as $n => $f) {
        if (strncmp($n, 'FILTER_', 7) === 0) { // get available IMagick filters
            $filterName = strtolower(substr($n, 7)); // extract filter name from constant
            foreach ($testBlurs as $blur) {
                $imSize = clone $im;
                $imSize->resizeImage(500, 500, $f, $blur, true);
                $imSize->writeImage(sprintf("%s-%s-%.1f.jpg", $imgPath, $filterName, $blur));
                $imSize->destroy();
            }
        }
    }
    
?>
up
23
Spitfires ΒΆ
14 years ago
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)
up
16
jdhawk at gmail dot com ΒΆ
15 years ago
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.