How to use Imagick to merge and mask images?

前端 未结 4 969
半阙折子戏
半阙折子戏 2020-12-02 08:05

I know very little of image processing and even less of the terminology used, so please bear with me.

Basically, I want to merge two images together where one of t

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 08:31

    So, finally, this should do what you need:

    Original image:

    http://i.stack.imgur.com/b7seR.png

    Opacity mask:

    enter image description here

    Overlay:

    http://i.stack.imgur.com/3ulkM.png

    Output:

    enter image description here

    The code:

    resizeImage(274, 275, Imagick::FILTER_LANCZOS, 1);
    
    // Copy opacity mask
    $base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
    
    // Add overlay
    $base->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);
    
    $base->writeImage('output.png');
    header("Content-Type: image/png");
    
    echo $base;
    ?>
    

    I hope it's right now! Note: In your example it looks like you downscaled the base image, which I didn't (my goal is just to show how the masking is done).

提交回复
热议问题