Home > Article > Backend Development > Basic tutorial on using PHP image graphics GD library
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.
Set the header to tell the browser the MIME type you want to generate
Create an image area, and all subsequent operations will be based on this image area
Draw a filled background in the blank image area
Draw the graphic outline on the background and input text
Output the final graphic
Clear all resources
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);
Draw lines
imageline()
Syntax: imageline(
sX,
eX,
col);
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);
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);
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);
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
filename)
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(
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
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!