PHP uses the GD library to draw and generate CAPTCHA images

Source: Internet
Author: User

First make sure that the php.ini setting has not opened GD extension function, test for example

Print_r (Gd_info ());

Assuming there is a printout of the content, such as the following, the GD feature is open:

Array (    [GD Version] = bundled (2.0.34 compatible)    [FreeType support] + 1    [FreeType Linkage] = Wit H FreeType    [t1lib support] + 1    [gif Read support] + 1    [gif Create support] + 1    [JPG support] = > 1    [PNG support] = 1    [WBMP support] + 1    [XPM support] =     [XBM support] + 1    [JIS -mapped Japanese Font Support] =)

The general process of the GD drawing is as follows:

1. Create a canvas resource

2. Create a color brush

3. Drawing

4. Save picture or Output picture

5. Destroying the Memory canvas resource


Test code such as the following:

<?

Phpheader ("Content-type:image/jpeg"); $width = n; Wide. High $height = n; $image = Imagecreate ($width, $height); First step: Create a blank image $white = imagecolorallocate ($image, 0, 0, 0); The first call to Imagecolorallocate () fills the color palette-based image with the background color, which is the image created with Imagecreate (). $green = imagecolorallocate ($image, 0, 255, 0); Step Two: Assign color imageline to the image ($image, 0, +,-A, $green); The third step: Draw line Imagerectangle ($image, 100,40,300,100, $green); Draw Rectangle Imagearc ($image, max, 0, N, $green); Draw round imagestring ($image, +, +, "PHP is Niubi honghong!", $green); Write String $str= "abcdefghjklmnpqrstuvwxyz23456789"; $randstr = substr (Str_shuffle ($STR), 0,4); Imagestring ($image, 14, 100 , 260, $RANDSTR, $green); Captcha Imagettftext ($image, 0, $green, './msjhbd. TTF ', "Chinese vsenglish"); Chinese verification//imagejpeg ($image, './test.jpg '); Save the picture as Test.jpgimagejpeg ($image) under the current path; Fourth step: Do not add the file name. Direct output to Web page Imagedestroy ($image); Fifth step: Destroy, Recycle resources?>


the test tries the pieces such as the following:



Note: GD Library is powerful enough to draw various reports (such as histogram, pie chart, etc.), thumbnails, watermark and stock charts.

Examples of thumbnail functions:

<?phpheader ("Content-type:image/png"); $width =   The original width. High $height =;  $thumb _width = (int) $width/2; $thumb _height = (int) $height/2; $dst = Imagecreate ($thumb _width, $thumb _height); Create a thumbnail drawing cloth $gray = Imagecolorallocate ($DST, +, +); $src = Imagecreatefrompng ('./me.png '); Read the original image//copy the original to the thumbnail canvas imagecopyresampled ($dst, $src, 0, 0, 0, 0, $thumb _width, $thumb _height, $width, $height); Imagepng ($DST, '/me_thumb.png '); Imagedestroy ($DST); Imagedestroy ($SRC);

>



PHP uses the GD library to draw and generate a Captcha picture

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.