当前位置:Gxlcms > PHP教程 > PHP操作Imagick库打印数字矩阵_PHP教程

PHP操作Imagick库打印数字矩阵_PHP教程

时间:2021-07-01 10:21:17 帮助过:15人阅读

PHP操作Imagick库可以实现不少图片效果,比如在一张图片上,打印8*10的数字矩阵。以上效果的实现方法如下:

PHP操作Imagick库代码

  1. //格子的尺寸
  2. $grid_font_size = 18; //字体大小
  3. $grid_font_color = "#000"; //字体颜色
  4. $grid_width = 36; //格子的宽度
  5. $grid_height = 24; //格子的高度
  6. $grid_origin_x = 15; //左上角的数字的起始横坐标
  7. $grid_origin_y = 98; //左上角的数字的起始纵坐标
  8. #原图
  9. $image = new Imagick('background.jpg');
  10. #写入密保卡数据
  11. $tmp_grid_origin_x = $grid_origin_x;
  12. $tmp_grid_origin_y = $grid_origin_y;
  13. foreach ($pData as $k => $v){
  14. foreach ($v as $k_grid_data => $v_grid_data){
  15. $tmp_grid_origin_x += $grid_width;
  16. $draw = new ImagickDraw();
  17. $draw->setFillColor($grid_font_color);
  18. $draw->setFontSize($grid_font_size);
  19. $draw->annotation($tmp_grid_origin_x, $tmp_grid_origin_y, $v_grid_data);
  20. $image->drawImage($draw);
  21. }
  22. $tmp_grid_origin_x = $grid_origin_x;
  23. $tmp_grid_origin_y += $grid_height;
  24. }
  25. $image->writeImage($ks_ImageSrcPath. $pSN. '.jpg');
  26. #释放资源
  27. $image->destroy();
  28. $draw->destroy();

这样做的后果是,在每次循环的时候,都要实例化一个ImagickDraw,并执行drawImage方法,非常占用CPU资源。

可以从以下两点优化:

1. 不必每次都执行new操作,一个就够了;

2. 不必每次都执行drawImage方法,一次就够了。也就是说,annotation方法好像具有“附加”的意味,不用担心后来的覆盖掉之前的;

PHP操作Imagick库优化后代码如下:

  1. $draw = new ImagickDraw();
  2. $draw->setFillColor($grid_font_color);
  3. $draw->setFontSize($grid_font_size);
  4. foreach ($pData as $k => $v){
  5. foreach ($v as $k_grid_data => $v_grid_data){
  6. $tmp_grid_origin_x += $grid_width;
  7. $draw->annotation($tmp_grid_origin_x, $tmp_grid_origin_y, $v_grid_data);
  8. }
  9. $tmp_grid_origin_x = $grid_origin_x;
  10. $tmp_grid_origin_y += $grid_height;
  11. }
  12. $image->drawImage($draw);


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446600.htmlTechArticlePHP操作Imagick库可以实现不少图片效果,比如在一张图片上,打印8*10的数字矩阵。以上效果的实现方法如下: PHP操作Imagick库代码 //格子的尺...

人气教程排行