// Prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
// Prepare paint
- $ Black = imagecolorallocate ($ im, 0, 0, 0 );
$ White = imagecolorallocate ($ im, 255,255,255 );
// Fill the background in black
- Imagefill ($ im, 0, 0, $ black );
// Draw a rectangle and fill it in white
- Imagefilledellipse ($ im, 258,151,200,200, $ white );
- // Output to the browser or save it
- Header ("content-type: image/png ");
- // Output image
- Imagepng ($ im );
// Close the canvas
- Imagedestroy ($ im );
- ?>
Php image processing function 1, math function 2, image processing function Mathematical functions: 1, max (); 2, min (); 3, mt_rand (); random number
- Echomt_rand (1, 5 );
- ?>
Mt_rand random value
// Obtain a random value from an array
- $ Arr = array ("a", "B", "c", "d", "e ");
$ Rkey = mt_rand (0, count ($ arr)-1 );
Echo $ arr [$ rkey];
- ?>
4. 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
?>
6. pi (); // π obtain the circumference rate
- Echo (pi ());
- Echo M_PI;
- ?>
Image processing function scenario 1. verification code 2. scaling 3. cropping 4. watermark Five steps for wearing an image in php 1. prepare the Canvas 2. prepare the paint 3. draw an image or text on the canvas 4. output the final image or caocun final image 5. release the canvas resources Example:
// 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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 filled, the default value is black.
- Imageellipse ($ im, 258,151,200,200, $ white );
// 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Draw an image: imagefill (); imagesetpixel (); // draw the pixel imageline (); // draw the line imagerectangle (); // draw a rectangle imagepolygon (); // draw a polygon imageellipse (); // draw an elliptical imageare (); draw an arc imagechar (); // horizontally draw a character imagestring (); // draw a line of string horizontally Example:
// Draw a line
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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 filled, the default value 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 or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example:
// Add interferon
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare the paint
- $ Black = imagecolorallocate ($ im, 0, 0, 0 );
- $ White = imagecolorallocate ($ im, 255,255,255 );
// 3. draw an image or text on the canvas
- // Generate random points
- For ($ I = 0; I I <1000; $ I ++ ){
Imagesetpixel ($ im, mt_rand (0,500), mt_rand (0,300), $ white );
}
- // Generate random lines
For ($ j = 0; $ j <100; $ j ++ ){
- Imageline ($ im, mt_rand (0,500), mt_rand (0,300), mt_rand (0,500), mt_rand (0,300), $ white );
- } // 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example:
// Draw a rectangle:
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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, 20, 20,480,280, $ white );//
- Imagefilledrectangle ($ im, 20, 20,480,280, $ white); // fill
// 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example:
// Imagepolygon: polyg_draw a triangle
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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 or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example: draw a 3D pie chart
// 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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 <10; $ I ++ ){
- Imagefilledarc ($ im, 250,150 + $ I, 200,200, 0, 70, $ gray, IMG_ARC_PIE );
- Imagefilledarc ($ im, 250,150 + $ I, 200,200, 70,190, $ grayred, IMG_ARC_PIE );
- Imagefilledarc ($ im, 250,150 + $ I, 200,200,190,270, $ green, IMG_ARC_PIE );
- Imagefilledarc ($ im, 250,150 + $ I, 200,200,270,360, $ blue, IMG_ARC_PIE );
}
- Imagefilledarc ($ im, 250,150,200,200, 0, 70, $ white, IMG_ARC_PIE );
- Imagefilledarc ($ im, 250,150,200,200, 70,190, $ red, IMG_ARC_PIE );
- Imagefilledarc ($ im, 250,150,200,200,190,270, $ green, IMG_ARC_PIE );
- Imagefilledarc ($ im, 250,150,200,200,270,360, $ blue, IMG_ARC_PIE );
// 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example:
// Write text:
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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,160, $ str, $ green );
- // 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example:
// Write a single character:
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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,160, $ str, $ green );
- // 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
Example,
// Write on the image
- // 1. prepare the canvas
- $ Im = imagecreatetruecolor (500,300 );
- // 2. prepare 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, 50, 0,100,200, $ green, $ file, $ str );
// 4. output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // 5. release canvas resources
- Imagedestroy ($ im );
- ?>
PHP verification code design
// Prepare the canvas
- $ Im = imagecreatetruecolor (100,50 );
- // Prepare paint
- $ Black = imagecolorallocate ($ im, 0, 0, 0 );
- $ Gray = imagecolorallocate ($ im, 200,200,200 );
// Fill in the background
- Imagefill ($ im, 0, 0, $ gray );
// Text coordinate
- $ X = (100-4*20)/2 + 6;
- $ Y = (50-20)/2 + 20;
// Draw an image or text on the canvas
// Associate the three arrays
- $ Strarr = array_merge (range (1, 9), range (a, z), range (A, Z ));
// Disrupt the array
- Shuffle ($ strarr );
// Array_slice: obtains the first few digits of the array.
- // Join converts the array into a string and uses the first variable as the separator.
- $ Str = join ('', array_slice ($ strarr, 0, 4 ));
$ File = "E:/PHP/fonts/msyh. ttf ";
- // $ File = "fonts/msyh. ttf ";
Imagettftext ($ im, 20, 0, $ x, $ y, $ black, $ file, $ str );
// Output or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // Release canvas resources
- Imagedestroy ($ im );
- ?>
-
Php verification code design: two pages are involved: index. php & reg. php description: This verification code version only allows you to dynamically obtain the verification code of the verification image on the registration page at the front end and compare the verification code generated for the image. the verification code consists of numbers, lowercase letters, and uppercase letters. Index. php // implement user registration
-
- Reg
-
-
-
- User registration page
-
-
-
Reg. php // used to verify whether 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 ){
- // Jump to the page
- Echo "script location = 'http: // www.xingzuo51.com 'script";
- } Else {
- Echo "script" alert ('verification code input error') script ";
- // Echo "return to registration page ";
- Echo "script location = 'index. php' script";
}
- ?>
Auth. php is used to generate a verification code
// Enable session. no output is allowed before the session is enabled.
- Session_start ();
- $ Width = 50; // verification code background width
- $ Height = 26; // verification code background high speed
- $ Fonttype = 10; // The size of the characters in the verification code
- // Prepare the canvas
- $ Im = imagecreatetruecolor ($ width, $ height );
- // Prepare paint
- $ Black = imagecolorallocate ($ im, 0, 0, 0 );
- $ Gray = imagecolorallocate ($ im, 200,200,200 );
// Fill in the background
- Imagefill ($ im, 0, 0, $ gray );
// Text coordinate
- $ X = ($ width-4 * $ fonttype)/2 + 2;
- $ Y = ($ height-$ fonttype)/2 + $ fonttype;
// Draw an image or text on the canvas
// Associate the three arrays
- $ Strarr = array_merge (range (1, 9), range (a, z), range (A, Z ));
// Disrupt the array
- Shuffle ($ strarr );
// Array_slice: obtains the first few digits of the array.
- // Join converts the array into a string and uses the first variable as the separator.
- $ Str = join ('', array_slice ($ strarr, 0, 4 ));
// Put $ str into the session to facilitate use on 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 or save the final image
- Header ("content-type: image/png ");
- Imagepng ($ im );
- // Release canvas resources
- Imagedestroy ($ im );
- ?>
-
Php 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 );
- ?>
Method 2: obtain the image size:
$ 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 );
- ?>
Image scaling function:
$ Imgfile = "lyf.jpg ";
// To obtain the width and height of a large image
- $ Imgarr = getimagesize ($ imgfile );
$ Maxw = $ imgarr [0];
- $ Maxh = $ imgarr [1];
- $ Maxt = $ imgarr [2];
- $ Maxm = $ imgarr ['Mime '];
// To change a large image to a resource
$ Im = imagecreatefromjpeg ("lyf.jpg ");
// Map resource
- $ Minw = 100;
- $ Minh = 400;
- // Proportional 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, $ minw, $ minh, $ maxw, $ maxh );
// Small image output
- Header ("content-type: {$ maxm }");
// Determine the 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 );
- // Release resources
- Imagedestroy ($ maxim );
- Imagedestroy ($ minim );
- ?>
Image cropping 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 );
- ?>
|