php How to use the gd library: First create a PHP sample file; then use the "imagecreatetruecolor" method in the GD library to create a blank picture; finally draw a simple line through imageline.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php gd library usage
The GD library will play an important role where image processing is required in PHP. PHP can create and process a variety of image formats including GIF, PNG, JPEG, WBMP and XPM. Simple Here are a few examples:
1. Use the GD library to create a blank picture, and then draw a simple line
$img=imagecreatetruecolor(100, 100); //创建空白图片 $red=imagecolorallocate($img, 0xFF, 0x00, 0x00); //创建画笔 imageline($img,0,0,100,100,$red); //绘制线条 //输出图像到页面 header("content-type: image/png"); imagepng($img); //释放图片资源 imagedestroy($img);
Then now A red line segment is drawn on the default black background, with coordinates from (0,0) to (100,100)
The effect is as follows:
2. Draw a string
$img = imagecreatetruecolor(100, 100); $red = imagecolorallocate($img, 0xFF, 0x00, 0x00); //开始绘制字符串 imagestring($img,5,0,13,"zengzhiying",$red); header("content-type: image/png"); imagepng($img); imagejpeg($img,'img.jpg',80); //输出图片到文件并设置压缩参数为80 imagedestroy($img);
The 7th line of code is to save the image to a file, directly You can open it, or you can use the imagepng() function to save it as a picture in PNG format
3. Generate a digital verification code [Recommended learning: "PHP Video Tutorial"]
$img = imagecreatetruecolor(100, 40); $black = imagecolorallocate($img, 0x00, 0x00, 0x00); $green = imagecolorallocate($img, 0x00, 0xFF, 0x00); $white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF); imagefill($img,0,0,$white); //绘制底色为白色 //绘制随机的验证码 $code = ''; for($i = 0; $i < 4; $i++) { $code .= rand(0, 9); } imagestring($img, 6, 13, 10, $code, $black); //加入噪点干扰 for($i=0;$i<50;$i++) { imagesetpixel($img, rand(0, 100) , rand(0, 100) , $black); imagesetpixel($img, rand(0, 100) , rand(0, 100) , $green); } //输出验证码 header("content-type: image/png"); imagepng($img); imagedestroy($img);
In this way, a 4-digit random digital verification code is generated, and there are black and green dot interferences. Of course, this is the simplest verification code. Here it is just Demonstrate the general process, the effect is as follows:
4. Add watermark to the picture
$filename = 'tmp.jpg'; $logofile='logo.png'; $im = imagecreatefromjpeg($filename); $logo = imagecreatefrompng($logofile); $size = getimagesize($logofile); imagecopy($im, $logo, 15, 15, 0, 0, $size[0], $size[1]); header("content-type: image/jpeg"); imagejpeg($im); imagedestroy($im);
imagecopy() is a function that adds a watermark. The parameters inside can be adjusted by yourself to make a better watermark
The above is the simplicity of the GD library Once applied, the code can also be used as a function.
The above is the detailed content of Usage of php gd library. For more information, please follow other related articles on the PHP Chinese website!