The use of PHP-GD Library--play with The Fortune Circle PHP (1)

Source: Internet
Author: User
Tags php script

Originally I was to do C #, because the unit reason to do PHP. have been in their own groping, there are problems Baidu, a variety of groups to solve their own problems. The recent occupation into the burnout period, also happens to be more idle. Prepare to update and review your knowledge and skills as a system. This is the first article in this category, hoping to follow the two blog posts per week. Edit summarizes PHP various points of knowledge.

Today we are going to play the pictures. This article series outlines the following

    1. Draw a line to write a word, by the way, generate a verification code
    2. Draw a picture curve, add a watermark spin turn
    3. Figure to look good, style to match
    4. Summarize

1. Draw a line to write a word, by the way, generate a verification code

Drawing a straight line should be the most basic function in drawing, so let's start playing here.  First, learn the basic drawing steps and recognize several functions. This article does not put the GD library that a large pile of functions enumerated, choose the most representative to play to save space. The paint step is simplified to the following

It looks so strange.

<?PHPHeader("Content-type:image/png"); //1. Create    $im= @imagecreate (+) or die("Cannot Initialize new GD image Stream"); $background _color= Imagecolorallocate ($im, 0, 0, 0); $red= Imagecolorallocate ($im, 255, 0, 0); //2. DrawingImageline ($im, 0, 0, 150, 150,$red); //3. OutputImagepng ($im); //4. DestructionImagedestroy ($im);?>

Run the result as shown, create a black background canvas, draw a red line

Create a canvas with Imagecreate, imagecolorallocate set the color, Imageline draw a line, imagepng output, and finally Imagedestroy destroy. Here are some students who know about PHP should have questions: why destroy? while the PHP script ends, all resources are automatically destroyed. But in the script we manually release resources to ensure that the script runs without consuming unnecessary resources.

Then write a few words. On the basis of the original code, add a line of code. Write a few words
1     Header("Content-type:image/png;charset=utf-8");2     //1. Create3     $im= @imagecreate (+) 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 Write Why don't I play with Chinese characters? 7Imagestring ($im, 3, Chinese, and ' Why isn't use? ',$red);8     //2.2 Draw a line9Imageline ($im, 0, 0, 150, 150,$red);Ten     //3. Output OneImagepng ($im); A     //4. Destruction -Imagedestroy ($im);

Run the following effect

Why not use Chinese characters? imagestring Use dot matrix, if you really want to use his words need to construct the Chinese lattice font, and then use Imageloadfont to load.
A more recommended and common practice is to use the Imagettftext function

The first application to a verification code idea to create a fixed long-width canvas, the random number of drawing on the canvas and then output. In order to avoid being easily identified by the program, add noise, and then draw a few color lines. Here's a nice captcha class. Put it in here.

1<?PHP2 //Verification Code Class3 classValidatecode {4      Private $charset= ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 ';//Random Factor5      Private $code;//Verification Code6      Private $codelen= 4;//Verification Code Length7      Private $width= 130;//width8      Private $height= 50;//Height9      Private $img;//Graphics Resource HandleTen      Private $font;//the specified font One      Private $fontsize= 20;//Specify font size A      Private $fontcolor;//Specify font Color -  - //Construction method initialization the       Public function__construct () { -       $this->font =dirname(__file__).‘ /elephant.ttf ';//Note that the font path must be written, otherwise it will not show the picture -      } -  +      //Generate random Code -      Private functionCreatecode () { +       $_len=strlen($this->charset)-1; A        for($i= 0;$i<$this->codelen;$i++) { at        $this->code. =$this->charset[Mt_rand(0,$_len)]; -       } -      } -  -      //Generate Background -      Private functionCreatebg () { in       $this->img = Imagecreatetruecolor ($this->width,$this-height); -       $color= Imagecolorallocate ($this->img,Mt_rand(157,255),Mt_rand(157,255),Mt_rand(157,255)); toImagefilledrectangle ($this->img,0,$this->height,$this->width,0,$color); +      } -  //Generate text the  Private functionCreateFont () { *   $_x=$this->width/$this-Codelen; $    for($i= 0;$i<$this->codelen;$i++) {Panax Notoginseng    $this->fontcolor = Imagecolorallocate ($this->img,Mt_rand(0,156),Mt_rand(0,156),Mt_rand(0,156)); -Imagettftext ($this->img,$this->fontsize,Mt_rand( -30,30),$_x*$i+Mt_rand(1,5),$this->height/1.4,$this->fontcolor,$this->font,$this->code[$i]); the   } +  } A  //generate lines, snowflakes the  Private functionCreateline () { +       //Lines -        for($i= 0;$i<6;$i++) { $        $color= Imagecolorallocate ($this->img,Mt_rand(0,156),Mt_rand(0,156),Mt_rand(0,156)); $Imageline ($this->img,Mt_rand(0,$this->width),Mt_rand(0,$this->height),Mt_rand(0,$this->width),Mt_rand(0,$this->height),$color); -       } -       //Snowflakes the        for($i= 0;$i<100;$i++) { -        $color= Imagecolorallocate ($this->img,Mt_rand(200,255),Mt_rand(200,255),Mt_rand(200,255));WuyiImagestring ($this->img,Mt_rand(1,5),Mt_rand(0,$this->width),Mt_rand(0,$this->height), ' * ',$color); the       } -      } Wu      //Output -      Private functionOutPut () { About       Header(' Content-type:image/png '); $Imagepng ($this-img); -Imagedestroy ($this-img); -      } -  A      //External Generation +       Public functiondoimg () { the       $this-CREATEBG (); -       $this-Createcode (); $       $this-createline (); the       $this-CreateFont (); the       $this-OutPut (); the      } the       -      //Get Verification Code in       Public functionGetCode () { the       return Strtolower($this-code); the      } About } the?>
View Code

Write it here today, next add a watermark spin

The use of PHP-GD Library--play with The Fortune Circle PHP (1)

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.