Use of the php-gd library -- Use php (1) and php-gd together with zhaocuan

Source: Internet
Author: User

Use of the php-gd library -- Use php (1) and php-gd together with zhaocuan

I originally made c #, and switched to php for unit reasons. I have been exploring for myself, having problems with Baidu, and working with various groups to solve my own problems. Recently, the job entered the burnout period, and it happened to be idle. Prepare to update the System and review your knowledge and skills. This is the first article in this category, hoping to speed up two blog posts per week. Edit and summarize various php knowledge points.

 

Let's start playing with images today. The outline of this series of articles is as follows:

1. Draw a line to write a dot and generate a verification code by the way.

Draw a straight line should be the most basic function in drawing, so we will start from here. First, let's take a look at the Basic Drawing steps and understand several functions. In this article, we will not list the many functions in the gd library, and choose the most representative functions to save space. The drawing procedure is simplified as follows:

It looks strange, so let's talk.

<? Php header ("Content-type: image/png"); // 1. create $ im = @ imagecreate (300,300) or die ("Cannot Initialize new GD image stream"); $ background_color = imagecolorallocate ($ im, 0, 0, 0 ); $ red = imagecolorallocate ($ im, 255, 0, 0); // 2. image imageline ($ im, 0, 0,150,150, $ red); // 3. output imagepng ($ im); // 4. destroy imagedestroy ($ im);?>

As shown in the running result, a canvas with a black background is created and a red line is drawn.

Why destroy it? When the php script ends, all resources are automatically destroyed. However, manually releasing resources in the script ensures that the script does not occupy unnecessary resources during running.

Then write a few words. Add a line of code based on the original code. Write several words
1 header ("Content-type: image/png; charset = UTF-8"); 2 // 1. create 3 $ im = @ imagecreate (300,300) or die ("Cannot Initialize new GD image stream"); 4 $ background_color = imagecolorallocate ($ im, 0, 0, 0 ); 5 $ red = imagecolorallocate ($ im, 255, 0, 0); 6 // 2.1 Why do I not need Chinese characters to write? 7 imagestring ($ im, 3,150,150, 'Why not use chinese? ', $ Red); 8 // 2.2 draw a straight line 9 imageline ($ im, 0, 0,150,150, $ red); 10 // 3. output 11 imagepng ($ im); 12 // 4. destroy 13 imagedestroy ($ im );

The running effect is as follows:

Imagestring uses a dot matrix font. If you really want to use it, you need to construct a Chinese dot matrix font and then use imageloadfont to load it.
We recommend that you use the imagettftext function.

The first application uses a verification code to create a canvas with a fixed length and width, and draws the random number to the canvas and then outputs the canvas. To avoid being easily recognized by programs, add noise and draw several lines of chaotic colors. Here is a good Verification Code class. Put it here

1 <? Php 2 // Verification code class 3 class ValidateCode {4 private $ charset = 'hangzhou'; // random factor 5 private $ code; // Verification code 6 private $ codelen = 4; // verification code length: 7 private $ width = 130; // width: 8 private $ height = 50; // height: 9 private $ img; // graphic resource handle: 10 private $ font; // specify the font 11 private $ fontsize = 20; // specify the font size 12 private $ fontcolor; // specify the font color 13 14 // constructor initializes 15 public function _ construct () {16 $ this-> font = Dirname (_ FILE __). '/Elephant. ttf'; // note that the font path must be correct. Otherwise, the image 17} 18 19 may not be displayed. // generate a random code 20 private function createCode () {21 $ _ len = strlen ($ this-> charset)-1; 22 for ($ I = 0; $ I <$ this-> codelen; $ I ++) {23 $ this-> code. = $ this-> charset [mt_rand (0, $ _ len)]; 24} 25} 26 27 // generate background 28 private function createBg () {29 $ this-> img = imagecreatetruecolor ($ this-> width, $ this-> height); 30 $ color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255); 31 imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color); 32} 33 // generate text 34 private function createFont () {35 $ _ x = $ this-> width/$ this-> codelen; 36 for ($ I = 0; $ I <$ this-> codelen; $ I ++) {37 $ this-> fontcolor = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); 38 imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30,30 ), $ _ X * $ I + mt_rand (1.4), $ this-> height/, $ this-> fontcolor, $ this-> font, $ this-> code [$ I]); 39} 40} 41 // generate a line, snowflake 42 private function createLine () {43 // line 44 for ($ I = 0; $ I <6; $ I ++) {45 $ color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156); 46 imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color); 47} 48/ /Snowflake 49 for ($ I = 0; $ I <100; $ I ++) {50 $ color = imagecolorallocate ($ this-> img, mt_rand (200,255 ), mt_rand (200,255), mt_rand (200,255); 51 imagestring ($ this-> img, mt_rand (), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), '*', $ color); 52} 53} 54 // outPut 55 private function outPut () {56 header ('content-type: image/png '); 57 imagepng ($ this-> img); 58 imagedestroy ($ this-> img); 59} 60 61 // 62 public function doimg () is generated externally () {63 $ this-> createBg (); 64 $ this-> createCode (); 65 $ this-> createLine (); 66 $ this-> createFont (); 67 $ this-> outPut (); 68} 69 70 // obtain the verification code 71 public function getCode () {72 return strtolower ($ this-> code ); 73} 74} 75?>View Code

I will write it here today, and add a watermark to rotate it next.

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.