-
-
//Prepare canvas - $im = imagecreatetruecolor(500, 300);
//Prepare paint
- $black = imagecolorallocate( $im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
//The background is filled with Black
- imagefill($im,0,0, $black);
//Draw a rectangle and fill it with white
- imagefilledellipse($im, 258, 151, 200, 200, $white );
- //Output to the browser or save it
- header("content-type:image/png");
- //Output the image
- imagepng($im);
// Close the canvas
- imagedestroy($im);
- ?>
-
Copy code
php image processing function
1. Mathematical functions
2. Image processing function
Math functions:
1,max();
2,min();
3. mt_rand(); randomly pick a number
mt_rand randomly picks a value
-
-
- //Randomly pick a value from an array
- $arr = array("a","b","c","d","e ");
$rkey = mt_rand(0,count($arr)-1);
echo $arr[$rkey];
- ?> ;
-
Copy code
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
?>
-
Copy code
6.pi(); //π takes pi
-
- echo(pi());
- echo M_PI;
- ?>
Copy code
image processing function usage scenarios
1.Verification code
2.Zoom
3.Crop
4. Watermark
Five steps to create an image in PHP
1. Prepare the canvas
2. Prepare paint
3. Draw images or text on the canvas
4. Output the final image or Caocun final image
5. Release canvas resources
Example:
-
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare paint
- $black = imagecolorallocate($im, 0, 0 , 0);
- $white = imagecolorallocate($im, 255, 255, 255);
//3. Draw images or text on the canvas
- //If the background is not filled, the default It's 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. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy the code
Draw the image:
imagefill();
imagesetpixel();//draw pixels
imageline();//Draw line
imagerectangle();//Draw a rectangle
imagepolygon();//Draw a polygon
imageellipse();//Draw an ellipse
imageare(); draw an arc
imagechar();//Draw a character horizontally
imagestring();//Draw a line of string horizontally
Example:
-
-
//Draw lines
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare paint
- $black = imagecolorallocate($ im, 0, 0, 0);
- $white = imagecolorallocate($im, 255, 255, 255);
//3. Draw images or text on the canvas
- //If The background is not filled, 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. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy code
Example:
-
-
//Add interferon
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare paint
- $black = imagecolorallocate( $im, 0, 0, 0);
- $white = imagecolorallocate($im, 255, 255, 255);
//3. Draw images or text on the canvas
- // Generate random points
- for ($i=0; $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 the final image or save the final image
- header("content-type:image/png ");
- imagepng($im);
- //5. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy code
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);//Filling
// 4. Output the final image or save the final image
- header("content-type:image/png");
- imagepng($im);
- //5. Release canvas resources
- imagedestroy($im);
- ?> < ;/p>
-
Copy code
Example:
-
-
//imagepolygon draw polygon_draw triangle
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare paint
- $black = imagecolorallocate($im, 0, 0, 0);
- $white = imagecolorallocate($im, 255, 255, 255);
//3. Draw images or text on the canvas
- $arr = array(
- 250,20,
- 60,240,
- 440,240
- );
- imagepolygon($im, $arr, 3, $white);
//4. Output final image or save the final image
- header("content-type:image/png");
- imagepng($im);
- //5. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy the code
Example to draw a 3D pie chart
-
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare 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 images 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 p>
}
- 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 the final image or save the final image
- header("content-type:image/png");
- imagepng($im);
- //5 .Release canvas resources
- imagedestroy($im);
- ?>
-
-
Copy code Example:
-
-
//Write text:
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare 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 images or text on the canvas
$str= "PHP is very much"; p>
imagestring($im, 5, 260, 160, $str, $green);
- //4. Output the final image or save the final image
- header("content-type:image/png") ;
- imagepng($im);
- //5. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy code
Example:
-
-
//Write a single character:
-
- //1. Prepare canvas
- $im = imagecreatetruecolor(500,300);
- //2. Prepare 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 images or text on the canvas
$str= "P";
imagechar($im, 5, 260, 160, $str, $green);
- //4. Output the final image or save the final image
- header("content-type:image/png");
- imagepng($im);
- //5. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy code
Example,
-
-
//Write on the picture
-
- //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 images 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 the final image or save the final image
- header("content-type:image/png");
- imagepng($im) ;
- //5. Release canvas resources
- imagedestroy($im);
- ?>
-
Copy code
PHP verification code design
-
-
- //Prepare canvas
- $im = imagecreatetruecolor(100,50);
- //Prepare paint
- $black = imagecolorallocate($im, 0, 0, 0 );
- $gray = imagecolorallocate($im, 200, 200, 200);
//Fill the background
- imagefill($im, 0, 0, $gray);
//Text coordinates
- $x = (100-4*20)/2 + 6;
- $y = (50-20)/2 + 20;
//Draw images or text on the canvas
//Connect three arrays
- $strarr = array_merge(range(1, 9),range(a, z),range(A , Z));
//Shuffle the array
- shuffle($strarr);
//array_slice: Take the first few digits of the array
- // Join turns the array into a string, and uses the first variable as the delimiter
- $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 the final image or save the final image
- header("content-type:image/png");
- imagepng($im );
- //Release canvas resources
- imagedestroy($im);
- ?>
-
-
Copy code
php verification code design: This involves two pages: index.php & reg.php
Description:
This verification code version only implements dynamic acquisition of verification images
Compare the verification code on the front-end registration page with the verification code on the generated image
The verification code is randomly composed of numbers, lowercase letters, and uppercase letters
index.php//Realize user registration
-
-
-
- reg
-
-
-
-
User registration page
- < ;/p>
-
-
Copy code
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 " "; </li>
<li>// print_r( $_SESSION); </li>
<li>// echo " ";
- $vstr = strtolower($_SESSION['vstr']);
if ($code==$vstr) {
- //Realize page jump
- 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>";
}
- ?>
-
Copy code
auth.php is used to generate verification codes
-
-
- //Open session, there cannot be any output before opening session
- session_start();
- $width = 50; //Verification code background width
- $height = 26; //Verification code background high speed
- $fonttype = 10; //Verification code font size
- //Prepare canvas
- $im = imagecreatetruecolor($width,$height);
- //Prepare paint
- $black = imagecolorallocate($im, 0, 0, 0);
- $gray = imagecolorallocate($im, 200, 200, 200);
//Fill the background
- imagefill($im, 0, 0, $gray);
//Text coordinates
- $x = ($width-4*$fonttype)/2 +2;
- $y = ($height-$fonttype)/2 + $fonttype;
//Draw images or text on the canvas
//Connect three arrays
- $strarr = array_merge(range(1, 9),range(a, z),range(A, Z));
//Shuffle the array
- shuffle($strarr); ($strarr, 0,4));
//Put $str into session to facilitate use on all pages
- $_SESSION['vstr'] = $str; p>
$file = "E:/PHP/fonts/msyh.ttf";
- // $file = "fonts/msyh.ttf";
imagettftext($ im, $fonttype, 0, $x, $y, $black, $file, $str);
//Output the final image or save the final image
- header("content-type: image/png");
- imagepng($im);
- //Release canvas resources
- imagedestroy($im);
- ?>
-
-
-
-
- Copy code
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);
- ?>
-
-
-
- Copy code
Method 2 to get the size of the image:
$imgfile = "lyf.jpg";
$imgarr = getimagesize($imgfile);
-
echo " "; </li>
<li>print_r($imgarr); </li>
<li>echo " ";
exit;
echo $x . $y;
header("content-type:image/jpeg ");
- imagejpeg($im);
- ?>
-
-
-
- Copy code
Image zoom function:
-
-
- $imgfile = "lyf.jpg";
//In order to get the width and height of the large image
- $imgarr = getimagesize( $imgfile);
$maxw = $imgarr[0];
- $maxh = $imgarr[1];
- $maxt = $imgarr[2];
- $maxm = $imgarr[ 'mime'];
//In order to turn large images into resources
$im = imagecreatefromjpeg("lyf.jpg");
//Small image resources
- $minw = 100;
- $minh = 400;
- //Equal 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);
//Zoom the large image Into a small image
- imagecopyresampled($minim, $im, 0, 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);
- ?>
-
Copy code
Image cropping function:
imagecopyresampled();
Image watermark function:
imagecopy();
3, Crop
4, watermark
-
-
- $maxim = imagecreatefromjpeg("lyf.jpg");
- $minim = imagecreatefromjpeg("lyf.jpg");
- $maxh = imagesy($maxim);
$minw = imagesx($minim);
- $minh = imagesy($minim); < ;/p>
imagecopy($maxim, $minim, $maxw-$minw, $maxh-$minh, 0, 0, $minw, $minh);
header("content-type:image/jpeg");
imagejpeg($mamim);
- ?>
-
Copy code
|