PHP Image graphics operation of the basic use of the GD library tutorial

Source: Internet
Author: User
This article mainly and everyone to share the PHP image graphics operation of the basic use of GD Library tutorial, I hope to help everyone.

1>GD Library Introduction

GD refers to the graphic device,php of the GD library is used to process graphics extension library, through the GD Library provides a series of APIs, you can process the image or directly generate new images.
In addition to the text processing, PHP, through the GD library, JPG, PNG, GIF, SWF and other images can be processed. GD library is commonly used in image watermarking, verification code generation and so on.
PHP is already integrated with the GD library by default and only needs to be opened when it is installed.

General process for creating images

    1. Set the header to tell the browser which MIME type you want to generate

    2. Create an image area that will be based on this image area in future operations

    3. Draw a fill background in a blank image area

    4. Draw a graphic outline on the background enter text

    5. Output Final graphic

    6. Clear All Resources

    7. Other page calls

Header ("Content-type:image/png"); $img =imagecreatetruecolor (+), $red =imagecolorallocate ($img, 0xFF, 0x00, 0x00); Imagefill ($img, 0, 0, $red); Imagepng ($img); Imagedestroy ($img);
    1. Draw lines
      Imageline ()
      Syntax: Imageline (

      Sx

      EX,

      COL);

    2. Draw a Circle
      Imagearc ()
      Syntax: Imagearc (

      CX,

      W

      StartAngle,

      color)

$img = Imagecreatetruecolor (200, 200);//Assign Color $red = imagecolorallocate ($img, 255, 0, 0); $white = Imagecolorallocate ($img, 255, 255, 255);//background filled with white Imagefill ($img, 0,0, $white);//Draw a red round imagearc ($img, N, N,,, 0, $red); Imagepng ($i MG);//Release Memory Imagedestroy ($IMG);
    1. Draw a rectangle
      Imagerectangle ()
      Syntax: Imagerectangle (

      X1,

      X2,

      Col

$img = Imagecreatetruecolor (200, 200);//Assign Color $red = imagecolorallocate ($img, 255, 0, 0); $white = Imagecolorallocate ($img, 255, 255, 255); Imagefill ($img, 0,0, $white);//Draw a red rectangle Imagerectangle ($img, 50,50,100, $red); Imagepng ($img);// Free memory Imagedestroy ($IMG);
    1. Draw text
      Grammar 1:imagestring (

      Font,

      Y

      COL)
      Grammar 2:imagettftext (

      Size

      X

      Color

      Text

Header ("Content-type:image/png");//imagestring font size cannot be set $img = Imagecreatetruecolor (+); $red = Imagecolorallocate ($img, 0xFF, 0x00, 0x00); Imagestring ($img, 5, ten, "Hello World", $red); Imagepng ($img); Imagedestroy ($img), $img 1=imagecreatetruecolor (200,200), $red =imagecolorallocate ($img 1,255,0,0); $white Imagecolorallocate ($img 1,255,255,255); Imagefill ($img 1,0,0, $red); $font = "C:\Windows\Fonts\simhei.ttf"; Imagettftext ($img 1,23,0,100,100, $white, $font, "How are You"); Imagepng ($img 1); Imagedestroy ($img 1);
    1. Draw a noise point
      Syntax: Imagesetpixel (

      X

      Col

Draw 10 noise for ($i =0; $i <10; $i + +) {  imagesetpixel ($img, rand (0), rand (0,), $black);   Imagesetpixel ($img, rand (0, +), rand (0, +), $green);}

Output image file
through Imagepng you can directly output an image to a browser, save the image to a file by specifying a path parameter
1. Imagepng ()
Meaning: Save picture in PNG format
Syntax: imag Epng (

filename)
2. imagejpeg ()
Meaning: Save picture to JPEG format
Syntax: imagepng (filename, $quality)
3. Imagegif ()
Meaning: Save a picture in GIF format
Syntax: imagegif (filename)

Case:
1. Randomly generated verification code (PHP)
2. Add a watermark to a 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.