Home Backend Development PHP Tutorial PHP basic learning: image processing

PHP basic learning: image processing

May 22, 2018 pm 01:49 PM
php Image Processing Base

In the process of learning PHP, you will encounter image processing. This article will introduce the image processing method.

<!-- Image processing -->

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

&lt;?php

//     图片处理gd2配置文件修改

?&gt;

    

&lt;!-- 用图片处理函数画一张图 --&gt;

&lt;?php

//     $img = imagecreatetruecolor(500, 500);

//     $red = imagecolorallocate($img, 255, 0, 0);

//     $green = imagecolorallocate($img, 0, 255, 0);

//     $blue = imagecolorallocate($img, 0, 0, 255);

//     $pur = imagecolorallocate($img, 255, 0, 255);

//     $yellow = imagecolorallocate($img, 121, 72, 0);

        

//     imagefilledrectangle($img, 0, 0, 500, 500, $green);

//     imageline($img, 0, 0, 500, 500, $red);

//     imageline($img, 500, 0, 0, 500, $blue);

        

//     imagefilledellipse($img, 250, 250, 200, 200, $yellow);

        

//     imagefilledellipse($img, 200, 200, 300, 300, $blue);

        

//     imagejpeg($img, &#39;haha.jpg&#39;);

//     echo &quot;&lt;img src=&#39;haha.jpg&#39;&gt;&quot;;

//     imagedestroy($img);

?&gt;

    

&lt;!-- 开发验证码(生成验证码) --&gt;

&lt;?php

    check_code();

    function check_code($width = 100, $height = 50,

        $num = 4, $type = &#39;jpeg&#39;){

        $img = imagecreate($width, $height);

        $string = &#39;&#39;;

        for ($i = 0;$i &lt; $num; $i++){

            $rand = mt_rand(0, 2);

            switch ($rand) {

                case 0:

                    $ascii = mt_rand(48, 57);

                    break;

                case 1:

                    $ascii = mt_rand(65, 90);

                    break;

                case 2:

                    $ascii = mt_rand(97, 122);

                    break;

            }

            $string .= sprintf(&#39;%c&#39;, $ascii);

        }

        imagefilledrectangle($img, 0, 0, $width, $height, randBg($img));

        for ($i = 0;$i &lt; 50; $i++){

            imagesetpixel($img, mt_rand(0, $width), 

                mt_rand(0, $height), randPix($img));

        }

        for ($i = 0;$i &lt; $num;$i++){

            $x = floor($width/$num) * $i + 2;

            $y = mt_rand(0, $height - 15);

                

            imagechar($img, 5, $x, $y, $string[$i], randPix($img));

        }

            

        $func = &#39;image&#39; . $type;

        $header = &#39;Content-type:image/&#39;.$type;

        if (function_exists($func)) {

            header($header);

            $func($img);

        }else {

           echo &#39;图片类型不支持&#39;; 

        }

        imagedestroy($img);

        return $string;

    }

    function randBg($img){

        return imagecolorallocate($img, mt_rand(130, 255),

            mt_rand(130, 255), mt_rand(130, 255));

    }

    function randPix($img){

        return imagecolorallocate($img, mt_rand(0, 120), 

            mt_rand(0, 120), mt_rand(0, 120));

    }

?&gt; 

&lt;!-- 图像缩放和剪裁技术 --&gt;

&lt;?php

    $image = imagecreatefrompng(&#39;fbb.png&#39;);

        

    $percent = 0.1;

    list($width, $height) = getimagesize(&#39;fbb.png&#39;);

        

    $new_width = $width * $percent;

    $new_height = $height * $percent;

        

    $new_image = imagecreatetruecolor($new_width, $new_height);

    imagecopyresampled($new_image, $image, 0, 0,

        0, 0, $new_width, $new_height, $width, $height);

    header(&#39;content-type:image/jpeg&#39;);

    imagejpeg($new_image);

?&gt;

&lt;!-- 图片水印处理 --&gt;

&lt;?php

    $dst = imagecreatefrompng(&#39;https://img.php.cn/upload/course/000/

        000/002/5833ebba648cf229.png&#39;);

    $src = imagecreatefrompng(&#39;https://img.php.cn/

        upload/course/000/000/002/5833ebe90cc11285.png&#39;);

    $dst_info = getimagesize(&#39;5833ebba648cf229.png&#39;);

    $src_info = getimagesize(&#39;5833ebe90cc11285.png&#39;);

        

    $dst_x = $dst_info[0] - $src_info[0];

    $dst_y = $dst_info[1] - $src_info[1];

    imagecopymerge($dst, $src, $dst_x, $dst_y, 0, 0, 

        $src_info[0], $src_info[1], 100);

    header(&#39;Content-type:image/png&#39;);

    imagepng($dst);

    imagedestroy($dst);

    imagedestroy($src);

?&gt;  

&lt;!-- 做一个智能的图片水印函数 --&gt;

&lt;?php       

?&gt;

Copy after login

This article explains the related methods of image processing. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Thief program through cURL

php session management and control

php basic learning: error handling

The above is the detailed content of PHP basic learning: image processing. For more information, please follow other related articles on the PHP Chinese website!

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles