系统:
centos
工具:
ImageMagicK(convert命令在ImageMagicK中)
需求:
图片添加水印文字,文字中含有emoji特殊字符
前提步骤:安装ImageMagick
yum install ImageMagick
据说需要提前安装ghostscript,但是我并没有安装依然能用convert命令,可能以前某个时机装过了
命令:
convert -background none pango:' --- <span size="102400"
rise="-0"foreground="white"> 好人?????将军</span> --- ' \
hao.png
image_size=`identify hao.png | awk '{print $3}'`
image_size_info=${image_size//x/,}
convert -gravity North -draw "image over 10,200,$image_size_info 'hao.png'" math.png 2.png
convert的-draw "text x,y '文字' "输出emoji到图片失败,所以想到导出emoji到图片,然后draw image的方法
第一条命令
convert -background none pango:' --- <span size="102400"
rise="-0"foreground="white"> 好人?????将军</span> --- ' \
hao.png
将需要输入的带emoji的文字导成图片,\是续行符
注意pango中的单位以1024为最小单位
Note that most numerical values are multiplied by a factor of 1024 as such the the value of "
size="49152"
" in the above example, means a text pointsize of 48 points. While the negative rise ("rise="-20480"
) means to lower the text position by 20 points (or pixels at 72dpi).
第二条命令
image_size=`identify hao.png | awk '{print $3}'`
image_size_info=${image_size//x/,}
获取图片信息,
identify也是imagemagick中带的工具
image_info=`identify hao.png`获取identify打印信息,里面包含图片宽高
image_size=`identify hao.png | awk '{print $3}'`获取idendity打印信息中的宽高
image_size_info=${image_size//x/,}将x替换为逗号
于是得出图片的宽高信息
identify hao.png的实际输出为
hao.png PNG 1105x127 1105x127+0+0 16-bit DirectClass 105KB 0.000u 0:00.000
所以得出的image_size_info就是1105,127
如果不是shell脚本编程,可以直接使用框架中的图片相关函数获取图片宽高
第三条命令
convert -gravity North -draw "image over 10,200,$image_size_info 'hao.png'" math.png 2.png
相当于
convert -gravity North -draw "image over 10,200,1105,127 'hao.png'" math.png 2.png
在math.png上画图片hao.png,重新生成2.png
-gravity表示相对坐标,如果是North,那么x坐标就无效
-draw "image over 10,200,$image_size_info 'hao.png'"表示把hao.png画到x=10,y=200的地方,宽为1105,高为127
因为设置了-gravity North所以x坐标无效,-gravity还可以设置为east、northeast等方向参数
原图:
math.png
效果图:
2.png
小贴士:
imagemagick说明
http://www.imagemagick.org/Usage/text/
pango参数说明
http://www.imagemagick.org/Usage/text/#pango
convert参数说明
http://www.imagemagick.org/script/convert.php
convert -define参数说明(可以用来修改图片文件的大小,不仅仅是尺寸)( -define jpeg:extent=500kb)
http://www.imagemagick.org/script/command-line-options.php#define
遇到的问题:
虚拟机中的emoji打印正常,centos服务器上emoji打印异常
于是我从虚拟机中把字体文件复制到服务器上就ok了,
使用的字体文件是:/usr/share/fonts/google-noto-emoji/NotoColorEmoji.ttf
其他字体文件是否有效还未可知
php代码:
// 将text导成图片,图片路径为target_png_path
// png_area表示需要限定的画布大小形式为800x600,宽度x高度,传空字符串就由工具自动生成
// font_size需要乘1024,是pango文档说的
// http://www.imagemagick.org/Usage/text/#pango
public function generate_png($text, $target_png_path, $png_area, &$png_width, &$png_height, $font_size, $font_color){
$font_size = $font_size * 1024;
$size = $png_area?"-size $png_area":"";
$gravity = "-gravity center";//文档说不能垂直居中,只能单行水平居中或者左上角
$text = str_replace('"', '\\"', $text);//pango:"" pango:''都可以,但是pango:''可能会影响单引号双引号的显示,所以推荐使用pngo:""然后转义双引号
$command = "convert -background none $size $gravity pango:\"<span size='$font_size' foreground='$font_color'>$text</span>\" $target_png_path";
$output = [];
exec($command,$output,$return_var);
if ($return_var) {
//log
return false;
} else {
if($image_info = getimagesize($target_png_path)){
$png_width = $image_info[0];
$png_height = $image_info[1];
return true;
}
//log
return false;
}
}
php也是有Imagick库的,但是看起来还有点麻烦,先直接用命令输出吧,api调用的方式以后在研究
https://www.php.net/manual/zh/book.imagick.php
圆形裁剪
convert math.png -alpha on -background none \( +clone -channel A -evaluate multiply 0 +channel -fill white -draw "ellipse 800,600 300,300 0,360" \) -compose DstOut -composite circle_out.png
convert math.png -alpha on -background none \( +clone -channel A -evaluate multiply 0 +channel -fill white -draw "ellipse 820,492 820,492 0,360" \) -compose DstIn -composite -resize 240x240! circle_in.png
convert math.png -alpha on -background none \( +clone -channel A -evaluate multiply 0 +channel -fill white -draw "ellipse 800,600 300,300 0,360" \) \( -clone 0,1 -compose DstOut -composite \) \( -clone 0,1 -compose DstIn -composite \) -delete 0,1 circle.png
第一个挖去一个圆形
第二个保留一个椭圆,然后resize成一个圆形
第三个生成了同时生成两个