PHP 1 for the GD library

Source: Internet
Author: User
Tags image identifier

GD Library, is the PHP processing graphics extension Library.

Working with graphics in PHP is similar to our usual drawing steps:

1, create the canvas------> ready to draw the place;2, set the brush------> Select the color of the pen to draw;3. Start Drawing.

By separating each step, creating the canvas is to set up a piece of area that you can use to work with the graphic, and the drawing to make sure that the drawing is not visible in this area if the area of the canvas is beyond the set.

The Create canvas is made using Imagecreatetruecolor (), and its specific format is as follows:

The parameters that are received are the width and height of the canvas, represented by the pixel pix, and return an image identifier
Resource$width$height )

The set brush is the color of the selected brush and is set using Imagecolorallocate ():

The first of the received parameters is the image identifier, and the second 3 is the RGB color value
Resource $image $red $green $blue )

Try to draw a diagonal line:

1 $img = Imagecreatetruecolor (200,150);  Set a canvas with a width of 200pix and a height of 150pix 2$red = imagecolorallocate ($img, 0xff,0x00,0x00); Set a red brush 3 imageline ($img, 0,0,200,150,$red);//Draw a red line from (0,0) to (200,150) coordinates  4header("Content-type:image/png");//image/png type 5 imagepng ( $img); Output in PNG format
Imagepng ($img, "myimg.png"); Save the drawn image to the Myimg.png file

Let's see how it works:

This is the effect of drawing a red diagonal line on the 200*150 canvas.

——————————————————————————————————

This simple figure doesn't seem to work, so let's find something we can use everyday to test it, that's the "All-evil verification code."

The principle of verification code is a set of random numbers presented on a canvas, but to prevent machine recognition and other problems, it is necessary to make this canvas of things messy.

Try the application of the GD library with one of the simplest CAPTCHA graphics:

1<?PHP2Set up the canvas and brushes3 $img= Imagecreatetruecolor (45,25);4 $red= Imagecolorallocate ($img, 0xff,0x00,0x00);5 $white= Imagecolorallocate ($img, 0xff,0xff,0xff);6 $black= Imagecolorallocate ($img, 0x00,0x00,0x00);7Imagefill ($img, 0, 0,$black);8Set 4-bit random number9 $code= ' ';Ten  for($i= 0;$i<4;$i++){ One     $code.=Rand(0,9); A } -Imagestring ($img, 5,5,5,$code,$red); Put the generated random number on the canvas -  for($i= 0;$i<100;$i++){ theImagesetpixel ($img,Rand(0,40),Rand(0,25),$white); Randomly draw 100 pixels on the canvas to form a noise. - } -  - Header("Content-type:image/png"); +Imagepng ($img);

The above code is a simple verification code image generation, it produces a black bottom, 4 random red numbers of verification code picture, look at the effect:

If you feel too much interference, you can change the noise to 50, so that the number of pixels will be less interference, the image of the verification code is more clear.

————————————————————————————————————————————

To the picture watermark is also often used in the usual function, the watermark can be a picture, can also be text.

Write an example to try the effect:

1<?PHP2 //picture to add a watermark to3 $img= Imagecreatefromjpeg ('./zjc3.jpg '));4 $red= Imagecolorallocate ($img, 0xff,0x00,0x00);5 6 //The picture you want to add to the picture, using the captcha image saved after the previous build7 $logo= Imagecreatefrompng ('./myimg1.png '));8 $size=getimagesize(' Myimg1.png ');9Imagecopy ($img,$logo, 5,5,0,0,$size[0],$size[1]);Ten  One //set Chinese font, if not set, add Chinese watermark will appear garbled A $font= "C:\WINDOWS\FONTS\SIMSUN.TTC"; - $size 2=getimagesize(' Zjc3.jpg '); -  the //added to the picture of Chinese as "King of the Woman" -Imagettftext ($img, 30, 0,$size 2[0]-160,$size 2[1]-20,$red,$font, "The King's Woman"); -  - Header("Content-type:image/jpeg"); +Imagejpeg ($img);

Take a look at the effect:

PHP 1 for the GD library

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.