Home  >  Article  >  Backend Development  >  PHP cutout tutorial (blending mode + clipping mask)

PHP cutout tutorial (blending mode + clipping mask)

WBOY
WBOYOriginal
2018-03-08 19:24:034469browse

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):

PHP cutout tutorial (blending mode + clipping mask)

After cutting out (png transparent):

PHP cutout tutorial (blending mode + clipping mask)

2. Feather edges (automatically detect background)

<?php
$A1=&#39;1.png&#39;;
$A2=&#39;2.png&#39;;
$A3=&#39;3.png&#39;;
$A4=&#39;4.png&#39;;
$A5=&#39;5.png&#39;;
$img=&#39;待扣图.jpg&#39;;
$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

PHP cutout tutorial (blending mode + clipping mask)

##After cutting out

PHP cutout tutorial (blending mode + clipping mask)

3. Clipping Mask

<?php
$img="1.jpg";//背景
$mask="2.png";//蒙版图。png格式
exec("convert $img $mask -alpha on -compose copy_opacity -composite ouput.png");

Background

PHP cutout tutorial (blending mode + clipping mask)

Mask

PHP cutout tutorial (blending mode + clipping mask)

Final effect

PHP cutout tutorial (blending mode + clipping mask)

4. Blending mode

<?php
//-geometry size size 叠加到指定位置坐标
//-compose xxxx 混合模式
$img=‘1.jpg’;//背景图
$img2=‘2.jpg’;//贴图
exec(&#39;convert $img $img2 -geometry +480+80 -compose Multiply -composite ouput.jpg&#39;);

Background image


PHP cutout tutorial (blending mode + clipping mask)

Overlay (white background)

PHP cutout tutorial (blending mode + clipping mask)

Final effect

PHP cutout tutorial (blending mode + clipping mask)


Mixed mode parameter description: ps:-compose The following parameters are mixed mode

php supports the following mixed mode parameters

Multiply, Screen, Bumpmap, Divide, Plus, Minus,

ModulusAdd, ModulusSubtract, Difference, Exclusion, Lighten, Darken, LightenIntensity, DarkenIntensity,


Mixed mode in Chinese and English

1. Normal mode

2. Dissolve mode

3. Darken mode

4. Multiply Multiply mode

5. Color Burn mode

6. Linear Burn mode

7. Lighten mode

8. Screen mode

9. Color Dodge mode

10. Linear Dodge mode

11. Overlay mode

12. Soft Light mode

13. Hard Light mode

14. Vivid Light ) Mode

15. Linear Light Mode

16. Pin Light Mode

17. Diference Mode

18. Exclusion mode

19. Hue mode

20. Saturation mode

21. Color mode

22. Luminosity mode


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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn