PHP Tenth lesson PHP image processing function and verification code implementation

Source: Internet
Author: User
Tags imagecopy imagejpeg mathematical functions shuffle

If you like this blog, please visit this blog address: HTTP://BLOG.CSDN.NET/JUNZAIVIP


Profile:

GD Library Drawing:

Mathematical functions

PHP Image processing function

Picture processing function Usage Scenarios

1. Verification Code
2. Zooming
3. Cropping
4. Watermark




GD Library Drawing:


1. Preparing the canvas
2. Preparing the Paint
3. Painting
4. Output image
5. Save the picture
6. Close the canvas


<?php//Prepare canvas $im = Imagecreatetruecolor (500, 300);//Prepare paint $black = imagecolorallocate ($im, 0, 0, 0); $white = Imagecoloral Locate ($im, 255, 255, 255);//background filled with black Imagefill ($im, 0,0, $black);//Draw a rectangle, fill it with white imagefilledellipse ($im, 258, 151, 200, 200 , $white)//output to Browser or save header ("Content-type:image/png");//Output Picture imagepng ($im);//Close Canvas Imagedestroy ($IM); >


PHP Image processing function
1. Mathematical Functions
2. Image processing function




Mathematical functions:
1.max ();
2.min ();
3.mt_rand (); randomly take a number

<?php Echomt_rand (1,5);? >

Mt_rand randomly take a value

<?php//randomly takes a 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

<?php Echo ceil (2.4);//3echo floor (2.4);//2echo round (2.4);//2echo round (2.6);//3?>



6.pi (); Pi take pi

<?php Echo (pi ()); Echo m_pi;? >


Picture processing function Usage Scenarios

1. Verification Code
2. Zooming
3. Cropping
4. Watermark

Five steps to wear a picture in PHP
1. Preparing the canvas
2. Preparing the Paint
3. Draw an image or text on the canvas
4. Output final image or Cao Cun final image
5. Releasing Canvas resources

<?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare 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 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. Release Canvas resource Imagedestroy ($IM); >

To draw an 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 drawing of a character
Imagestring ();//horizontal drawing of a line of string


Draw line <?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare 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 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 resource Imagedestroy ($IM); >

Add interferon <?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare 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 dot for ($i =0; $i < $i + +) {Imagesetpixel ($im, Mt_rand (0,500), Mt_rand (0,300), $white);} Generate random lines for ($j =0; $j < $j + +) {imageline ($im, Mt_rand (0,500), Mt_rand (0,300), Mt_rand (0,500), Mt_rand (0,300), $WH ITE);} 4. Output the final image or save the final image header ("Content-type:image/png"); Imagepng ($im);//5. Release Canvas resource Imagedestroy ($IM); >



Draw a rectangle: <?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare Paint $black = imagecolorallocate ($im, 0, 0, 0); $white = Imagecolorallocate ($im, 255, 255, 255);//3. Draw an image or text imagerectangle on the canvas ($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. Release Canvas resource Imagedestroy ($IM);? >

Imagepolygon Draw Polygon _ Draw Triangle <?php//1. Prepare canvas $im = Imagecreatetruecolor (500,300);//2. Preparing 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. Release Canvas resource Imagedestroy ($IM);? >

Draw a 3D pie chart <?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare Paint $black = imagecolorallocate ($im, 0, 0, 0); $red = Imagecolorallocate ($im, 255, 0, 0); $grayred = Imagecolorallocate ($im, 255, N.); $green = imagecolorallocate ($im, 0, 2 0); $blue = imagecolorallocate ($im, 0, 0, 255), $gray = Imagecolorallocate ($im, $, (); $white = Imagecoloralloca Te ($im, 255, 255, 255);//3. Draw an image on the canvas or text for ($i =0; $i < $i + +) {Imagefilledarc ($im,, 150+ $i, $,, 0, a, $gr Ay,img_arc_pie); Imagefilledarc ($im, 250, 150+ $i, N., $grayred, Img_arc_pie); Imagefilledarc ($im,, 150+ $i, Img_arc_pie, Imagefilledarc, 150+, $green, and the $im, the $i, the $blue, and the Img_arc_pie);} Imagefilledarc ($im, 0, Imagefilledarc, $white, Img_arc_pie), $red ($im, $, N, a, $, a, J, I  Mg_arc_pie); Imagefilledarc ($im, 250, 150, 200, 200, Img_arc_pie) Imagefilledarc (the $im, and the $blue, Img_arc_pie);//4. Output mostEnd image or save the final image header ("Content-type:image/png"); Imagepng ($im);//5. Release Canvas resource Imagedestroy ($IM); >


Reprint Please specify source: HTTP://BLOG.CSDN.NET/JUNZAIVIP


Write text: <?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare Paint $black = imagecolorallocate ($im, 0, 0, 0); $red = Imagecolorallocate ($im, 255, 0, 0); $grayred = Imagecolorallocate ($im, 255, N.); $green = imagecolorallocate ($im, 0, 2 0); $blue = imagecolorallocate ($im, 0, 0, 255), $gray = Imagecolorallocate ($im, $, (); $white = Imagecoloralloca Te ($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. Release Canvas resource Imagedestroy ($IM); >

Write a single character: <?php//1. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare Paint $black = imagecolorallocate ($im, 0, 0, 0); $red = Imagecolorallocate ($im, 255, 0, 0); $grayred = Imagecolorallocate ($im, 255, N.); $green = imagecolorallocate ($im, 0, 2 0); $blue = imagecolorallocate ($im, 0, 0, 255), $gray = Imagecolorallocate ($im, $, (); $white = Imagecoloralloca Te ($im, 255, 255, 255);//3. Draw an image or text $str= "P" on the canvas, Imagechar ($im, 5, 260, $STR, $green)//4. Output the final image or save the final image header (" Content-type:image/png "); Imagepng ($im);//5. Release Canvas resource Imagedestroy ($IM); >

Write <?php//1 on the picture. Prepare the canvas $im = Imagecreatetruecolor (500,300);//2. Prepare Paint $black = imagecolorallocate ($im, 0, 0, 0); $red = Imagecolorallocate ($im, 255, 0, 0); $grayred = Imagecolorallocate ($im, 255, N.); $green = imagecolorallocate ($im, 0, 2 0); $blue = imagecolorallocate ($im, 0, 0, 255), $gray = Imagecolorallocate ($im, $, (); $white = Imagecoloralloca Te ($im, 255, 255, 255);//3. Draw an image or text $str= "JUNZAIVIP" on the canvas; $file = "e:/php/fonts/simyou. TTF ";//$file =" fonts/simyou. TTF "; Imagettftext ($im, 0, $file, $green, $str),//4. Output final image or save final image header (" Content-type:image/png "); Imagepng ($im);//5. Release Canvas resource Imagedestroy ($IM);? >

Design of PHP Verification code

<?php//Prepare canvas $im = Imagecreatetruecolor (100,50);//Prepare paint $black = imagecolorallocate ($im, 0, 0, 0); $gray = Imagecolorallocate ($im, 200, 200, 200);//Fill 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//put three arrays together $strarr = Array_merge (range (1, 9), range (A, z), range (A, z)),//scrambled array shuffle ($strarr);//array_ Slice: Takes the first few//join of an array into a string, and makes the delimiter with the initial 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);//Release Canvas resource Imagedestroy ($IM);? >

PHP captcha Design: There are two pages involved: index.php & reg.php
Description


This verification code version only enables the dynamic acquisition of validated images
The verification code of the foreground registration page and the verification code of the generated picture are compared
The verification code is made up of numbers in lowercase letters.


index.php//to implement user registration


reg.php//to verify that the verification code is correct

<?phpsession_start ();//echo $_post[' username '];//echo $_post[' password ']; $code = Strtolower ($_post[' Vcode ']);// echo $code;//echo "<pre>";//Print_r ($_session);//echo "</pre>"; $vstr = Strtolower ($_session[' vstr ']); if ($code = = $vstr) {//Implement page jump echo "<script>location= ' http://www.baidu.com ' </script>";} Else{echo "<script>alert (' Verification Code input error ') </script>";//echo "<a href= ' index.php ' > Return to registration page </a>"; echo "<script>location= ' index.php ' </script>";}? >

Auth.php used to generate a verification code

<?php//Open session, before opening session, can not have any output session_start (); $width = 50;//Verification code background Width $height = 26;//captcha background High speed $fonttype = 10;// Verify the size of the word in the code//Prepare Canvas $im = Imagecreatetruecolor ($width, $height);//Prepare paint $black = imagecolorallocate ($im, 0, 0, 0); $gray = Imagecolorallocate ($im, 200, 200, 200);//Fill Background imagefill ($im, 0, 0, $gray);//Text coordinate $x = ($width -4* $fonttype)/2 +2; $y = ($heig ht-$fonttype)/2 + $fonttype;//Draw an image or text on canvas//Connect three arrays $strarr = Array_merge (range (1, 9), range (A, z), range (A, z));// Scrambled array shuffle ($strarr);//array_slice: Takes the first few//join of an array into a string, and makes a delimiter with the initial variable $str = Join (", Array_slice ($strarr, 0,4));// Put $str into SESSION, can be convenient for all pages using $_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);//Release Canvas resource Imagedestroy ($IM);? >

PHP Verification Code Design:
Page Jump:
1.php Jump
Header ("location:index.php");


2.js Jump (recommended)
echo "<script>location = ' http://www.baidu.com ' </script>";
echo "<script>location = ' index.php ' </script>";


JS pop-up window:
Echo ' <script>alert ("Verification code is wrong, please re-enter") </script> ";


2. Zooming
Get the width height of the picture
1.getimagesize (); Get the image size can be directly through the picture ("Lyf.jpg") can get all the information of the picture
2.imagesx ();//Get the picture's width must first obtain the picture of the resource (Imagecreatefromjpeg ("lyf.jpg");), in order to get the information of the picture
3.imagesy ();//Get pictures of the high must first obtain the resources of the picture, in order to get the picture information


An existing picture forms a canvas resource:
1.imagecreatefromjpeg ();

<?php$im = Imagecreatefromjpeg ("lyf.jpg"), $x = Imagesx ($im); $y = Imagesy ($im); Echo $x. $y; Exit;header ("Content-type:image/jpeg"); imagejpeg ($im);? >

Method two gets the size of the picture:

<?php$imgfile = "Lyf.jpg", $imgarr = getimagesize ($imgfile), echo "<pre>";p Rint_r ($imgarr); echo "</pre> "; exit; $im = Imagecreatefromjpeg (" lyf.jpg "); Echo $x. $y, Header ("Content-type:image/jpeg"); imagejpeg ($im);? >



Picture Zoom function:
Imagecopyresampled ();


Proportional scaling of pictures:

<?php$imgfile = "lyf.jpg";//in order to get the width of the large image $imgarr = getimagesize ($imgfile); $maxw = $imgarr [0]; $maxh = $imgarr [1]; $maxt = $ IMGARR[2]; $maxm = $imgarr [' MIME '];//in order to turn a large image into a resource $im = Imagecreatefromjpeg ("lyf.jpg");//small graph resource $minw = n; $minh = 400;// Equal scale 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 small figure imagecopy Resampled ($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"; $imageout ($minim); $minfilename = "S_". $imgfile; $imageout ($minim, $minfilename);//imagejpeg ($IM);// Release Resources Imagedestroy ($maxim); Imagedestroy ($minim);? >

Picture clipping function:
Imagecopyresampled ();


Image watermark Function:
Imagecopy ();




3. Cropping
4. Watermark

<?php $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);?>




PHP Tenth lesson PHP image processing function and verification code implementation

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.