Home  >  Article  >  Backend Development  >  PHP graphics image manipulation

PHP graphics image manipulation

WBOY
WBOYOriginal
2016-08-08 09:20:09824browse

Introduction to GD library

GD refers to Graphic Device. PHP's GD library is an extension library for processing graphics. Through a series of APIs provided by the GD library, images can be processed or new pictures can be directly generated.

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.

<span>header("content-type: image/png"); $img=imagecreatetruecolor(100, 100); $red=imagecolorallocate($img, 0xFF, 0x00, 0x00); imagefill($img, 0, 0, $red); imagepng($img); imagedestroy($img);<span><span></span></span></span></preubuntu></divmicrosoft></p> <divmicrosoft yahei sans gb neue font-size:14px line-height:21px><p><span>Draw lines</span></p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>To operate graphics, first create a new canvas, and create a true-color blank picture through the imagecreatetruecolor function: </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$img = imagecreatetruecolor(100, 100);</span><p><span>In the GD library for The color used by the brush needs to be allocated through the imagecolorallocate function. The color of the brush is determined by setting the RGB color value via parameters: </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);</span><p><span> Then we draw the line by calling the line segment function imageline, and specify the starting point and end point. Finally get lines. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imageline($img, 0, 0, 100, 100, $red);</span><p><span>After the lines are drawn, the image is output through header and imagepng. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>header("content-type: image/png"); imagepng($img);</span><p><span>Finally, you can call imagedestroy to release the memory occupied by the image. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagedestroy($img);</span><p><span>Through the above steps, you can find that drawing graphics with PHP is very simple, but many times we not only need to output pictures, we may also need to get an image file. We can specify the file name through the imagepng function to save the drawn image. into the file. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagepng($img, 'img.png');</span></preubuntu></preubuntu></preubuntu></preubuntu></preubuntu></preubuntu></divmicrosoft></p> <divmicrosoft yahei sans gb neue font-size:14px line-height:21px><p>Draw text in images</p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>The GD library can perform a variety of basic graphics operations. Commonly used ones include drawing lines, background filling, drawing rectangles, drawing text, etc. </span></p> <p><span>Similar to drawing lines, you first need to create a new picture and initialize the color. </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$img = imagecreatetruecolor(100, 100); $red = imagecolorallocate($img, 0xFF, 0x00, 0x00);</span><p><span>Then use the imagestring function to draw text. This function has many parameters: imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col ), which can be passed $font sets the font size, x and y set the text display position, $s is the text to be drawn, and $col is the color of the text. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagestring($img, 5, 0, 0, "Hello world", $red); header("content-type: image/png"); imagepng($img); imagedestroy</span><span>($img); </span></preubuntu></preubuntu></divmicrosoft></p> <p><span>Output image file</span></p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>We have learned earlier that imagepng can directly output images to the browser, but many times, we want to save the processed image to a file so that Can be used multiple times. Save the image to a file by specifying the path parameter. </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$filename = 'img.png'; imagepng($img, $filename);</span><p><span>Use imagepng to save the image into png format. If you want to save it into other formats, you need to use different functions. Use imagejpeg to save the image into jpeg format, and imagegif to save the image into gif format. It should be noted that imagejpeg The image is compressed, so a quality parameter can also be set. </span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$filename = 'img.jpg'; ?imagejpeg($img, $filename, 80);</span></preubuntu></preubuntu></divmicrosoft></p> <p>Generate image verification code</p> <p><divmicrosoft yahei sans gb neue font-size:14px><p><span>A simple verification code actually outputs a few characters in the picture, which can be achieved through the imagestring function we mentioned in the previous chapter. </span></p> <p><span>But in terms of processing, in order to make the verification code more secure and prevent other programs from automatically recognizing it, it is often necessary to perform some interference processing on the verification code. Usually, some noise points are drawn, interference line segments are drawn, and the output characters are tilted , twist and other operations. </span></p> <p><span>You can use imagesetpixel to draw points to achieve noise interference, but drawing only one point has little effect, so loops are often used here to draw randomly. </span></p> <preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>for($i=0;$i<50;$i++) { imagesetpixel($im, rand(0, 100) , rand(0, 100) , $black); imagesetpixel($im, rand(0, 100) , rand(0, 100) , $green); } </span><span></span></preubuntu></divmicrosoft></p> <br><p><br></p> <pre name="code"><?php $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, 5, 10, 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);

Add watermark to pictures

There are generally two ways to add watermarks to pictures, one is to add a string to the picture, the other is to add a watermark to the picture Add a logo or other picture.

Because what is being processed here is an existing image, you can create a canvas directly from an existing image, and create an image directly from an image file through imagecreatefromjpeg.

<span>$im = imagecreatefromjpeg($filename);</span><p><span>创建图像对象以后,我们就可以通过前面的GD函数,绘制字符串到图像上。如果要加的水印是一个logo图片,那么就需要再建立一个图像对象,然后通过GD函数imagecopy将logo的图像复制到源图像中。</span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>$logo = imagecreatefrompng($filename); imagecopy($im, $logo, 15, 15, 0, 0, $width, $height);</span><p><span>当将logo图片复制到原图片上以后,将加水印后的图片输出保存就完成了加水印处理。</span></p><preubuntu mono line-height:1.6em font-size:13px word-break:break-word><span>imagejpeg(</span><span>$im, $filename); </span></preubuntu></preubuntu></preubuntu></divmicrosoft></p> <br><p></p> <pre name="code"><?php //这里仅仅是为了案例需要准备一些素材图片 $url = 'http://www.iyi8.com/uploadfile/2014/0521/20140521105216901.jpg'; $content = file_get_contents($url); $filename = 'tmp.jpg'; file_put_contents($filename, $content); $url = 'http://wiki.ubuntu.org.cn/images/3/3b/Qref_Edubuntu_Logo.png'; file_put_contents('logo.png', file_get_contents($url)); //开始添加水印操作 $im = imagecreatefromjpeg($filename); $logo = imagecreatefrompng('logo.png'); $size = getimagesize('logo.png'); imagecopy($im, $logo, 15, 15, 0, 0, $size[0], $size[1]); header("content-type: image/jpeg"); imagejpeg($im);

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了PHP图形图像操作,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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