PHP Operation Imagick Library can achieve a lot of picture effects, such as on a picture, printing 8*10 digital matrix. The above effects are implemented in the following ways:
PHP Operation Imagick Library code
- //size of lattice
- $grid _font_size = 18; //font size
- $grid _font_color = "#000" ; //Font color
- $grid _width = 36; //width of the lattice
- $grid _height = 24; //height of lattice
- $grid _origin_x = 15; the starting horizontal of the number in the upper-left corner
- $grid _origin_y = 98; the starting ordinate of the number in//upper left corner
- #原图
- $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 ();
The consequence of this is that in each loop, you instantiate a Imagickdraw and execute the DrawImage method, which consumes CPU resources.
Can be optimized from the following two points:
1. Do not need to do new operations every time, one is enough;
2. Do not have to execute the DrawImage method every time, once is enough. In other words, the annotation method seems to have an "attached" meaning, without worrying about the subsequent overwrite of the previous;
PHP Operation Imagick Library is optimized after the code is as follows:
- $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);
http://www.bkjia.com/PHPjc/446600.html www.bkjia.com true http://www.bkjia.com/PHPjc/446600.html techarticle PHP Operation Imagick Library can achieve a lot of picture effects, such as on a picture, printing 8*10 digital matrix. The above effect is implemented as follows: PHP Operation Imagick Library code//lattice ruler ...