您现在的位置: 365建站网 > 365学习 > PHP操作Imagick库打印数字矩阵

PHP操作Imagick库打印数字矩阵

文章来源:365jz.com     点击数:62    更新时间:2009-12-20 00:36   参与评论

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

本文作者:未知

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛


发表评论 (62人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码: 点击我更换图片
最新评论
------分隔线----------------------------