略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Imagick::setImageFormat

2024-11-14

Imagick::setImageFormat

(PECL imagick 2, PECL imagick 3)

Imagick::setImageFormat为图片设置指定的格式

说明

Imagick::setImageFormat(string $format): bool

为图片设置指定的格式

参数

format

表示图片格式的一个字符串。支持的格式依赖于 ImageMagick 的安装。

返回值

成功时返回 true

add a noteadd a note

User Contributed Notes 7 notes

up
5
optymizer at yahoo dot com
14 years ago
The previous example did not work for me. I received an error that the IMagick object cannot be converted to string.

This example uses the IMagick::getImageBlob() method to properly output the contents of the converted image:

function getImage($filename)
{
            $image->readImage($filename);
            $image->setImageFormat("png");
            header("Content-type: image/png");
            echo $image->getImageBlob();
}

Hope this helps!
up
2
markus dot s dot schmitz at gmail dot com
9 years ago
If you use writeFile or writeFiles without setting the format, it is automatically set according to the file extension used within the parameter of writeFile.
up
3
nathan at crause dot name
11 years ago
For those of you completely confused by the utter lack of documentation for this class (including notes being dropped, probably this one included), the differences between any "setXXX" and "setImageXXX" seems to be entirely dependently on HOW the image was loaded.

If the image was loaded at object constructor time, it seems that you use "setXXX" - if you loaded it after the fact (such as via "readImageBlob") then you use "setImageXXX".

This is irregular, however. For example:

<?php
        $image
= new Imagick();

       
$image->setResolution(300, 300);
       
$image->readImageBlob(...);
       
// convert the output to JPEG
       
$image->setImageFormat('jpeg');
       
$image->setImageCompressionQuality(90);
?>

Note that we use "setResolution" not "setImageResolution".
up
2
layzee dot dk at gmail dot com
8 years ago
This method only affects the individual loaded/constructed images.

To set the format of the entire object, use the Imagick::setFormat method. E.g. load TIFF files, then use setFormat('pdf') on the Imagick object, then writeImagesFile('foo.pdf') or getImagesBlob().
up
0
andy at live dot nl
13 years ago
I had a problem when using $im->setImageFormat('jpeg');
Image colors got inverted when i converted pdfs to jpg thumbs.
Had to add $im->setImageColorspace(255); to solve it.

<?php
// read page 1
$im = new imagick( 'test.pdf[ 0]' );

// convert to jpg
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');

//resize
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1); 

//write image on server
$im->writeImage('thumb.jpg');
$im->clear();
$im->destroy();
?>
up
0
barclay[dot]loftus[at]gmail.com
14 years ago
Another note about image output. For jpeg images, the quality is adjusted by Imagick::setCompressionQuality().

<?php
$img
->setCompressionQuality(90);
$img->setImageFormat('jpeg');
header('Content-type: image/jpg');
echo
$img;
?>
up
-2
Devo
14 years ago
A list of formats can be found here: http://www.imagemagick.org/script/formats.php

Formats marked with a W can be output to file with writeImage (capabilities depend on your particular installation of course).

For example:

<?php
// create new imagick object from image.jpg
$im = new Imagick( "image.jpg" );

// change format to png
$im->setImageFormat( "png" );

// output the image to the browser as a png
header( "Content-Type: image/png" );
echo
$im;

// or you could output the image to a file:
//$im->writeImage( "image.png" );
?>

官方地址:https://www.php.net/manual/en/imagick.setimageformat.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3