Imagick::flattenImages

(PECL imagick 2.0.0)

Imagick::flattenImagesMerges a sequence of images

Описание

Imagick Imagick::flattenImages ( void )

Merges a sequence of images. This is useful for combining Photoshop layers into a single image.

Возвращаемые значения

В случае успешной работы возвращает TRUE.

Ошибки

Вызывает ImagickException при ошибке.

Коментарии

Автор:
Note that the function returns an Imagick object and does not modify the existing object. Below is my code for converting a PNG with transparency into a JPG with a background color. This code illustrates the difference.

<?php

$im 
= new Imagick('image.png');
$im->setImageBackgroundColor('white');

$im->flattenImages(); // This does not do anything.
$im $im->flattenImages(); // Use this instead.

$im->setImageFormat('jpg');
$im->writeImage('image.jpg');

?>
2010-11-30 10:29:34
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html
This is also useful for accurately converting .ico files to .png. (Other types as well, in theory, but I've only tested ico->png.) Simply using setFormat will create a valid .png file, but will result in image artifacts if the original .ico had any transparency. The following code will create an accurate copy:

<?php

$im 
= new Imagick();

// When dealing with .ico files, make sure to setFormat before loading the image or you'll get a nasty exception. See https://bugs.php.net/bug.php?id=58515 for more details.
$im->setFormat("ico");

$im->readImage("favicon.ico");

$im $im->flattenImages(); // Thanks for the tip, Jairu5!

$im->setFormat("png");

$new fopen("favicon.png""w");
$im->writeImageFile($new);
$im->clear();
$im->destroy();

?>
2011-12-05 11:32:57
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html
Автор:
Small correction to Jairu5's code:

If you want to set the background color of a transparent image you have to set it _before_ loading the image.
Flattening images by default uses the background color 'white'. That's why Jairu5's code seem to work at first, but only until you want to actually change the background color to something different than white.

Try using this instead:

<?php

$im 
= new Imagick();
$im->setImageBackgroundColor('green');
$im->readimage('image.png');

$im $im->flattenImages();

$im->setImageFormat('jpg');
$im->writeImage('image.jpg');

?>
2014-02-10 13:15:11
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html
This function is deprecated and will throw a warning in PHP 5.6. Simply replace this call with Imagick::mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN) and it will behave the same way.
2014-11-11 16:38:23
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html
Автор:
As nick said, the function Imagick::flattenImages() is deprecated. Replacing it by Imagick::mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN) partially worked. Images with transparency (ex: .png) get black background even if the image background is set to white.

Old working code:
$im->setImageBackgroundColor('white');
$im = $im->flattenImages();

New working code:
$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(imagick::ALPHACHANNEL_REMOVE);
$im->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);

imagick::ALPHACHANNEL_REMOVE is not shown on the constants page but do works with Imagick 3.2.0RC1.
2015-02-06 21:57:01
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html
Автор:
The actual replacement now that flattenImages() has been deprecated is:

<?php
$im 
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
?>

So you need to (re)assign the returned Imagick object.
2015-03-10 02:05:25
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html
Автор:
Replying to Francois:
<?php
$im
->setImageBackgroundColor('white');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
?>

Imagick::ALPHACHANNEL_REMOVE has been added in 3.2.0b2 (before the RC): http://pecl.php.net/package-info.php?package=imagick&version=3.2.0b2

The problem is with people who want to implement this, but are stuck with an Imagick version < 3.2.0b2. They can't use this constant. However, all is not lost. I've found a reference where someone got this to work using an integer: http://stackoverflow.com/q/28154179/1000608

The number he used is 11, which seems to suggest that the value of Imagick::ALPHACHANNEL_REMOVE is 11, and the function worked correctly in this usecase even before the constant was implemented. So if you're stuck with <3.2.0b2 this is the code you need:

<?php
$im
->setImageBackgroundColor('white');
$im->setImageAlphaChannel(11);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
?>

This works at least as far back as version 3.1.0~rc1-1 (current version of the php5-imagick package in Debian 7).
2015-03-26 15:44:51
http://php5.kiev.ua/manual/ru/imagick.flattenimages.html

    Поддержать сайт на родительском проекте КГБ