Home Backend Development PHP Tutorial PHP image processing function examples and PHP verification code

PHP image processing function examples and PHP verification code

Jul 25, 2016 am 08:51 AM

  1. //Prepare canvas

  2. $im = imagecreatetruecolor(500, 300);

  3. //Prepare paint

  4. $black = imagecolorallocate( $im, 0, 0, 0);

  5. $white = imagecolorallocate($im, 255, 255, 255);

  6. //The background is filled with Black

  7. imagefill($im,0,0, $black);

  8. //Draw a rectangle and fill it with white

  9. imagefilledellipse($im, 258, 151, 200, 200, $white );
  10. //Output to the browser or save it
  11. header("content-type:image/png");
  12. //Output the image
  13. imagepng($im);

  14. // Close the canvas

  15. imagedestroy($im);
  16. ?>

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

  1. echomt_rand(1,5);
  2. ?>
Copy code

mt_rand randomly picks a value

  1. //Randomly pick a value from an array
  2. $arr = array("a","b","c","d","e ");

  3. $rkey = mt_rand(0,count($arr)-1);

  4. echo $arr[$rkey];

  5. ?> ;

Copy code

4.ceil(); ceiling 5.floor(); 6.round(); rounding

  1. echo ceil(2.4);//3
  2. echo floor(2.4);//2
  3. echo round(2.4);//2
  4. echo round(2.6 );//3

  5. ?>

Copy code

6.pi(); //π takes pi

  1. echo(pi());
  2. echo M_PI;
  3. ?>
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. //1. Prepare canvas
  2. $im = imagecreatetruecolor(500,300);
  3. //2. Prepare paint
  4. $black = imagecolorallocate($im, 0, 0 , 0);
  5. $white = imagecolorallocate($im, 255, 255, 255);

  6. //3. Draw images or text on the canvas

  7. //If the background is not filled, the default It's black
  8. imageellipse($im,258,151,200,200,$white);

  9. //4. Output the final image or save the final image

  10. header("content-type:image/png");
  11. imagepng($im);
  12. //5. Release canvas resources
  13. imagedestroy($im);
  14. ?>

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:

  1. //Draw lines

  2. //1. Prepare canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare paint
  5. $black = imagecolorallocate($ im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);

  7. //3. Draw images or text on the canvas

  8. //If The background is not filled, the default is black
  9. imageline($im,0,0,500,300,$white);
  10. imageline($im,0,300,500,0,$white);
  11. imageline($im,0,150,500,150,$white);
  12. imageline( $im,250,0,250,300,$white);

  13. //4. Output the final image or save the final image

  14. header("content-type:image/png");
  15. imagepng($ im);
  16. //5. Release canvas resources
  17. imagedestroy($im);
  18. ?>

Copy code

Example:

  1. //Add interferon

  2. //1. Prepare canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare paint
  5. $black = imagecolorallocate( $im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);

  7. //3. Draw images or text on the canvas

  8. // Generate random points
  9. for ($i=0; $i < 1000; $i++) {

  10. imagesetpixel($im,mt_rand(0,500),mt_rand(0,300),$white) ;

  11. }

  12. //Generate random lines

  13. for ($j=0; $j < 100; $j++) {

  14. imageline($ im, mt_rand(0,500), mt_rand(0,300), mt_rand(0,500), mt_rand(0,300), $white);
  15. }//4. Output the final image or save the final image
  16. header("content-type:image/png ");
  17. imagepng($im);
  18. //5. Release canvas resources
  19. imagedestroy($im);
  20. ?>

Copy code

Example:

  1. //Draw a rectangle:

  2. //1. Prepare the canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare the paint
  5. $black = imagecolorallocate( $im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);

  7. //3. Draw an image or text on the canvas

  8. imagerectangle( $im, 20, 20, 480, 280, $white);//
  9. imagefilledrectangle($im, 20, 20, 480, 280, $white);//Filling

  10. // 4. Output the final image or save the final image

  11. header("content-type:image/png");
  12. imagepng($im);
  13. //5. Release canvas resources
  14. imagedestroy($im);
  15. ?> < ;/p>
Copy code

Example:

  1. //imagepolygon draw polygon_draw triangle

  2. //1. Prepare canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare paint
  5. $black = imagecolorallocate($im, 0, 0, 0);
  6. $white = imagecolorallocate($im, 255, 255, 255);

  7. //3. Draw images or text on the canvas

  8. $arr = array(
  9. 250,20,
  10. 60,240,
  11. 440,240
  12. );
  13. imagepolygon($im, $arr, 3, $white);

  14. //4. Output final image or save the final image

  15. header("content-type:image/png");
  16. imagepng($im);
  17. //5. Release canvas resources
  18. imagedestroy($im);
  19. ?>

Copy the code

Example to draw a 3D pie chart

  1. //1. Prepare canvas
  2. $im = imagecreatetruecolor(500,300);
  3. //2. Prepare paint
  4. $black = imagecolorallocate($im, 0, 0 , 0);
  5. $red = imagecolorallocate($im, 255, 0, 0);
  6. $grayred = imagecolorallocate($im, 255, 100, 100);
  7. $green = imagecolorallocate($im, 0, 255, 0 );
  8. $blue = imagecolorallocate($im, 0, 0, 255);
  9. $gray = imagecolorallocate($im, 200, 200, 200);
  10. $white = imagecolorallocate($im, 255, 255, 255);

  11. //3. Draw images or text on the canvas

  12. for ($i=0; $i < 10; $i++) {
  13. imagefilledarc($im, 250, 150+$ i, 200, 200, 0, 70, $gray,IMG_ARC_PIE);
  14. imagefilledarc($im, 250, 150+$i, 200, 200, 70, 190, $grayred,IMG_ARC_PIE);
  15. imagefilledarc($im, 250
  16. }

  17. imagefilledarc($im, 250, 150, 200, 200, 0, 70, $white,IMG_ARC_PIE);
  18. imagefilledarc($im, 250, 150, 200, 200, 70, 190, $red,IMG_ARC_PIE);
  19. imagefilledarc($im, 250, 150, 200, 200, 190, 270, $green,IMG_ARC_PIE);
  20. imagefilledarc($im, 250, 150, 200, 200, 270, 360, $blue ,IMG_ARC_PIE);

  21. //4. Output the final image or save the final image

  22. header("content-type:image/png");
  23. imagepng($im);
  24. //5 .Release canvas resources
  25. imagedestroy($im);
  26. ?>

Copy code

Example:

  1. //Write text:

  2. //1. Prepare canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare paint
  5. $black = imagecolorallocate( $im, 0, 0, 0);
  6. $red = imagecolorallocate($im, 255, 0, 0);
  7. $grayred = imagecolorallocate($im, 255, 100, 100);
  8. $green = imagecolorallocate($im , 0, 255, 0);
  9. $blue = imagecolorallocate($im, 0, 0, 255);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. $white = imagecolorallocate($im, 255 , 255, 255);

  12. //3. Draw images or text on the canvas

  13. $str= "PHP is very much";

  14. imagestring($im, 5, 260, 160, $str, $green);

  15. //4. Output the final image or save the final image
  16. header("content-type:image/png") ;
  17. imagepng($im);
  18. //5. Release canvas resources
  19. imagedestroy($im);
  20. ?>

Copy code

Example:

  1. //Write a single character:

  2. //1. Prepare canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare paint
  5. $black = imagecolorallocate ($im, 0, 0, 0);
  6. $red = imagecolorallocate($im, 255, 0, 0);
  7. $grayred = imagecolorallocate($im, 255, 100, 100);
  8. $green = imagecolorallocate($ im, 0, 255, 0);
  9. $blue = imagecolorallocate($im, 0, 0, 255);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. $white = imagecolorallocate($im, 255, 255, 255);

  12. //3. Draw images or text on the canvas

  13. $str= "P";

  14. imagechar($im, 5, 260, 160, $str, $green);

  15. //4. Output the final image or save the final image
  16. header("content-type:image/png");
  17. imagepng($im);
  18. //5. Release canvas resources
  19. imagedestroy($im);
  20. ?>

Copy code

Example,

  1. //Write on the picture

  2. //1. Prepare the canvas
  3. $im = imagecreatetruecolor(500,300);
  4. //2. Prepare the paint
  5. $black = imagecolorallocate ($im, 0, 0, 0);
  6. $red = imagecolorallocate($im, 255, 0, 0);
  7. $grayred = imagecolorallocate($im, 255, 100, 100);
  8. $green = imagecolorallocate($ im, 0, 255, 0);
  9. $blue = imagecolorallocate($im, 0, 0, 255);
  10. $gray = imagecolorallocate($im, 200, 200, 200);
  11. $white = imagecolorallocate($im, 255, 255, 255);

  12. //3. Draw images or text on the canvas

  13. $str= "junzaivip";

  14. $file = " E:/PHP/fonts/SIMYOU.TTF";
  15. // $file = "fonts/SIMYOU.TTF";

  16. imagettftext($im, 50, 0, 100, 200, $ green, $file, $str);

  17. //4. Output the final image or save the final image

  18. header("content-type:image/png");
  19. imagepng($im) ;
  20. //5. Release canvas resources
  21. imagedestroy($im);
  22. ?>

Copy code

PHP verification code design

  1. //Prepare canvas
  2. $im = imagecreatetruecolor(100,50);
  3. //Prepare paint
  4. $black = imagecolorallocate($im, 0, 0, 0 );
  5. $gray = imagecolorallocate($im, 200, 200, 200);

  6. //Fill the background

  7. imagefill($im, 0, 0, $gray);
  8. //Text coordinates

  9. $x = (100-4*20)/2 + 6;
  10. $y = (50-20)/2 + 20;

  11. //Draw images or text on the canvas

  12. //Connect three arrays

  13. $strarr = array_merge(range(1, 9),range(a, z),range(A , Z));

  14. //Shuffle the array

  15. shuffle($strarr);

  16. //array_slice: Take the first few digits of the array

  17. // Join turns the array into a string, and uses the first variable as the delimiter
  18. $str = join('',array_slice($strarr, 0,4));

  19. $file = "E:/PHP/fonts/msyh.ttf";

  20. // $file = "fonts/msyh.ttf";

  21. imagettftext($im, 20, 0, $x, $ y, $black, $file, $str);

  22. //Output the final image or save the final image

  23. header("content-type:image/png");
  24. imagepng($im );
  25. //Release canvas resources
  26. imagedestroy($im);
  27. ?>
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

  1. reg
  2. User registration page


  3. < td>
  4. Name:
    Password:
    Verification code:
    < ;/p>
Copy code

reg.php//Used to verify whether the verification code is correct

  1. session_start();
  2. // echo $_POST['username'];
  3. // echo $_POST['password'];
  4. $code = strtolower( $_POST['vcode']);

  5. // echo $code;

  6. // echo "

    "; </li>
    <li>// print_r( $_SESSION); </li>
    <li>// echo "
    ";
  7. $vstr = strtolower($_SESSION['vstr']);

  8. if ($code==$vstr) {

  9. //Realize page jump
  10. echo "<script>location='http://www.xingzuo51.com'</script>";
  11. }else{
  12. echo "<script>alert(' Verification code input error')</script>";
  13. //echo "Return to registration page";
  14. echo "<script>location=' index.php'</script>";

  15. }

  16. ?>

Copy code

auth.php is used to generate verification codes

  1. //Open session, there cannot be any output before opening session
  2. session_start();

  3. $width = 50; //Verification code background width
  4. $height = 26; //Verification code background high speed
  5. $fonttype = 10; //Verification code font size
  6. //Prepare canvas
  7. $im = imagecreatetruecolor($width,$height);
  8. //Prepare paint
  9. $black = imagecolorallocate($im, 0, 0, 0);
  10. $gray = imagecolorallocate($im, 200, 200, 200);

  11. //Fill the background

  12. imagefill($im, 0, 0, $gray);

  13. //Text coordinates

  14. $x = ($width-4*$fonttype)/2 +2;
  15. $y = ($height-$fonttype)/2 + $fonttype;

  16. //Draw images or text on the canvas

  17. //Connect three arrays

  18. $strarr = array_merge(range(1, 9),range(a, z),range(A, Z));

  19. //Shuffle the array

  20. shuffle($strarr);

    ($strarr, 0,4));

  21. //Put $str into session to facilitate use on all pages

  22. $_SESSION['vstr'] = $str;
  23. $file = "E:/PHP/fonts/msyh.ttf";

  24. // $file = "fonts/msyh.ttf";

  25. imagettftext($ im, $fonttype, 0, $x, $y, $black, $file, $str);

  26. //Output the final image or save the final image

  27. header("content-type: image/png");
  28. imagepng($im);
  29. //Release canvas resources
  30. imagedestroy($im);
  31. ?>
  32. Copy code
php verification code design: Page jump: 1.php jump

    $im = imagecreatefromjpeg("lyf.jpg");

  1. $x = imagesx($im);

  2. $y = imagesy($im);

  3. echo $x . $y;

  4. exit;

  5. header("content-type:image/jpeg");

  6. imagejpeg($im);
  7. ?>

  8. Copy code
Method 2 to get the size of the image:

    $imgfile = "lyf.jpg";

  1. $imgarr = getimagesize($imgfile);

  2. echo "

    "; </li>
    <li>print_r($imgarr); </li>
    <li>echo "
    ";

  3. exit;

  4. echo $x . $y;

  5. header("content-type:image/jpeg ");

  6. imagejpeg($im);
  7. ?>

  8. Copy code

Image zoom function:

  1. $imgfile = "lyf.jpg";

  2. //In order to get the width and height of the large image

  3. $imgarr = getimagesize( $imgfile);

  4. $maxw = $imgarr[0];

  5. $maxh = $imgarr[1];
  6. $maxt = $imgarr[2];
  7. $maxm = $imgarr[ 'mime'];

  8. //In order to turn large images into resources

  9. $im = imagecreatefromjpeg("lyf.jpg");

  10. //Small image resources

  11. $minw = 100;
  12. $minh = 400;

  13. //Equal scaling
  14. if (($minw/$maxw)> ;($minh/$maxh)) {
  15. $rate = $minh/$maxh ;
  16. }else{
  17. $rate = $minw / $maxw ;
  18. }

  19. $minw = floor ($maxw * $rate);

  20. $minh = floor($maxh * $rate);
  21. $minim = imagecreatetruecolor($minw, $minh);

  22. //Zoom the large image Into a small image

  23. imagecopyresampled($minim, $im, 0, 0, 0, 0, $minw, $minh, $maxw, $maxh);

  24. //Small image output

  25. header ("content-type:{$maxm}");

  26. //Determine the type

  27. switch ($maxt) {
  28. case 1:
  29. $imageout = "imagegif";
  30. break;
  31. case 2:
  32. $imageout = "imagejpeg";
  33. break;
  34. case 3:
  35. $imageout = "imagepng";
  36. break;

  37. }

  38. $imageout($minim);

  39. $minfilename = "s_".$imgfile;
  40. $imageout($minim,$minfilename);
  41. // imagejpeg($im);

  42. / /Release resources
  43. imagedestroy($maxim);
  44. imagedestroy($minim);
  45. ?>

Copy code

Image cropping function: imagecopyresampled();

Image watermark function: imagecopy();

3, Crop

4, watermark

  1. $maxim = imagecreatefromjpeg("lyf.jpg");
  2. $minim = imagecreatefromjpeg("lyf.jpg");

  3. $maxh = imagesy($maxim);

  4. $minw = imagesx($minim);

  5. $minh = imagesy($minim); < ;/p>
  6. imagecopy($maxim, $minim, $maxw-$minw, $maxh-$minh, 0, 0, $minw, $minh);

  7. header("content-type:image/jpeg");

  8. imagejpeg($mamim);

  9. ?>

Copy code


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles