PHP operations on the Imagick library can achieve a lot of image effects, such as printing 8*10 digital matrices on an image. The above results are implemented as follows:
PHP operation Imagick library code
- // Grid size
- $ Grid_font_size= 18;// Font size
- $ Grid_font_color="#000";// Font color
- $ Grid_width= 36;// The grid width
- $ Grid_height= 24;// Grid height
- $ Grid_origin_x= 15;// Start abscissa of the number in the upper left corner
- $ Grid_origin_y= 98;// Start ordinate of the number in the upper left corner
-
- # Source Image
- $ Image=NewImagick ('Background.jpg');
- # Writing data to the security card
- $ 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=NewImagickDraw ();
- $ 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');
-
- # Releasing resources
- $ Image-> Destroy ();
- $ Draw-> Destroy ();
-
The consequence of doing so is that an ImagickDraw should be instantiated during each loop, and the drawImage method should be executed, which occupies a lot of CPU resources.
The following two optimizations are available:
1. You don't have to execute the new operation every time. One is enough;
2. You do not have to execute the drawImage method every time. That is to say, the annotation method seems to have the meaning of "additional", so you don't have to worry about overwriting the previous one;
After the Imagick library is optimized in PHP, 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);
-