php - PHP中用图案图像填充图像的透明部分

标签 php image imagemagick image-manipulation php-gd

我正在尝试使用 GD 库在透明图像上应用图案。

我有一个反透明图像和几个要应用于占位符的图案图像。

任何关于如何使用 GD 库或图像魔法来实现此目的的建议。

enter image description here

enter image description here

enter image description here

最佳答案

我可以告诉您如何使用 ImageMagick Unix 命令行语法进行操作。基本思想是在衬衫和平铺图案之间使用 -compose 强光。

第一步是修剪图案图像周围的参差不齐的边框。因此,我手动将其裁剪为如下所示,以便平铺时不会显示参差不齐的边框:

enter image description here

使用 -compose 强光时,如果图像的平均颜色为中灰色 (50%),效果最佳。所以首先要获取平均灰度值以及与中灰度的差值:

mean=`convert shirt.png -scale 1x1! -alpha off -format "%[fx:100*mean]\n" info:`
diff=`convert xc: -format "%[fx:50-$mean)]" info:`
echo "mean=$mean; diff=$diff;"

mean=86.9006; diff=-36.9006;

注意:上述方法仅获取不透明衬衫的平均灰度级,并忽略透明区域。

接下来,您需要删除 Alpha channel 并保存以供以后使用。然后通过添加所需的差异来修改没有 Alpha channel 的图像,使其达到中灰色的平均值。然后您需要修改对比度以增加它,以便显示皱纹的阴影。

convert shirt.png -alpha extract mask.png

enter image description here

convert shirt.png -alpha off -evaluate add $diff% -sigmoidal-contrast 10,50% shirt_mod.png

enter image description here

然后,通过调整大小来平铺图案,使图案比原始图案更密集。最后与强光合成并将 mask 放回 Alpha channel 。

convert shirt_mod.png \
\( -clone 0 -resize 400% -tile pattern.png -draw "color 0,0 reset" -resize 25% \) \
-compose hardlight -composite \
mask.png -alpha off -compose copy_opacity -composite \
shirt_pattern.png

enter image description here

请注意,有多种方法可以平铺图案以填充某些图像尺寸。请参阅http://www.imagemagick.org/Usage/canvas/#tile

关于php - PHP中用图案图像填充图像的透明部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46992440/

相关文章:

php - 将 PHP 表单发送到 MySQL 数据库

php - 如何限制用户不对一篇文章进行多次投票?

ios - 如何在Firebase中使用writeToFile来处理超出最大大小的情况?

HTML 动态重复的边框图像

algorithm - 图像调整算法

php - 如何使用 PHP 为图像添加阴影?

php - 创建新闻模块: part ADD

javascript - 为什么我的图库页面加载时间这么长?

Python Wand PDF to List of Images 类型错误

fonts - Imagemagick font_stretch 或字母之间的间距?