PHP 5.5.16 is released

Imagick::resizeImage

(PECL imagick 2.0.0)

Imagick::resizeImageRedimensionne une image

Description

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

Redimensionne une image aux dimensions désirées, avec un filtre.

Note: Le comportement du paramètre bestfit a changé avec Imagick 3.0.0. Avant cette version, fournir les dimensions 400x400 à une image de dimensions 200x150 faisait que la partie gauche était inchangée. Avec Imagick 3.0.0 et suivants, l'image est réduite à la taille 400x300, sachant que c'est le meilleur résultat pour ces dimensions. Si le paramètre bestfit est utilisé, la largeur et la hauteur doivent être fournies.

Liste de paramètres

columns

La largeur de l'image cible.

rows

La hauteur de l'image cible.

filter

Une constante de filtre.

blur

Le facteur de flou, où > 1 est flou, et < 1 est net.

bestfit

Paramètre optionnel de fit.

Valeurs de retour

Returns TRUE on success.

Historique

Version Description
2.1.0 Ajout d'un paramètre optionnel de fit. Cette méthode supporte maintenant la mise à l'échelle proportionnelle. Passez zéro comme valeur de paramètre pour une mise à l'échelle proportionnelle.

add a note add a note

User Contributed Notes 6 notes

up
22
dennis at gofolo dot com
4 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
5
Spitfires
2 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
7
andrabr at gmail dot com
6 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
4
jdhawk at gmail dot com
3 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.
up
-5
Spitfires
2 years ago
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);
    }
?>
up
-14
michael dot heca at gmail dot com
4 years ago
Use setImageOpacity(1.0) before resizing, for proper handling of transparency in png and gif.
To Top