PHP 8.1.0 Beta 1 available for testing


(PECL imagick 2, PECL imagick 3)

Imagick::thumbnailImageGörüntünün boyutlarını değiştirir


public Imagick::thumbnailImage(
    int $sütun,
    int $satır,
    bool $sığdır = false,
    bool $doldur = false,
    bool $eski = false
): bool

Bir görüntünün boyutlarını belirtilen boyutlara indirir ve görüntü ile ilişkili profilleri siler. Yöntemin amacı, bir HTML sayfada gösterilmek üzere görüntünün küçük bir örneğini oluşturmaktır.

Bilginize: sığdır değiştirgesinin davranışı Imagick 3.0.0 sürümünde değişmiştir. Evvelce 200x150 ebadındaki bir görüntü 400x400 ebadındaki bir alana sığdırılmak istendiğinde bu yapılmazdı. Artık görüntü 400x300 ebadına getirilerek belirtilen alana sığdırılmaktadır. sığdır değiştirgesi kullanıldığı takdirde yükseklik ve genişliğin ikisi de verilmelidir.



Görüntünün genişliği.


Görüntünün yüksekliği.


true belirtildiği takdirde, her iki yönde de azami uzunluk olarak satır ve sütun değerleri kullanılır. İki kenarın uzunluğu belirtilen boyuta veya biraz daha altına inene kadar küçültülür.

Dönen Değerler

Başarı durumunda true döner.


Hata durumunda bir ImagickException istisnası oluşur.


Örnek 1 - Imagick::thumbnailImage() örneği

function thumbnailImage($imagePath) {
$imagick = new \Imagick(realpath($imagePath));
$imagick->setbackgroundcolor('rgb(64, 64, 64)');
header("Content-Type: image/jpg");


add a note add a note

User Contributed Notes 7 notes

9 years ago
If the 3rd parameter is true, the image will be resized in such a way that it’ll be contained within the dimensions mentioned.
It’s like changing the dimensions of the image so that it can be contained in a box of given dimension.

Eg: Image’s dimension is 1600*800. If we use thumbnailImage(400, 400, true), the new image’s dimension will be 400*200.

Some of the previous notes in this page mentions that the image is resized proportionally so that its _smallest_ dimension matches the width or height specified, NOT both.
But this is not the case now. Both width & height of the image will be <= that mentioned in thumbnailImage().
Maybe imagick changed/updated the functionality.
jarrod at jarrodchristman dot com
9 years ago
Even though thumbnailImage is meant to produce the smallest file size image possible, i found it didn't. I put together this code and bordering different compression settings, found it produced the smallest file size:

// Max vert or horiz resolution

// create new Imagick object
$image = new Imagick('input_image_filename_and_location');

// Resizes to whichever is larger, width or height
if($image->getImageHeight() <= $image->getImageWidth())
// Resize image using the lanczos resampling algorithm based on width
// Resize image using the lanczos resampling algorithm based on height

// Set to use jpeg compression
// Set compression level (1 lowest quality, 100 highest quality)
// Strip out unneeded meta data
// Writes resultant image to output directory
// Destroys Imagick object, freeing allocated resources in the process


I found setCompression to not function at all and had to use setImageCompression. The stripImage call is needed and strips out unneeded meta data. You can choose whatever filter you want, but i found lanczos to be the best for image reduction, though it is more computationally heavy.
web at johnbaldock dot com
9 years ago
When shrinking a jpg you can get more then double the performance if you use <?php $image->setOption('jpeg:size', '800x532') ?>, exchanging 800x532 to the resolution you want the final image to be. For instance instead of this:

= new Imagick('foo.jpg');

You would use this:

= new Imagick();
$image->setOption('jpeg:size', '800x532');
13 years ago
Here is a function to calculate the new dimensions of a thumbnail, to fit within the given dimensions on both sides.

* Calculate new image dimensions to new constraints
* @param Original X size in pixels
* @param Original Y size in pixels
* @return New X maximum size in pixels
* @return New Y maximum size in pixels
function scaleImage($x,$y,$cx,$cy) {
//Set the default NEW values to be the old, in case it doesn't even need scaling
//If image is generally smaller, don't even bother
if ($x>=$cx || $y>=$cx) {
//Work out ratios
if ($x>0) $rx=$cx/$x;
        if (
$y>0) $ry=$cy/$y;
//Use the lowest ratio, to ensure we don't go over the wanted image size
if ($rx>$ry) {
        } else {
//Calculate the new size based on the chosen ratio
//Return the results
return array($nx,$ny);

Use it like this:

//Read original image and create Imagick object
$thumb=new Imagick($originalImageFilename);

//Work out new dimensions

//Scale the image

//Write the new image to a file
n-sw-bit at ya dot ru
13 years ago
If you want to resize your picture to fit smallest parameter:

$fitbyWidth = (($maxWidth/$w)<($maxHeight/$h)) ?true:false;

    $im->thumbnailImage($maxWidth, 0, false);
    $im->thumbnailImage(0, $maxHeight, false);
raybdbomb . gmail
13 years ago
As noted here
With either of the params as 0, the aspect ratio is maintained.
sgarner at expio dot co dot nz
13 years ago
With $fit == true, the image is resized proportionally so that its _smallest_ dimension matches the width or height specified, NOT both.

For example, if you say thumbnailImage(400, 400, true), on an image of 1600x800, it will be resized to 800x400, NOT 400x200 as you might expect.

The solution is to compare the original image's dimensions to the specified dimensions, and substitute zero for the smaller dimension, and set $fit = false.

i.e.: thumbnailImage(400, 0, false) would resize that 1600x800 image to 400x200.
To Top