Home  >  Article  >  Backend Development  >  Basic tutorial on using PHP image graphics GD library

Basic tutorial on using PHP image graphics GD library

小云云
小云云Original
2018-03-02 11:21:081382browse

This article mainly shares with you the basic tutorial on using the GD library for operating PHP images and graphics. I hope it can help you.

1>Introduction to GD library

GD refers to Graphic Device. PHP’s GD library is an extension library used to process graphics. Through a series of APIs provided by the GD library, you can Process images or directly generate new images.
In addition to text processing, PHP can also process JPG, PNG, GIF, SWF and other images through the GD library. The GD library is commonly used in image watermarking, verification code generation, etc.
PHP has integrated the GD library by default, you just need to enable it during installation.

General process of creating images

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

  2. Create an image area, and all subsequent operations will be based on this image area

  3. Draw a filled background in the blank image area

  4. Draw the graphic outline on the background and input text

  5. Output the final graphic

  6. Clear all resources

  7. Other page calls

header("content-type: image/png");$img=imagecreatetruecolor(100, 100);$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);// 分配颜色$red = imagecolorallocate($img, 255, 0, 0);$white = imagecolorallocate($img, 255, 255, 255);//背景填充白色
imagefill($img,0,0,$white);// 画一个红色的圆
imagearc($img, 100, 100, 150, 150, 0, 360, $red);
imagepng($img);// 释放内存
imagedestroy($img);
  1. Draw a rectangle
    imagerectangle()
    Syntax: imagerectangle (

    x1 ,

    x2 ,

    col)

$img = imagecreatetruecolor(200, 200);// 分配颜色$red = imagecolorallocate($img, 255, 0, 0);$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img,0,0,$white);// 画一个红色的矩形
imagerectangle ($img,50,50,100 ,100 ,$red);
imagepng($img);// 释放内存
imagedestroy($img);
  1. Draw text
    Syntax 1: imagestring (

    font ,

    y ,

    col )
    Syntax 2: imagettftext(

    size,

    x,

    color,

    text)

header("content-type: image/png");//imagestring字体大小设置不了$img = imagecreatetruecolor(100, 100);$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);
imagestring($img, 5, 10, 10, "Hello world", $red);
imagepng($img);
imagedestroy($img);$img1=imagecreatetruecolor(200,200);$red=imagecolorallocate($img1,255,0,0);$white=imagecolorallocate($img1,255,255,255);
imagefill($img1,0,0,$red);$font="C:\Windows\Fonts\simhei.ttf";
imagettftext($img1,23,0,100,100,$white,$font,"你好吗");
imagepng($img1);
imagedestroy($img1);
  1. Drawing noise
    Syntax: imagesetpixel(

    x,

    col)

//绘制10个噪点for($i=0;$i<10;$i++) {
  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); 
  imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green);
}

Output image file
Through imagepng, you can directly output the image to the browser and save the image to the file by specifying the path parameter
1. imagepng()
Meaning: Save the picture in png format
 Syntax: imagepng(

filename)
2. imagejpeg()
Meaning: Save the picture into jpeg format
 Syntax: imagepng(filename,$quality)
3. imagegif()
Meaning: Save the picture into gif format
 Syntax: imagegif(filename)

Case:
1. Randomly generate verification code (php)
2. Add a watermark to the picture

Related recommendations:

Solution to the garbled watermark generated by the GD library

Detailed explanation of how to use PHP Tutorial on using the GD library to complete the verification code effect

What is the GD library? Detailed introduction to loading GD library in PHP

The above is the detailed content of Basic tutorial on using PHP image graphics GD library. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn