PHP class 10 PHP image processing functions and verification code implementation, php Image Processing

Source: Internet
Author: User
Tags imagecopy imagejpeg mathematical functions

PHP class 10 PHP image processing functions and verification code implementation, php Image Processing

If you like this blog, visit this blog address: http://blog.csdn.net/junzaivip


Summary:

Gd library drawing:

Mathematical functions

PHP image processing functions

Use Cases of image processing functions

1. Verification Code
2. Zoom
3. Crop
4. Watermark




Gd library drawing:


1. Prepare the canvas
2. Prepare paint
3. Painting
4. output image
5. Save the image
6. Close the canvas


<? Php // prepare the canvas $ im = imagecreatetruecolor (500,300); // prepare the paint $ black = imagecolorallocate ($ im, 0, 0, 0); $ white = imagecolorallocate ($ im, 255,255,255); // fill the background with black imagefill ($ im, 258,151,200,200, $ black); // draw a rectangle and fill it with white imagefilledellipse ($ im, $ white ); // output to the browser or save the header ("content-type: image/png"); // output the image imagepng ($ im ); // close the canvas imagedestroy ($ im);?>


PHP image processing functions
1. mathematical functions
2. Image Processing functions




Mathematical functions:
1. max ();
2. min ();
3. mt_rand (); Random Number

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

Mt_rand random Value

<? Php // 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

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



6. pi (); // π obtain the circumference Rate

<?php echo(pi());echo M_PI;?>


Use Cases of image processing functions

1. Verification Code
2. Zoom
3. Crop
4. Watermark

Five steps for wearing 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

<? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. $ 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 the final image or save the final image header ("content-type: image/png"); imagepng ($ im); // 5. release the canvas resource imagedestroy ($ im);?>

Image Rendering:
Imagefill ();
Imagesetpixel (); // draw a pixel
Imageline (); // draw a line
Imagerectangle (); // draw a rectangle
Imagepolygon (); // draw a polygon
Imageellipse (); // draw an elliptic
Imageare (); draw an arc
Imagechar (); // draw a character horizontally
Imagestring (); // draw a horizontal string


// Draw a line <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. $ 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, 500,300, 0,300,500, $ white); imageline ($ im, 0, $ white); imageline ($ im, 0,150,500,150, $ white); imageline ($ im, 250,300, $ white); // 4. output the final image or save the final image header ("content-type: image/png"); imagepng ($ im); // 5. release canvas resources imagedestro Y ($ im);?>

// Add interferon <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. $ 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 a random line 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 final image or save final image hea Der ("content-type: image/png"); imagepng ($ im); // 5. Release the canvas resource imagedestroy ($ im);?>



// Draw a rectangle: <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. $ black = imagecolorallocate ($ im, 0, 0, 0); $ white = imagecolorallocate ($ im, 255,255,255); // 3. draw an image or text imagerectangle ($ im, 20, 20,480,280, $ white) on the canvas; // imagefilledrectangle ($ im, 20, 20,480,280, $ white ); // fill // 4. output the final image or save the final image header ("content-type: image/png"); imagepng ($ im); // 5. release the canvas resource imagedestroy ($ im);?>

// Imagepolygon: Draw a polyg_draw a triangle <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. $ black = imagecolorallocate ($ im, 0, 0, 0); $ white = imagecolorallocate ($ im, 255,255,255); // 3. draw an image or text on the canvas $ arr = array (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 the canvas resource imagedestroy ($ im);?>

Draw a 3D pie chart <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. paint $ black = imagecolorallocate ($ im, 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 the final image or save the final image header ("content-type: image/png"); imagepng ($ im); // 5. release the canvas resource imagedestroy ($ im);?>


Reprinted please indicate the source: http://blog.csdn.net/junzaivip


// Write text: <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. paint $ black = imagecolorallocate ($ im, 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 the final image or save the final image header ("content-type: image/png"); imagepng ($ im); // 5. release the canvas resource imagedestroy ($ im);?>

// Write a single character: <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. paint $ black = imagecolorallocate ($ im, 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"; imag Echar ($ 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 the canvas resource imagedestroy ($ im);?>

// Write on the image <? Php // 1. prepare the canvas $ im = imagecreatetruecolor (500,300); // 2. paint $ black = imagecolorallocate ($ im, 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 = "junzaiv Ip "; $ 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 the canvas resource imagedestroy ($ im);?>

PHP verification code design

<? Php // prepare the canvas $ im = imagecreatetruecolor (, 50); // prepare the 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: take the first few digits of the array // Join to convert the array into a string, and use the first variable as the separator $ str = join ('', array_sli Ce ($ 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 the canvas resource imagedestroy ($ im);?>

Php verification code design: two pages are involved: index. php & reg. php
Note:


This verification code version only enables dynamic acquisition of verification Images
The verification code on the front-end registration page is compared with the verification code generated for the image
The verification code is composed of lowercase letters, uppercase letters, and numbers.


Index. php // Implement User Registration

<Html> 

Reg. php // used to verify whether 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 the registration page </a> "; echo" <script> Location = 'index. php' </script> ";}?>

Auth. php is used to generate a verification code

<? Php // enable session. No output session_start (); $ width = 50; // Verification Code background width $ height = 26; // Verification Code background High Speed $ fonttype = 10; // size of the characters in the verification code // prepare the canvas $ im = imagecreatetruecolor ($ width, $ height ); // prepare the paint $ black = imagecolorallocate ($ im, 0, 0, 0); $ gray = imagecolorallocate ($ im, 200,200,200); // fill 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 $ str Arr = array_merge (range (1, 9), range (a, z), range (A, Z); // disrupt the array shuffle ($ strarr); // array_slice: take the first few digits of the array // Join to convert the array into a string, and use the first variable as the separator $ str = join ('', array_slice ($ strarr )); // put $ str in the session to facilitate the use of $ _ SESSION ['vstr'] = $ str; $ 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 the canvas resource imagedestroy ($ im);?>

Php verification code design:
Page jump:
1. php jump
Header ("location: index. php ");


2. js redirection (recommended)
Echo "<script> location = 'HTTP: // www.baidu.com '</script> ";
Echo "<script> location = 'index. php' </script> ";


Js pop-up window:
Echo '<script> alert ("the verification code is incorrect. Please enter it again") </script> ';


2. Zoom
Obtain the width and height of an image.
1. getimagesize (); // you can use the image ("lyf.jpg") to obtain all the information of the image.
2. imagesx (); // to obtain the image width, you must first obtain the image resource (imagecreatefromjpeg ("lyf.jpg");) to obtain the image information.
3. imagesy (); // to get the image height, you must first obtain the image resource to get the image information.


Existing images form canvas resources:
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 2: Obtain the image size:

<?php$imgfile = "lyf.jpg";$imgarr = getimagesize($imgfile);echo "<pre>";print_r($imgarr);echo "</pre>";exit;$im = imagecreatefromjpeg("lyf.jpg");echo $x . $y;header("content-type:image/jpeg");imagejpeg($im);?>



Image Scaling function:
Imagecopyresampled ();


Proportional scaling of images:

<? Php $ imgfile = "lyf.jpg"; // to get 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 = imagecreatetrue Color ($ minw, $ minh); // scale the image into a small image imagecopyresampled ($ minim, $ im, 0, 0, 0, 0, $ minw, $ minh, $ maxw, $ maxh); // The output header ("content-type: {$ maxm}"); // The Judgment 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 imagedestroy ($ maxim); imaged Estroy ($ minim);?>

Image cropping function:
Imagecopyresampled ();


Image Watermark function:
Imagecopy ();




3. Crop
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 image Verification Code

This is a phpcms Verification Code. It is very useful after over 10 thousand website experiences.
<? Php

Session_start ();

$ Enablegd = 1;
// Determine whether the image processing function exists
$ Funcs = array ('imagecreatetruecolor', 'imagecolorallocate', 'imagefill', 'imagestring', 'imageline ', 'imagerotate', 'imagedestroy', 'imagecolorallocatealpha ', 'imageellipse', 'imagepng ');
Foreach ($ funcs as $ func)
{
If (! Function_exists ($ func ))
{
$ Enablegd = 0;
Break;
}
}

Ob_clean (); // clear the buffer

If ($ enablegd)
{
// Create captcha
$ Consts = 'cdfgkmnpqrstwxyz23456 ';
$ Vowels = 'aek23456789 ';
For ($ x = 0; $ x <6; $ x ++)
{
$ Const [$ x] = substr ($ consts, mt_rand (0, strlen ($ consts)-1), 1); // obtain a random number in $ consts
$ Vow [$ x] = substr ($ vowels, mt_rand (0, strlen ($ vowels)-1), 1); // obtain a random number in $ vowels
}
$ Radomstring = $ const [0]. $ vow [0]. $ const [2]. $ const [1]. $ vow [1]. $ const [3]. $ vow [3]. $ const [4];
$ _ SESSION ['checkcode'] = $ string = substr ($ radomstring,); // display 4 characters

$ ImageX = strlen ($ radomstring) * 8; // Image Width
$ ImageY = 20; // Image Height
$ Im = imagecreatetruecolor ($ imageX, $ imageY); // create a true color image

// Creates two variables to store color
$ Background = imagecolorallocate ($ im, rand (180,250), rand (180,250), rand (180,250); // background color
$ ForegroundArr = array (imagecolorallocate ($ im, rand (0, 20), rand (0, 20), rand (0, 20 )),
Imagecolorallocate ($ im, rand (0, 20), rand (0, 10), rand (245,255 )),
Imagecolorallocate ($ im, rand (245,... the remaining full text>

How can I use php to implement the verification code?

<? Php
/*
* Filename: authpage. php
*/

Srand (double) microtime () * 1000000 );

// Verify that the user input is consistent with the verification code
If (isset ($ HTTP_POST_VARS ['authinput'])
{
If (strcmp ($ HTTP_POST_VARS ['authnum'], $ HTTP_POST_VARS ['authinput']) = 0)

Echo "Verification Successful! ";
Else
Echo "Verification Failed! ";
}

// Generate a new four-digit integer Verification Code
While ($ authnum = rand () % 10000) <1000 );
?>
<Form action = authpage. php method = post>
<Table>
Enter the verification code: <input type = text name = authinput style = "width:
80px "> <br>
<Input type = submit name = "verify" value = "submit verification code">
<Input type = hidden name = authnum value = <? Echo $ authnum;?>

</Table>
</Form>

?? Code 2:

<? Php
/*
* Filename: authimg. php
* Author: huuworm
* Date: 2003-04-28
* @ Copyleft huuworm.org
*/

// Generate a verification code Image
Header ("Content-type: image/PNG ");
Srand (double) microtime () * 1000000 );
$ Im = I... the remaining full text>

Related Article

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.