PS: Everything described in this article requires support from the ImageMagick library. Most hosts support it, but when I build my own local environment, ImageMagick is not turned on by default. The exec function cannot be disabled either.
1. Cutout
<?php //纯色底抠图 $input="./1.jpg"; //待抠图文件路径 $ouput="./1.png"; //存放路径 $bgcolor="white"; //背景颜色。可使用十六进制色码和rgb(1,1,1) $fuzz="30000"; //容差 exec("convert $input -fuzz $fuzz -transparent $bgcolor $ouput"); //执行抠图操作。并输入到指定路径
Before cutting out (white background):
After cutting out (png transparent):
2. Feather edges (automatically detect background)
<?php $A1='1.png'; $A2='2.png'; $A3='3.png'; $A4='4.png'; $A5='5.png'; $img='待扣图.jpg'; $im = new Imagick($img); //8000为边缘容差,修改此数值可适当去除多余相似杂色 $im->transparentPaintImage($im->getImagePixelColor(0, 0), 0, 8000,0);//生成png格式 $im->setImageFormat("png");//保存文件名 $im->writeImage("$A1");//细化边缘 exec("convert $A1 -threshold 75% $A2"); exec("convert $A2 -fill black -opaque white $A3"); exec("convert $A3 -channel RGBA -blur 0x2 $A4"); exec("convert $A1 $A4 -alpha on -compose copy_opacity -composite $A5"); //删除临时图片 unlink($A1); unlink($A2); unlink($A3); unlink($A4);
Before cutting out
##After cutting out 3. Clipping Mask<?php $img="1.jpg";//背景 $mask="2.png";//蒙版图。png格式 exec("convert $img $mask -alpha on -compose copy_opacity -composite ouput.png");
<?php //-geometry size size 叠加到指定位置坐标 //-compose xxxx 混合模式 $img=‘1.jpg’;//背景图 $img2=‘2.jpg’;//贴图 exec('convert $img $img2 -geometry +480+80 -compose Multiply -composite ouput.jpg');
The above is the detailed content of PHP cutout tutorial (blending mode + clipping mask). For more information, please follow other related articles on the PHP Chinese website!