php imagick api蒙板抠图

《php imagick api蒙板抠图》 m3.png

《php imagick api蒙板抠图》 bg.jpg

《php imagick api蒙板抠图》 d4.png

<?php

//imagick 抠图流程

$imagick = new Imagick();
$width = 300;
$height = 435;


$bg = new Imagick("D:/phpStudy/WWW/test/bg.jpg");
$bg->resizeImage($width,$height,Imagick::FILTER_LANCZOS,1);


$pathOut = "D:/phpStudy/WWW/test/test5.jpg";

//1.0 生成衣服背景色,
$color_transparent = new ImagickPixel('#ff00ff'); //transparent 透明色
//$color_transparent = new ImagickPixel('transparent'); //transparent 透明色
$imagick->newImage($width, $height, $color_transparent, 'png');

//2.0 模板图
$path2 = "D:/phpStudy/WWW/test/testt.png";
$imagick2 = new Imagick("D:/phpStudy/WWW/test/m3.png");
$imagick->compositeImage($imagick2,Imagick::COMPOSITE_OVER,0,0);

//3.0 抠图图层, 白色为待抠区(抠成透明,1.0透明度),黑色的保留区0.0透明度,$imagick3 is a hole; 使用alpha通道抠出
$imagick3 = new Imagick("D:/phpStudy/WWW/test/d4.png");
$imagick3->negateImage(FALSE);
// (and/or) Drop matte state of mask. Same as +matte
$imagick3->setImageMatte(FALSE);

//4.0 合成成抠出的模板
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
// Apply holes mask as the new alpha channel.
$imagick->compositeImage($imagick3, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
//$imagick->compositeImage($imagick3,Imagick::COMPOSITE_DSTIN,0,0, Imagick::CHANNEL_ALPHA);

//5.0 背景 + 抠出模板 组合
$bg->compositeImage($imagick,Imagick::COMPOSITE_DEFAULT,0,0);
$bg->writeImage($pathOut);

$imagick3->destroy();
$imagick2->destroy();
$imagick->destroy();
$bg->destroy();

下面是合成效果:

《php imagick api蒙板抠图》 test5.jpg

    原文作者:成木2016
    原文地址: https://www.jianshu.com/p/cc96754f6c4c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞