PHP利用GD库处理图片方法实现

ganfanghua · · 174 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

添加水印(文字和图片)

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

<?php

/**

 * [创建图片文字水印]

 * @param  [string] $imagename [需要添加水印的值]

 * @param  [string] $string    [图片上添加的文字]

 * @param  [string] $locate    [水印位置,center,left_buttom,right_buttom三选一]

 * @return [null]            [description]

 */

function create_words_watermark($imagename,$string,$locate){

    list($width,$height,$type)=getimagesize($imagename);

    $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",

        4 => "SWF",5 => "PSD",6 => "BMP",

        7 => "TIFF",8 => "TIFF",9 => "JPC",

        10 => "JP2",11 => "JPX",12 => "JB2",

        13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");

    $type=strtolower($types[$type]);

    $create="imagecreatefrom".$type;

    $img=$create($imagename);

 

    $string_color=imagecolorallocate($img,200, 200, 200);

    $fontsize=4;

     

    // 图片的宽和高也可用下面两个函数获得

    // $width=imagesx($img);

    // $height=imagesy($img);

     

    switch($locate){

        case 'center':

            $x=($width-imagefontwidth($fontsize)*strlen($string))/2;

            $y=($height-imagefontheight($fontsize))/2;

            break;

        case 'left_buttom':

            $x=5;

            $y=($height-imagefontheight($fontsize)-3);

            break;

        case 'right_buttom':

            $x=($width-imagefontwidth($fontsize)*strlen($string)-3);

            $y=($height-imagefontheight($fontsize)-3);

            break;

        default:

            die("未指定水印位置!");

            break;

    }

     

    imagestring($img,$fontsize,$x,$y,$string,$string_color);

    imagestring($img,$fontsize,$x+1,$y+1,$string,$string_color);

 

    $save="image".$type;

    //保存

    //$save($img,"new_".$imagename);

     

    //显示

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

    $save($img);

    imagedestroy($img);

}

 

// create_words_watermark("test.png","hello world","right_buttom");

 

/**

 * [create_pic_watermark 添加图片水印]

 * @param  [string] $dest_image [需要添加图片水印的图片名]

 * @param  [string] $watermark  [水印图片名]

 * @param  [string] $locate     [水印位置,center,left_buttom,right_buttom三选一]

 * @return [type]             [description]

 */

function create_pic_watermark($dest_image,$watermark,$locate){

    list($dwidth,$dheight,$dtype)=getimagesize($dest_image);

    list($wwidth,$wheight,$wtype)=getimagesize($watermark);

 

    $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",

        4 => "SWF",5 => "PSD",6 => "BMP",

        7 => "TIFF",8 => "TIFF",9 => "JPC",

        10 => "JP2",11 => "JPX",12 => "JB2",

        13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");

 

    $dtype=strtolower($types[$dtype]);//原图类型

    $wtype=strtolower($types[$wtype]);//水印图片类型

 

    $created="imagecreatefrom".$dtype;

    $createw="imagecreatefrom".$wtype;

     

    $imgd=$created($dest_image);

    $imgw=$createw($watermark);

 

    switch($locate){

        case 'center':

            $x=($dwidth-$wwidth)/2;

            $y=($dheight-$wheight)/2;

            break;

        case 'left_buttom':

            $x=1;

            $y=($dheight-$wheight-2);

            break;

        case 'right_buttom':

            $x=($dwidth-$wwidth-1);

            $y=($dheight-$wheight-2);

            break;

        default:

            die("未指定水印位置!");

            break;

    }

 

    imagecopy($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight);

 

    $save="image".$dtype;

 

    //显示

    header("content-type:image/".$dtype);

    $save($imgd);

 

    imagedestroy($imgw);

    imagedestroy($imgd);

}

 

create_pic_watermark("ganlixin.jpg","test.png","left_buttom");

?>

 

剪切图片

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

2

<?php

/**

 * [cut_image]                  从原图中剪切一部分

 * @param  [string] $old_imagename [需要剪切的图片名]

 * @param  [int] $start_width   [从原图片宽为的$start_width开始剪切]

 * @param  [int] $start_height  [从原图片高为的$start_height开始剪切]

 * @param  [int] $new_width     [从原图片中剪切$new_width的宽度]

 * @param  [int] $new_height    [从原图片中剪切$new_height的高度]

 * @return [null]                [null]

 */

function cut_image($old_imagename,$start_width,$start_height,$new_width,$new_height){

    list($old_width,$old_height,$type)=getimagesize($old_imagename);

    $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",

        4 => "SWF",5 => "PSD",6 => "BMP",

        7 => "TIFF",8 => "TIFF",9 => "JPC",

        10 => "JP2",11 => "JPX",12 => "JB2",

        13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");

    $type=strtolower($types[$type]);

    $create="imagecreatefrom".$type;

 

    $old_img=$create($old_imagename);

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

 

    imagecopyresampled($new_img,$old_img,0,0,$start_width,$start_height,$new_width,$new_height,$new_width,$new_height);

    $save="image".$type;

 

    //保存

    //$save($new_img,"new_".$old_imagename);

     

    //显示

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

    $save($new_img);

 

    //销毁

    imagedestroy($old_img);

    imagedestroy($new_img);

}

 

cut_image("ganlixin.jpg",0,0,200,200);

?>

  

翻转图片

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

<?php

/**

 * [rotate_image 图片旋转]

 * @param  [string] $imagename [要进行旋转的图片名]

 * @param  [string] $angle     [旋转的角度,逆时针为正]

 * @return [null]            [description]

 */

function rotate_image($imagename,$angle){

    list($width,$height,$type)=getimagesize($imagename);

    $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",

        4 => "SWF",5 => "PSD",6 => "BMP",

        7 => "TIFF",8 => "TIFF",9 => "JPC",

        10 => "JP2",11 => "JPX",12 => "JB2",

        13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");

    $type=$types[$type];

 

    $create="imagecreatefrom".$type;

    $img=$create($imagename);

    $new_img=imagerotate($img,$angle,0);

    $save="image".$type;

 

    //显示

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

    $save($new_img);

 

    // 保存

    // $save($new_img,"new_".$imagename);

 

    imagedestroy($img);

    imagedestroy($new_img);

}

 

rotate_image("ganlixin.jpg",-90);

 

?>

  

翻转图片

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

<?php

/**

 * [overturn_image 翻转图片]

 * @param  [string] $imagename [要反转的图片名]

 * @param  [char] $method    [按x轴或y轴翻转,只有x,y选项]

 * @return [type]            [description]

 */

function overturn_image($imagename,$method){

    $method=strtolower($method);

    list($width,$height,$type)=getimagesize($imagename);

    $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",

        4 => "SWF",5 => "PSD",6 => "BMP",

        7 => "TIFF",8 => "TIFF",9 => "JPC",

        10 => "JP2",11 => "JPX",12 => "JB2",

        13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");

    $type=$types[$type];

 

    $create="imagecreatefrom".$type;

    $img=$create($imagename);//源图片

    $new_img=imagecreatetruecolor($width,$height);//翻转之后的图片

 

    if($method=='y'){

        for($i=0;$i<$width;$i++){

            imagecopy($new_img,$img,$width-$i-1,0,$i,0,1,$height);

        }

    } else if($method=='x'){

        for($i=0;$i<$height;$i++){

            imagecopy($new_img,$img,0,$height-$i-1,0,$i,$width,1);

        }

    }

    $save="image".$type;

 

    //显示

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

    $save($new_img);

 

    // 保存

    // $save($new_img,"new_".$imagename);

 

    imagedestroy($img);

    imagedestroy($new_img);

}

overturn_image("ganlixin.jpg","y");

?>

本文来自:开源中国博客

感谢作者:ganfanghua

查看原文:PHP利用GD库处理图片方法实现

174 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet