This article describes the PHP based on the GD library image processing methods. Share to everyone for your reference, specific as follows:
GD Image Processing Technology
Extension=php_gd2.dll
Create Canvas
Canvas, a resource-type data, an operational image resource
Create canvas (New)
Imagecreate (width,height)//Create a palette based canvas
Imagecreatetruecolor (width,height)//Create a true Color canvas
Create a canvas based on a picture (open)
Imagecreatefromjpeg (URL)
Imagecreatefrompng (URL)
Imagecreatefromgif (URL)
Manipulating canvas
Assign color: If you want to use a color on the canvas, you should first assign the color to the canvas.
(color identification) = Imagecolorallocate (img,r,g,b)
Fill Canvas
Imagefill (img,x,y, color identification)
Output canvas
1. Output to picture file
2. Direct output, you need to tell the browser output for picture information (header ("content-type:image/png;") )
Imagepng (Img[,url])
imagejpeg ()
imagegif ()
Destroying Canvas Resources
Imagedestroy (IMG)
<?php
Header (' content-type:image/png ');
$img = Imagecreate (300,300);
$color = Imagecolorallocate ($img, 223,22,44);
Imagefill ($img, 3,3, $color);
Imagepng ($img);
Imagedestroy ();
? >
The Operation effect chart is as follows:
Verification Code Implementation
<?php
Header (' content-type:image/png ');
$code = ' 123456789abcdefghijklmnpqrstuvwxvz ';
$length = strlen ($code);
$print = ';
For ($i =0 $i <4; $i + +) {
$print. = $code [Mt_rand (0, $length-1)];
} echo $print;
$img = Imagecreatefrompng ('./str.png ');
$color = Mt_rand (0,1) ==1?imagecolorallocate ($img, 0,0,0): Imagecolorallocate ($img, 255,255,255);
Picture size
$img _width = Imagesx ($img);
$img _height = Imagesy ($img);
Font size
$font = 5;
$font _width = Imagefontwidth ($font);
$font _height = Imagefontheight ($font);
$fin _w = ($img _width-$font _width*4)/2;
$fin _h = ($img _height-$font _height)/2;
Imagestring ($img, $font, $fin _w, $fin _h, $print, $color);
Imagepng ($img);
Imagedestroy ($img);
? >
<image src= "gd_string.php" onclick= "this.src= ' gd_string.php?ra= ' +math.random ()" ></image>
The Operation effect chart is as follows:
<?php
session_start ();
$im =imagecreatetruecolor (80,30);
$str = "";
For ($i =0 $i <4; $i + +) {
$str. =dechex (rand (0,15));
$_session[' code ']= $str;
$white =imagecolorallocate ($im, 255,255,255);
Imagestring ($im, rand (2,5), Rand (0,70), Rand (0,10), $str, $white);
Imagettftext ($im, Rand (0,5), Rand (0,180), Rand (0,100), Rand (0,10), $white, "Simhei.ttf", $str);
For ($i =0 $i <20; $i + +) {
$color =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imageline ($im, Rand (0,90), Rand (0,20), Rand (0,100), Rand (0,100), $color);
Header ("Content-type:image/png");
Imagepng ($im);
Imagedestroy ($im);
? >
Note: no extra output before and after picture output
For more information on PHP related content readers can view the site topics: "PHP graphics and pictures Operating skills summary", "Basic PHP Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP array (Array) Operations tips Encyclopedia, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"
I hope this article will help you with the PHP program design.