PHP operation Imagick library printing numerical matrix_PHP tutorial

WBOY
Release: 2016-07-15 13:26:35
Original
1003 people have browsed it

PHP can achieve many picture effects by operating the Imagick library, such as printing an 8*10 digital matrix on a picture. The method to achieve the above effect is as follows:

PHP operates Imagick library code

<ol class="dp-c">
<li class="alt"><span><span class="comment"><font color="#008200">//格子的尺寸 </font></span><span> </span></span></li>
<li class="">
<span></span><span class="vars"><font color="#dd0000">$grid_font_size</font></span><span> = 18;           </span><span class="comment"><font color="#008200">//字体大小 </font></span><span> </span>
</li>
<li class="alt">
<span></span><span class="vars"><font color="#dd0000">$grid_font_color</font></span><span> = </span><span class="string"><font color="#0000ff">"#000"</font></span><span>;      </span><span class="comment"><font color="#008200">//字体颜色 </font></span><span> </span>
</li>
<li class="">
<span></span><span class="vars"><font color="#dd0000">$grid_width</font></span><span> = 36;               </span><span class="comment"><font color="#008200">//格子的宽度 </font></span><span> </span>
</li>
<li class="alt">
<span></span><span class="vars"><font color="#dd0000">$grid_height</font></span><span> = 24;              </span><span class="comment"><font color="#008200">//格子的高度 </font></span><span> </span>
</li>
<li class="">
<span></span><span class="vars"><font color="#dd0000">$grid_origin_x</font></span><span> = 15;           </span><span class="comment"><font color="#008200">//左上角的数字的起始横坐标 </font></span><span> </span>
</li>
<li class="alt">
<span></span><span class="vars"><font color="#dd0000">$grid_origin_y</font></span><span> = 98;            </span><span class="comment"><font color="#008200">//左上角的数字的起始纵坐标 </font></span><span> </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span> #原图  </span></li>
<li class="">
<span> </span><span class="vars"><font color="#dd0000">$image</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> Imagick(</span><span class="string"><font color="#0000ff">'background.jpg'</font></span><span>);  </span>
</li>
<li class="alt"><span>   </span></li>
<li class=""><span> #写入密保卡数据  </span></li>
<li class="alt">
<span> </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span> = </span><span class="vars"><font color="#dd0000">$grid_origin_x</font></span><span>;  </span>
</li>
<li class="">
<span> </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_y</font></span><span> = </span><span class="vars"><font color="#dd0000">$grid_origin_y</font></span><span>;  </span>
</li>
<li class="alt">
<span> </span><span class="keyword"><strong><font color="#006699">foreach</font></strong></span><span> (</span><span class="vars"><font color="#dd0000">$pData</font></span><span> </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$k</font></span><span> => </span><span class="vars"><font color="#dd0000">$v</font></span><span>){  </span>
</li>
<li class="">
<span>   </span><span class="keyword"><strong><font color="#006699">foreach</font></strong></span><span> (</span><span class="vars"><font color="#dd0000">$v</font></span><span> </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$k_grid_data</font></span><span> => </span><span class="vars"><font color="#dd0000">$v_grid_data</font></span><span>){  </span>
</li>
<li class="alt">
<span>     </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span> += </span><span class="vars"><font color="#dd0000">$grid_width</font></span><span>;  </span>
</li>
<li class="">
<span>     </span><span class="vars"><font color="#dd0000">$draw</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> ImagickDraw();  </span>
</li>
<li class="alt">
<span>     </span><span class="vars"><font color="#dd0000">$draw</font></span><span>->setFillColor(</span><span class="vars"><font color="#dd0000">$grid_font_color</font></span><span>);  </span>
</li>
<li class="">
<span>     </span><span class="vars"><font color="#dd0000">$draw</font></span><span>->setFontSize(</span><span class="vars"><font color="#dd0000">$grid_font_size</font></span><span>);  </span>
</li>
<li class="alt">
<span>     </span><span class="vars"><font color="#dd0000">$draw</font></span><span>->annotation(</span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span>, </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_y</font></span><span>, </span><span class="vars"><font color="#dd0000">$v_grid_data</font></span><span>);  </span>
</li>
<li class="">
<span>     </span><span class="vars"><font color="#dd0000">$image</font></span><span>->drawImage(</span><span class="vars"><font color="#dd0000">$draw</font></span><span>);  </span>
</li>
<li class="alt"><span>   }  </span></li>
<li class="">
<span>   </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span> = </span><span class="vars"><font color="#dd0000">$grid_origin_x</font></span><span>;  </span>
</li>
<li class="alt">
<span>   </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_y</font></span><span> += </span><span class="vars"><font color="#dd0000">$grid_height</font></span><span>;  </span>
</li>
<li class=""><span> }  </span></li>
<li class="alt">
<span> </span><span class="vars"><font color="#dd0000">$image</font></span><span>->writeImage(</span><span class="vars"><font color="#dd0000">$ks_ImageSrcPath</font></span><span>. </span><span class="vars"><font color="#dd0000">$pSN</font></span><span>. </span><span class="string"><font color="#0000ff">'.jpg'</font></span><span>);  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span> #释放资源  </span></li>
<li class="">
<span> </span><span class="vars"><font color="#dd0000">$image</font></span><span>->destroy();  </span>
</li>
<li class="alt">
<span> </span><span class="vars"><font color="#dd0000">$draw</font></span><span>->destroy();  </span>
</li>
<li class=""><span> </span></li>
</ol>
Copy after login

The consequence of this is that it must be instantiated every time it is looped An ImagickDraw and executes the drawImage method, which consumes a lot of CPU resources.

can be optimized from the following two points:

1. It is not necessary to execute the new operation every time, one is enough;

2. It is not necessary to execute the drawImage method every time, Once is enough. In other words, the annotation method seems to have the meaning of "additional", so you don't have to worry about the later overwriting the previous one;

The optimized code for PHP operation Imagick library is as follows:

<ol class="dp-c">
<li class="alt"><span><span class="vars"><font color="#dd0000">$draw</font></span><span> = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> ImagickDraw();  </span></span></li>
<li class="">
<span></span><span class="vars"><font color="#dd0000">$draw</font></span><span>->setFillColor(</span><span class="vars"><font color="#dd0000">$grid_font_color</font></span><span>);  </span>
</li>
<li class="alt">
<span></span><span class="vars"><font color="#dd0000">$draw</font></span><span>->setFontSize(</span><span class="vars"><font color="#dd0000">$grid_font_size</font></span><span>);  </span>
</li>
<li class="">
<span></span><span class="keyword"><strong><font color="#006699">foreach</font></strong></span><span> (</span><span class="vars"><font color="#dd0000">$pData</font></span><span> </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$k</font></span><span> => </span><span class="vars"><font color="#dd0000">$v</font></span><span>){  </span>
</li>
<li class="alt">
<span>  </span><span class="keyword"><strong><font color="#006699">foreach</font></strong></span><span> (</span><span class="vars"><font color="#dd0000">$v</font></span><span> </span><span class="keyword"><strong><font color="#006699">as</font></strong></span><span> </span><span class="vars"><font color="#dd0000">$k_grid_data</font></span><span> => </span><span class="vars"><font color="#dd0000">$v_grid_data</font></span><span>){  </span>
</li>
<li class="">
<span>    </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span> += </span><span class="vars"><font color="#dd0000">$grid_width</font></span><span>;  </span>
</li>
<li class="alt">
<span>    </span><span class="vars"><font color="#dd0000">$draw</font></span><span>->annotation(</span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span>, </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_y</font></span><span>, </span><span class="vars"><font color="#dd0000">$v_grid_data</font></span><span>);  </span>
</li>
<li class=""><span>  }  </span></li>
<li class="alt">
<span>   </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_x</font></span><span> = </span><span class="vars"><font color="#dd0000">$grid_origin_x</font></span><span>;  </span>
</li>
<li class="">
<span>   </span><span class="vars"><font color="#dd0000">$tmp_grid_origin_y</font></span><span> += </span><span class="vars"><font color="#dd0000">$grid_height</font></span><span>;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="vars"><font color="#dd0000">$image</font></span><span>->drawImage(</span><span class="vars"><font color="#dd0000">$draw</font></span><span>);  </span>
</li>
</ol>
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446600.htmlTechArticlePHP can achieve many picture effects by operating the Imagick library, such as printing 8*10 numbers on a picture matrix. The method to achieve the above effect is as follows: PHP operation Imagick library code //The ruler of the grid...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template