GD Library processing Images

Source: Internet
Author: User

in php5 php5 in php.ini gd expansion pack, just remove GD The corresponding annotations of the expansion pack can be used normally. php5 contains the gd library is the upgraded gd2 library, which contains some useful jpg function.

generally generated graphs, by PHP 's document format is stored, but can be directly obtained by using the HTML image insertion Method SRC to get the dynamic graphics. For example, verification code, watermark, thumbnail, and so on.

A Create an image

General process for creating images:

1). set the header to tell the browser what you want to generate MIME type.

2). Create an image area that will be based on this image area in future operations.

3). draws a fill background in a blank image area.

4). draws a graphic outline into the background to enter text.

5). outputs the final graphic.

6). clears all resources.

7). other pages call the image.

  //1 Set Header//header ("Content-type:text/html;charset=utf-8"); Do not write is the default, the default is the HTML document  Header("Content-type:image/png;"); //2 Create a graphics area, image background//two ways  $im= Imagecreatetruecolor (200, 200);//returns a resource handle//3 draw fill background//Fill color in blank image area The prime minister has a color filler.  $blue= Imagecolorallocate ($im, 0, 102, 255); //fills the color to the background, Imagefill ();Imagefill ($im, 0, 0,$blue); //4 Drawing a graphic outline on the background enter text  $white= Imagecolorallocate ($im, 255, 255, 255); //Draw a line imageline ();Imageline ($im, 0, 0, 200, 200,$white); Imageline ($im, 0, 200, 200, 0,$white); //Draw Text imagestring (); Second number font, x y position, text, colorImagestring ($im, 5, A, a, ' Mr.dang ',$white); //5 output Final graphicImagepng ($im);//output PNG format picture//6 emptying ResourcesImagedestroy ($im);

7). other page call Image: Just need to call in another PHP file, output img tag, src write file address of image created on top

  // demo2.php  Echo ' 

Two Simple Small case

1 Verification Code

/*Simple Verification Code case*/    //1 get four-digit letter mixed//mt_rand (); Get random number//dechex (); Decimal to hexadecimal   for($i= 0;$i<4;++$i){      $nmsg.=Dechex(Mt_rand(0, 15)); }    //2 Turn the top into a picture  Header(' Content-type:image/png '); $im= Imagecreatetruecolor (75, 25); $blue= Imagecolorallocate ($im, 0, 102, 255); $white= Imagecolorallocate ($im, 255,255,255); Imagefill ($im, 0, 0,$blue); Imagestring ($im, 5, 20, 5,$nmsg,$white); Imagepng ($im); Imagedestroy ($im);

2 Loading an existing image: this image in advance to prevent and PHP files in the same directory, can be called with relative or absolute address, recommended absolute because faster, our example is absolute, with the magic constant depends on the implementation of

/* load an existing image   */ //   ////   define (' URL ', DirName (__file__). ' \ \ ');//   header (' content-type:image/png ');//   $im = Imagecreatefrompng (URL. ') 2.png ');//   $white = imagecolorallocate ($im, 255, 255, 255);//   imagestring ($im, 5, Ten, ' http:// Www.dang.com ', $white);//   imagepng ($IM);//   Imagedestroy ($im);

3 using fonts in a native system

/* using System-supplied  fonts */ //    $font = ' C:\WINDOWS\FONTS\MSYH.TTC ';//pass the path in//   define (' URL ', DirName (__file__). ' \ \ ');   header (' content-type:image/png ');//   $im = Imagecreatefrompng (URL. ') 2.png ');//   $white = imagecolorallocate ($im, 255, 255, 1);//   Imagettftext ($im, 40,10,50,100, $white, $font, ' Hello, Party hing Ming '); First digit font size, second rotation angle//   imagepng ($IM);//   Imagedestroy ($im);

4 Miniature Chart

/*Thumbnail problem*/  Define(' URL ',dirname(__file__).‘ \\‘); Header(' Content-type:image/png '); //1 Getting the image size  List($width,$height) = (getimagesize(URL. ' 2.png '));//returns an array containing the size of the//2 to reduce the width and height of the original image by a percentage  $_width=$width*0.4; $_height=$height*0.4; //3 Creating a new diagram  $im= Imagecreatetruecolor ($_width,$_height); //4 Load the original, copy the original image to the new diagram//load  $_im= Imagecreatefrompng (URL. ') 2.png '); //Resample the original image, copy it to the new diagram, and then output//imagecopyresampled () at a scale of 0.4; resample the copy picture and resize itImagecopyresampled ($im,$_im, 0, 0, 0, 0,$_width,$_height,$width,$height); //5 Output DestructionImagepng ($im); Imagedestroy ($im); Imagedestroy ($_im);

Note: the PNG format picture used in the example above can be used in other formats (as long as the PNG in the function is replaced). But JPG in the function to write jpeg,bmp to write Wbmp.

GD Library processing Images

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.