PHP Doku:: Changes the size of an image - function.imagick-thumbnailimage.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzBildverarbeitung und -generierungImage Processing (ImageMagick)The Imagick classImagick::thumbnailImage

Ein Service von Reinhard Neidl - Webprogrammierung.

The Imagick class

<<Imagick::thresholdImage

Imagick::tintImage>>

Imagick::thumbnailImage

(PECL imagick 2.0.0)

Imagick::thumbnailImageChanges the size of an image

Beschreibung

bool Imagick::thumbnailImage ( int $columns , int $rows [, bool $bestfit = false ] )
Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.

Changes the size of an image to the given dimensions and removes any associated profiles. The goal is to produce small low cost thumbnail images suited for display on the Web. If TRUE is given as a third parameter then columns and rows parameters are used as maximums for each side. Both sides will be scaled down until the match or are smaller than the parameter given for the side.

Hinweis: Das Verhalten des Parameters bestfit hat sich mit Imagick 3.0.0 geändert. Vor dieser Version wurde ein Bild der Größe 200x150 bei der angegebenen Größe von 400x400 nicht verändert. In Imagick 3.0.0 und späteren Versionen wird dieses Bild auf die Größe 400x300 hochgerechnet, da dies die passendste größe für die angegebene Bildgröße ist. Wenn der bestfit-Parameter angegeben wird, so muss sowohl eine Breite als auch eine Höhe angegeben werden.

Parameter-Liste

columns

Image width

rows

Image height

bestfit

Whether to force maximum values

Rückgabewerte

Liefert TRUE bei Erfolg.

Fehler/Exceptions

Wirft ImagickException bei Fehlern.


4 BenutzerBeiträge:
- Beiträge aktualisieren...
Anonymous
2.07.2008 13:15
Here is a function to calculate the new dimensions of a thumbnail, to fit within the given dimensions on both sides.

<?php
/**
 * 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
   
list($nx,$ny)=array($x,$y);
   
   
//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) {
           
$r=$ry;
        } else {
           
$r=$rx;
        }
       
       
//Calculate the new size based on the chosen ratio
       
$nx=intval($x*$r);
       
$ny=intval($y*$r);
    }   
   
   
//Return the results
   
return array($nx,$ny);
}
?>

Use it like this:

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

//Work out new dimensions
list($newX,$newY)=scaleImage(
       
$thumb->getImageWidth(),
       
$thumb->getImageHeight(),
       
$newMaximumWidth,
       
$newMaximumHeight);

//Scale the image
$thumb->thumbnailImage($newX,$newY);

//Write the new image to a file
$thumb->writeImage($thumbnailFilename);
?>
n-sw-bit at ya dot ru
9.06.2008 18:48
If you want to resize your picture to fit smallest parameter:

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

if($fitbyWidth){
    $im->thumbnailImage($maxWidth, 0, false);
}else{
    $im->thumbnailImage(0, $maxHeight, false);
}
sgarner at expio dot co dot nz
17.10.2007 7:11
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.
raybdbomb . gmail
11.09.2007 1:18
As noted here
http://php.net/manual/en/ref.imagick.php
With either of the params as 0, the aspect ratio is maintained.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)