PHP操作Imagick库
程序员文章站
2022-03-24 19:05:15
...
在一张图片上,打印8*10的数字矩阵,实现如下:
//格子的尺寸
$grid_font_size = 18; //字体大小
$grid_font_color = "#000"; //字体颜色
$grid_width = 36; //格子的宽度
$grid_height = 24; //格子的高度
$grid_origin_x = 15; //左上角的数字的起始横坐标
$grid_origin_y = 98; //左上角的数字的起始纵坐标
#原图
$image = new Imagick('background.jpg');
#写入密保卡数据
$tmp_grid_origin_x = $grid_origin_x;
$tmp_grid_origin_y = $grid_origin_y;
foreach ($pData as $k => $v){
foreach ($v as $k_grid_data => $v_grid_data){
$tmp_grid_origin_x += $grid_width;
$draw = new ImagickDraw();
$draw->setFillColor($grid_font_color);
$draw->setFontSize($grid_font_size);
$draw->annotation($tmp_grid_origin_x, $tmp_grid_origin_y, $v_grid_data);
$image->drawImage($draw);
}
$tmp_grid_origin_x = $grid_origin_x;
$tmp_grid_origin_y += $grid_height;
}
$image->writeImage($ks_ImageSrcPath. $pSN. '.jpg');
#释放资源
$image->destroy();
$draw->destroy();
这样做的后果是,在每次循环的时候,都要实例化一个ImagickDraw,并执行drawImage方法,非常占用CPU资源。
可以从以下两点优化:
1. 不必每次都执行new操作,一个就够了;
2. 不必每次都执行drawImage方法,一次就够了。也就是说,annotation方法好像具有“附加”的意味,不用担心后来的覆盖掉之前的;
代码如下:
$draw = new ImagickDraw();
$draw->setFillColor($grid_font_color);
$draw->setFontSize($grid_font_size);
foreach ($pData as $k => $v){
foreach ($v as $k_grid_data => $v_grid_data){
$tmp_grid_origin_x += $grid_width;
$draw->annotation($tmp_grid_origin_x, $tmp_grid_origin_y, $v_grid_data);
}
$tmp_grid_origin_x = $grid_origin_x;
$tmp_grid_origin_y += $grid_height;
}
$image->drawImage($draw);
--------------------
2009-9-1 更新
--------------------
换种思路!
把8*10的数字矩阵看做“由80个数字、每10个换一行、每2个有一定间距”的字符串,哈!就可以从“调用80次annotation方法”减少到“调用1次”。
只是,我们不得不重新修改背景图的尺寸、“该字符串”在背景图的起始坐标等。
示例代码如下:
$numX = 0;
$str = '';
$tData = explode(",", $pData);
foreach ($tData as $d) {
if (($numX++)%10 == 0) {
$str .= "\n\n";
}
$str .= $d . " ";
}
$draw->setFillColor($grid_font_color);
$draw->setFontSize($grid_font_size);
$draw->annotation($grid_origin_x, $grid_origin_y, $str);
$image->drawImage($draw);
上一篇: 高德地图app家人地图成员怎么备注?