Transparent to white in Imagick for PHP

后端 未结 10 1620
暗喜
暗喜 2020-12-16 11:58

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 basically this:

$image          


        
相关标签:
10条回答
  • 2020-12-16 12:26
    $image = new Imagick('transparent.pdf');
    $image->setImageType (imagick::IMGTYPE_TRUECOLOR);
    $image->writeImage('opaque.tif');
    

    did for me!

    (instead of the formerly imagick::IMGTYPE_TRUECOLORMATTE)

    0 讨论(0)
  • 2020-12-16 12:26

    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(...);)

    0 讨论(0)
  • 2020-12-16 12:27

    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 讨论(0)
  • 2020-12-16 12:34

    Try this one:

    $white->newImage($image->getImageWidth(), $image->getImageHeight(), "transparent");
    
    0 讨论(0)
  • 2020-12-16 12:34

    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

    0 讨论(0)
  • 2020-12-16 12:35

    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!

    0 讨论(0)
提交回复
热议问题