PHP Graphics Image Processing Basics

Source: Internet
Author: User

&LT;?PHP/*GD Library Introduction GD refers to the graphic device,php 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 PHP In addition to text processing, With the GD library, you can process jpg, PNG, GIF, SWF, and so on. GD library commonly used in the picture watermark, verification code claims and so on PHP default has generated the GD library, only need to open on the installation of the */header ("Content-type:image/png"); #新建一个画布, The Imagecreatetruecolor function allows you to create a blank picture of a true Color $img = imagecreatetruecolor (100, 100); $red  =  Imagecolorallocate ($img, 0xff,0x00,0x00), Imagefill ($img, 0,0, $red), Imagepng ($img), $imagedestroy ($IMG);/* Drawing lines The color of the brush used in the GD library needs to be assigned through the Imagecolorallcoate function, which determines the color of the brush by setting the color value of the RGB */$red  = imagecolorallocate ($img, 0xff,0x00,0x00);///Then we can draw the line by calling the draw segment function Imageline, by specifying the starting point and ending point to get the line Imageline ($img, 0,0,100,100, $red)//Draw Well, Image output Header ("Content-type:image/png") imagepng ($IMG)//via header and Imagepng Finally, you can call Imagedestroy to release the Memory Imagedestroy ($IMG)/* To draw text in an image similar to drawing a line, first create a new picture and initialize the color and then use the Imagestring function to draw the text, The parameters of this function are many imagestring (resource  $image,int  $font,int  $x,int  $y,string  $s,int  $col) You can set the font by $font, X, y to set the position of the text display, $s is the text to be drawn, $col is the color of the text */imagestRing ($img, 5,0,0, "Hello world", $red);/* Generate an image verification code simple verification code is actually output a few characters in the picture, through the above imagestring function can be implemented but processing, in order to make the verification code more secure , preventing other programs from automatically identifying, so often need to do some interference processing of the verification code, usually draw some noise interference line, the output of the character skew/distort and so on can use Imagesetpixel plot points to achieve noise interference, but only to draw some points of the role of not only, As a result, this is often done randomly using loops, etc. */$img  = imagecreatetruecolor (100,40); $black  = imagecolorallocate ($img, 0x00,0x00,0x00); $green  = imagecolorallocate ($img, 0x00,0xff,0x00); $white  =  Imagecolorallocate ($img, 0xff,0x00,0x00); 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 a watermark to the picture with two methods, one is to add a string to the picture, The other is to add a logo or other image to the image because it is already existing image, you can directly from the existing image to create a canvas, through the imagecreatefromjpeg can be directly from the image file creation * * *$img  = imagecreatefromjpeg ($filename);/* After creating the image object, we can draw the string to the image through the previous GD function, if the watermark to be added is a logo image, Then you need to create an image object and then copy the image of the logo to the source image via the GD function Imagecopy */$logo  = imagecreatefrompng ($filename); Imagecopy ($img , $logo, 15,15,0,0, $width, $height)//The logo image is copied to the source image, after the image is added watermark after the completion of the addition of watermark processing//Here is only for the case to prepare some material pictures $url =  ' Http://www.stchat.cn/data/attachment/forum/201506/12/100759pidbdaydh8dy7iby.jpg '; $content  = file_ Get_contents ($url); $filename  =  ' tmp.jpg '; file_put_contents ($filename,  $content); $url  =   '; file_put_contents (' Logo.png ',  file_get_contents ($url));//Start adding a watermark operation $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);? >

Zhongzhiyuan Nanjing 904727147, Jiangsu

PHP Graphics Image Processing Basics

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.