PHP Doku:: Sets the image resolution - function.imagick-setresolution.html

Verlauf / Chronik / History: (1) anzeigen

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

Ein Service von Reinhard Neidl - Webprogrammierung.

The Imagick class

<<Imagick::setPointSize

Imagick::setResourceLimit>>

Imagick::setResolution

(PECL imagick 2.0.0)

Imagick::setResolutionSets the image resolution

Beschreibung

bool Imagick::setResolution ( float $x_resolution , float $y_resolution )
Warnung

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

Sets the image resolution.

Parameter-Liste

x_resolution

y_resolution

Rückgabewerte

Liefert TRUE bei Erfolg.


3 BenutzerBeiträge:
- Beiträge aktualisieren...
carter dot sharon at gmail dot com
11.10.2010 20:07
If you are reading or creating a new image and want to set the resolution you need to set the Image Units. Undefined image units will cause imagick to use the default resolution (72x72).

<?php
$img
= new Imagick();
   
$img->setResolution(300,300);
$img->newimage(100,100,'none');   
$img->setImageFormat('png');
// imagick::RESOLUTION_UNDEFINED  imagick::RESOLUTION_PIXELSPERINCH  imagick::RESOLUTION_PIXELSPERCENTIMETER

$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);

print_r($img->identifyImage());
   
?>

Output
Array (
[resolution] => Array ( [x] => 300 [y] => 300 )
[units] => PixelsPerInch
)
sualk at lednew dot de
7.01.2010 15:50
This method uses the "convert -density {$x_resolution}x{$y_resolution}" parameter. However be aware, that Imagick::setResolution() is much more alike the "convert -density" option than Imagick::setImageResolution()

It's very irritating that both Imagick::setResolution() and Imagick::setImageResolution() are introduced with "Sets the image resolution."

Use Imagick::setResolution() prior to reading a raster image. This method does not affect an image. However this method tells the image to which size it has to be sized in relation to images inherent resolution! With this method you are able to affect the real pixel-size of an image after reading. E.g. your image has a size of 100x100 pixels and an inherent resolution of 72. Setting the Resolution to 144 and reading this image results in a new image size of 200x200 pixels.

<?php
$im
= new Imagick();
$im->setResolution(144,144);
$im->readImage("test.eps");
$im->setImageFormat("png");
header("Content-Type: image/png");
echo
$im;
?>

Use Imagick::setImageResolution() to alter the resolution of an already read image. This method does not actually change the size of an image, but has influence to methods, which depends on a given image resolution like Imagick::resampleImage(). E.g. your image has a size of 100x100 pixels and a resolution of 72. Setting ImageResolution to 144 does nothing, unless you are resampling image afterwards in relation to the ImageResolution you set!

<?php
$im
= new Imagick();
$im->readImage("test.eps");
$im->setImageResolution(144,144);
$im->resampleImage  (288,288,imagick::FILTER_UNDEFINED,1);
$im->setImageFormat("png");
header("Content-Type: image/png");
echo
$im;
?>

which actually does the same like

<?php
$im
= new Imagick();
$im->readImage("test.eps");
$im->setImageResolution(72,72);
$im->resampleImage  (144,144,imagick::FILTER_UNDEFINED,1);
$im->setImageFormat("png");
header("Content-Type: image/png");
echo
$im;
?>
znupi69 com
23.08.2008 20:55
If you want to resize a vector-graphics image (such as SVG) to a certain dimension in pixels, without losing quality, you have to do this:

<?php

$im
= new Imagick();
$im->readImage("/path/to/image.svg");
$res = $im->getImageResolution();
$x_ratio = $res['x'] / $im->getImageWidth();
$y_ratio = $res['y'] / $im->getImageHeight();
$im->removeImage();
$im->setResolution($width_in_pixels * $x_ratio, $height_in_pixels * $y_ratio);
$im->readImage("/path/to/image.svg");
// Now you can do anything with the image, such as convert to a raster image and output it to the browser:
$im->setImageFormat("png");
header("Content-Type: image/png");
echo
$im;

?>

It took me a couple or so days to figure this out, I hope this saves someone else's time! Have fun! :-)



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",...)