开发者

Transparent to white in Imagick for PHP

I have a png image with a transparent background and I want to convert it to a jpg image with a white background.

The code is basicall开发者_C百科y this:

$image = new Imagick('transparent.png');
$image->writeImage('opaque.jpg');

But that creates a black background jpg. I've been struggling with the worst documentation ever trying to find a way to convert the transparent to white to no avail.

Edit: Well, I tried Marc B's idea and kind of got it to work.

$image = new Imagick('transparent.png');
$white = new Imagick();

$white->newImage($image->getImageWidth(), $image->getImageHeight(), "white");
$white->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0);
$white->writeImage('opaque.jpg');

$image->destroy();
$white->destroy();

The problem now is, it always causes the script to segfault.


flattenImages() actually works.

But keep in mind that it doesn't modify the given \Imagick() object but returns a new one:

$image = new \Imagick('transparent.png');

// Need to use the result of $image->flattenImages() here!
$image = $image->flattenImages();
$image->writeImage('opaque.jpg');

flattenImages() defaults to the background color white. If you want to use another background color you have to set it before loading the image:

$image = new \Imagick();

// Needs to be called before the image is loaded!
$image->setbackgroundcolor('green');
$image->readimage('transparent.png');

$image = $image->flattenImages();
$image->writeImage('opaque.jpg');

In general the Imagick API is very sensible when it comes to the order of function calls (just like convert and its parameters on the command line) so always check if your order is correct.

Good luck!

Edit April 2016:

$image->flattenImages() was deprecated and should be replaced by:

$image->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN)

It's hard to find exact informations about this, but it seems that this applies to PHP >= 5.6.

Thanks to vee for the tip!


Try:

$image = new Imagick('transparent.png');
$image->setImageMatte(true);
$image->setImageMatteColor('white');
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE);
$image->writeImage('opaque.jpg');


I ran into the same problem when converting PDFs to PNGs, and I used flattenImages().

        //get the first page of the PDF
        $im = new imagick( $file.'[0]' );

        //set the background to white
        $im->setImageBackgroundColor('white');

        //flatten the image
        $im = $im->flattenImages(); 

        //do the rest of the image operations
        $im->setResolution( 181, 181 );
        $im->setCompressionQuality(100);
        $im->resizeImage ( 181, 181,  imagick::FILTER_LANCZOS, 1, TRUE);
        $im->setImageFormat('png');
        $imageName = $title.'_thumb.png';


$image = new Imagick('transparent.pdf');
$image->setImageType (imagick::IMGTYPE_TRUECOLOR);
$image->writeImage('opaque.tif');

did for me!

(instead of the formerly imagick::IMGTYPE_TRUECOLORMATTE)


Try this one:

$white->newImage($image->getImageWidth(), $image->getImageHeight(), "transparent");


You can try it by changing Imagick constant as shown below

//$image will conatains image which needs background to be transparent
$white = new Imagick();

$white->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel( "white" ));
$white->compositeimage($image, Imagick::COMPOSITE_DEFAULT, $x1OfTransparentImage, $y1OfTransparentImage,);
$white->flattenImages();
$white->writeImage('opaque.jpg');    

$white->destroy();


Try the following, it works for me:

$im = new Imagick('trans.png');
$im->setimagebackgroundcolor('white');
$im = $im->flattenimages();

$im->writeimage('transToWhite.jpg');

Hope that helps!


Regarding the issue with segfault I ran into the same issue.
Apparently $image->writeImage('somename') destroys $image or the reference to it.

I was running into the same issue. The way I got around it was by removing the call to destroy on the object that I had finished writing. Seems sloppy but that solved the issue with the segfault


Segfault issue: I had a similar problem (script kept giving me segfault, even when the image was properly processed and written), the solution I found came after checking bug reports, see: https://bugs.php.net/bug.php?id=61122

Knowing that, try adding
$white->setResourceLimit(6, 1); // 6 means "limit threads to"
before the problematic line (in my case I had to put it before $im->resizeImage(...);)


I had a situation where I was trying to replace transparent background with white (but keep as png). Tried several different methods (including setImageAlphaChannel with setImageBackgroundColor). Combining OP's use of compositeImage I came up with this (hopefully helpful to someone else):

$pic = new Imagick($filelocation); //specify file name
$pic->setResourceLimit(6, 1);
if ($pic->getImageAlphaChannel()) {
    $white = new Imagick();
    $white->newImage($pic->getImageWidth(), $pic->getImageHeight(), "white");
    $white->compositeImage($pic, Imagick::COMPOSITE_OVER, 0, 0);
    $pic = clone $white;
    $white->destroy();
    $pic->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
}
//do more things with $pic
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜