PHP操作Imagick库可以实现不少图片效果,比如在一张图片上,打印8*10的数字矩阵。以上效果的实现方法如下:
PHP操作Imagick库代码
- //格子的尺寸
- $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方法好像具有“附加”的意味,不用担心后来的覆盖掉之前的;
PHP操作Imagick库优化后代码如下:
- $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);
【编辑推荐】