PHP operation Imagick library print digital matrix

Source: Internet
Author: User

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

 
 
  1. // Grid size 
  2. $ Grid_font_size= 18;// Font size 
  3. $ Grid_font_color="#000";// Font color 
  4. $ Grid_width= 36;// The grid width 
  5. $ Grid_height= 24;// Grid height 
  6. $ Grid_origin_x= 15;// Start abscissa of the number in the upper left corner 
  7. $ Grid_origin_y= 98;// Start ordinate of the number in the upper left corner 
  8.  
  9. # Source Image
  10.  $ Image=NewImagick ('Background.jpg');
  11. # Writing data to the security card
  12.  $ Tmp_grid_origin_x=$ Grid_origin_x;
  13.  $ Tmp_grid_origin_y=$ Grid_origin_y;
  14.  Foreach($ PData As $ K=>$ V){
  15. Foreach($ V As $ K_grid_data=>$ V_grid_data){
  16. $ Tmp_grid_origin_x+ =$ Grid_width;
  17. $ Draw=NewImagickDraw ();
  18. $ Draw-> SetFillColor ($ Grid_font_color);
  19. $ Draw-> SetFontSize ($ Grid_font_size);
  20. $ Draw-> Annotation ($ Tmp_grid_origin_x,$ Tmp_grid_origin_y,$ V_grid_data);
  21. $ Image-> DrawImage ($ Draw);
  22. }
  23. $ Tmp_grid_origin_x=$ Grid_origin_x;
  24. $ Tmp_grid_origin_y+ =$ Grid_height;
  25. }
  26.  $ Image-> WriteImage ($ Ks_ImageSrcPath.$ PSN.'.Jpg');
  27.  
  28. # Releasing resources
  29.  $ Image-> Destroy ();
  30.  $ Draw-> Destroy ();
  31.  

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:

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.