Prepare the canvas
- $im = Imagecreatetruecolor (500, 300);
Preparing Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
$white = Imagecolorallocate ($im, 255, 255, 255);
Background filled with black
- Imagefill ($im, 0,0, $black);
Draw a rectangle, fill it with white
- Imagefilledellipse ($im, 258, 151, $white);
- Output to a browser or save.
- Header ("Content-type:image/png");
- Output picture
- Imagepng ($im);
Close Canvas
- Imagedestroy ($im);
- ?>
Copy CodePHP image processing function 1, math function 2, image processing function Mathematical functions: 1,max (); 2,min (); 3,mt_rand (); randomly taking a number
- Echomt_rand (1,5);
- ?>
Copy CodeMt_rand randomly take a value
Randomly take a value from an array
- $arr = Array ("A", "B", "C", "D", "E");
$rkey = Mt_rand (0,count ($arr)-1);
echo $arr [$rkey];
- ?>
Copy Code4.ceil (); ceiling 5.floor (); 6.round (); rounding
echo ceil (2.4);//3
- echo Floor (2.4);//2
- Echo round (2.4);//2
- Echo round (2.6);//3
?>
Copy Code6.pi (); Pi take pi
- Echo (pi ());
- Echo M_pi;
- ?>
Copy CodePicture processing functions use scenario 1. Verification Code 2. Scaling 3. Cropping 4. Watermark Five steps to wear an image in PHP 1. Prepare the Canvas 2. Prepare Paint 3. Draw an image or text on the canvas 4. Output final image or Cao Cun final image 5. Release Canvas Resources Example:
1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
- If the background is not populated, the default is black
- Imageellipse ($im, 258,151,200,200, $white);
4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeDraw Image: Imagefill (); Imagesetpixel ();//Portrait Point Imageline ();//Draw line Imagerectangle ();//Draw a rectangle Imagepolygon ();// Draw a polygon imageellipse ();//Draw an ellipse imageare (); Draw an arc Imagechar ();//horizontal Draw a character imagestring ();//horizontal draw a line of string Example:
Draw Line
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
- If the background is not populated, the default is black
- Imageline ($im, 0,0,500,300, $white);
- Imageline ($im, 0,300,500,0, $white);
- Imageline ($im, 0,150,500,150, $white);
- Imageline ($im, 250,0,250,300, $white);
4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeExample:
Add interferon
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
- Generate a random point
- for ($i =0; $i < $i + +) {
Imagesetpixel ($im, Mt_rand (0,500), Mt_rand (0,300), $white);
}
- Generate a random line
for ($j =0; $j < $j + +) {
- Imageline ($im, Mt_rand (0,500), Mt_rand (0,300), Mt_rand (0,500), Mt_rand (0,300), $white);
- }//4. outputting the final image or saving the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeExample:
Draw a rectangle:
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
- Imagerectangle ($im, 480, 280, $white);//
- Imagefilledrectangle ($im, 480, 280, $white);//Fill
4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeExample:
Imagepolygon Draw Polygon _ Draw Triangle
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
- $arr = Array (
- 250,20,
- 60,240,
- 440,240
- );
- Imagepolygon ($im, $arr, 3, $white);
4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy Codeexample, draw a 3D pie chart
1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $red = Imagecolorallocate ($im, 255, 0, 0);
- $grayred = Imagecolorallocate ($im, 255, 100, 100);
- $green = imagecolorallocate ($im, 0, 255, 0);
- $blue = imagecolorallocate ($im, 0, 0, 255);
- $gray = Imagecolorallocate ($im, 200, 200, 200);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
- for ($i =0; $i < $i + +) {
- Imagefilledarc ($im, $i, 150+, 0, $gray, Img_arc_pie);
- Imagefilledarc ($im, $i, 150+, $grayred, Img_arc_pie);
- Imagefilledarc ($im, $i, 150+, $green, Img_arc_pie);
- Imagefilledarc ($im, $i, 150+, $blue, Img_arc_pie);
}
- Imagefilledarc ($im, $, 0, $white, Img_arc_pie);
- Imagefilledarc ($im,,,,,,,,, $red, Img_arc_pie);
- Imagefilledarc ($im,,,,,,,, $green, Img_arc_pie);
- Imagefilledarc ($im,,,,,,,, $blue, Img_arc_pie);
4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeExample:
Write text:
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $red = Imagecolorallocate ($im, 255, 0, 0);
- $grayred = Imagecolorallocate ($im, 255, 100, 100);
- $green = imagecolorallocate ($im, 0, 255, 0);
- $blue = imagecolorallocate ($im, 0, 0, 255);
- $gray = Imagecolorallocate ($im, 200, 200, 200);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
$str = "PHP is very much";
Imagestring ($im, 5, 260, $STR, $green);
- 4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeExample:
Write a single character:
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $red = Imagecolorallocate ($im, 255, 0, 0);
- $grayred = Imagecolorallocate ($im, 255, 100, 100);
- $green = imagecolorallocate ($im, 0, 255, 0);
- $blue = imagecolorallocate ($im, 0, 0, 255);
- $gray = Imagecolorallocate ($im, 200, 200, 200);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
$str = "P";
Imagechar ($im, 5, 260, $STR, $green);
- 4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeExample
Write on a picture
- 1. Preparing the canvas
- $im = Imagecreatetruecolor (500,300);
- 2. Preparing the Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $red = Imagecolorallocate ($im, 255, 0, 0);
- $grayred = Imagecolorallocate ($im, 255, 100, 100);
- $green = imagecolorallocate ($im, 0, 255, 0);
- $blue = imagecolorallocate ($im, 0, 0, 255);
- $gray = Imagecolorallocate ($im, 200, 200, 200);
- $white = Imagecolorallocate ($im, 255, 255, 255);
3. Draw an image or text on the canvas
$str = "JUNZAIVIP";
- $file = "e:/php/fonts/simyou. TTF ";
- $file = "fonts/simyou. TTF ";
Imagettftext ($im, 0, $green, $file, $STR);
4. Output the final image or save the final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- 5. Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodeDesign of PHP Verification code
Prepare the canvas
- $im = Imagecreatetruecolor (100,50);
- Preparing Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $gray = Imagecolorallocate ($im, 200, 200, 200);
Fill background
- Imagefill ($im, 0, 0, $gray);
Text coordinates
- $x = (100-4*20)/2 + 6;
- $y = (50-20)/2 + 20;
Draw an image or text on the canvas
Connect the three arrays.
- $strarr = Array_merge (range (1, 9), range (A, z), range (A, z));
Scrambled Array
- Shuffle ($strarr);
Array_slice: Take the first few of the array
- Join converts an array into a string and breaks it with the first variable
- $str = Join ("', Array_slice ($strarr, 0,4));
$file = "E:/php/fonts/msyh.ttf";
- $file = "Fonts/msyh.ttf";
Imagettftext ($im, 0, $x, $y, $black, $file, $STR);
Output final image or save final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodePHP captcha Design: There are two pages involved: index.php & reg.php Description: This verification code version only implements the verification image of the dynamic access to the foreground registration page verification code and generate a picture of the verification code to compare the verification code is made up of digital lowercase capital letters randomly index.php//to implement user registration
- Reg
User Registration page
Copy Codereg.php//to verify that the verification code is correct
Session_Start ();
- echo $_post[' username '];
- echo $_post[' password '];
- $code = Strtolower ($_post[' Vcode ');
Echo $code;
echo " "; - Print_r ($_session);
- echo "
";
- $vstr = Strtolower ($_session[' vstr ');
if ($code = = $vstr) {
- Implement a page jump
- echo "";
- }else{
- echo "";
- echo "Return to registration page";
- echo "";
}
- ?>
Copy CodeAuth.php used to generate a verification code
Open session, before opening session, no output
- Session_Start ();
- $width = 50;//Verification Code background width
- $height = 26;//Verification Code background High speed
- $fonttype = size of Word in 10;//verification code
- Prepare the canvas
- $im = Imagecreatetruecolor ($width, $height);
- Preparing Paint
- $black = imagecolorallocate ($im, 0, 0, 0);
- $gray = Imagecolorallocate ($im, 200, 200, 200);
Fill background
- Imagefill ($im, 0, 0, $gray);
Text coordinates
- $x = ($width -4* $fonttype)/2 +2;
- $y = ($height-$fonttype)/2 + $fonttype;
Draw an image or text on the canvas
Connect the three arrays.
- $strarr = Array_merge (range (1, 9), range (A, z), range (A, z));
Scrambled Array
- Shuffle ($strarr);
Array_slice: Take the first few of the array
- Join converts an array into a string and breaks it with the first variable
- $str = Join ("', Array_slice ($strarr, 0,4));
Put $str into session for easy access to all pages
- $_session[' vstr '] = $str;
$file = "E:/php/fonts/msyh.ttf";
- $file = "Fonts/msyh.ttf";
Imagettftext ($im, $fonttype, 0, $x, $y, $black, $file, $STR);
Output final image or save final image
- Header ("Content-type:image/png");
- Imagepng ($im);
- Releasing Canvas resources
- Imagedestroy ($im);
- ?>
Copy CodePHP Verification Code design: page Jump: 1,php jump
$im = Imagecreatefromjpeg ("lyf.jpg");
$x = Imagesx ($im);
- $y = Imagesy ($im);
Echo $x. $y;
- Exit
Header ("Content-type:image/jpeg");
- Imagejpeg ($im);
- ?>
Copy CodeMethod two gets the size of the picture:
$imgfile = "Lyf.jpg";
$imgarr = getimagesize ($imgfile);
echo " "; - Print_r ($imgarr);
- echo "
";
Exit
$im = Imagecreatefromjpeg ("lyf.jpg");
Echo $x. $y;
Header ("Content-type:image/jpeg");
- Imagejpeg ($im);
- ?>
Copy CodePicture Zoom function:
$imgfile = "Lyf.jpg";
In order to get the wide height of the big picture
- $imgarr = getimagesize ($imgfile);
$MAXW = $imgarr [0];
- $maxh = $imgarr [1];
- $maxt = $imgarr [2];
- $MAXM = $imgarr [' MIME '];
In order to change the big picture into a resource
$im = Imagecreatefromjpeg ("lyf.jpg");
Small map Resources
- $MINW = 100;
- $minh = 400;
- Equal scale Scaling
- if (($MINW/$maxw) > ($minh/$maxh)) {
- $rate = $minh/$maxh;
- }else{
- $rate = $MINW/$maxw;
- }
$MINW = Floor ($MAXW * $rate);
- $minh = Floor ($MAXH * $rate);
- $minim = Imagecreatetruecolor ($MINW, $minh);
Scale a large image to a small image
- Imagecopyresampled ($minim, $im, 0, 0, 0, 0, $MINW, $minh, $maxw, $maxh);
Small graph output
- Header ("content-type:{$MAXM}");
Judging type
- Switch ($MAXT) {
- Case 1:
- $imageout = "Imagegif";
- Break
- Case 2:
- $imageout = "Imagejpeg";
- Break
- Case 3:
- $imageout = "Imagepng";
- Break
}
$imageout ($minim);
- $minfilename = "S_". $imgfile;
- $imageout ($minim, $minfilename);
- Imagejpeg ($im);
- Freeing resources
- Imagedestroy ($maxim);
- Imagedestroy ($minim);
- ?>
Copy CodePicture clipping function: imagecopyresampled (); Image watermark function: imagecopy (); 3, crop 4, Watermark
$maxim = Imagecreatefromjpeg ("lyf.jpg");
- $minim = Imagecreatefromjpeg ("lyf.jpg");
$maxw = Imagesx ($maxim);
- $maxh = Imagesy ($maxim);
$MINW = Imagesx ($minim);
- $minh = Imagesy ($minim);
Imagecopy ($maxim, $minim, $maxw-$MINW, $maxh-$minh, 0, 0, $MINW, $minh);
Header ("Content-type:image/jpeg");
Imagejpeg ($mamim);
- ?>
Copy Code |