PHP generated image based on GD2 graphics library thumbnail class code sharing _php Tutorial

Source: Internet
Author: User
Tags imagejpeg php server

PHP generated image thumbnail class code sharing based on GD2 graphics library


This article mainly introduces the PHP generated image based on the GD2 graphics library thumbnail class code sharing, this article directly give the implementation code and use method, the need for friends can refer to the next

To use PHP to generate image thumbnails, make sure your PHP server has a GD2 graphics library that uses a class to generate thumbnails of pictures

1. How to use

?

1

2

$resizeimage = new Resizeimage ("Image source file Address", "200", "100", "0", "thumbnail address");

Just use the above words, you can generate thumbnails, where the source file and thumbnail address can be the same, the 200,100 represents the width and height

2. Thumbnail Class Code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

118

119

Use the following class to generate a picture thumbnail,

Class Resizeimage

{

Type of picture

var $type;

Actual width

var $width;

Actual height

var $height;

The width after the change

var $resize _width;

The height after the change

var $resize _height;

Whether or not to cut the map

var $cut;

Source image

var $srcimg;

Destination image Address

var $dstimg;

Temporarily created images

var $im;

function Resizeimage ($img, $wid, $hei, $c, $dstpath)

{

$this->srcimg = $img;

$this->resize_width = $wid;

$this->resize_height = $hei;

$this->cut = $c;

Type of picture

$this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1);

Initializing an image

$this->initi_img ();

Destination image Address

$this-Dst_img ($dstpath);

//--

$this->width = imagesx ($this->im);

$this->height = Imagesy ($this->im);

Creating images

$this->newimg ();

Imagedestroy ($this->im);

}

function newimg ()

{

The proportions of the altered image

$resize _ratio = ($this->resize_width)/($this->resize_height);

Ratio of actual images

$ratio = ($this->width)/($this->height);

if (($this->cut) = = "1")

Crop chart

{

if ($ratio >= $resize _ratio)

High priority

{

$newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);

Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this Height) * $resize _ratio), $this->height);

Imagejpeg ($newimg, $this->dstimg);

}

if ($ratio < $resize _ratio)

Width first

{

$newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);

Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width , (($this->width)/$resize _ratio));

Imagejpeg ($newimg, $this->dstimg);

}

}

Else

Do not cut the map

{

if ($ratio >= $resize _ratio)

{

$newimg = Imagecreatetruecolor ($this->resize_width, ($this->resize_width)/$ratio);

Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this- >width, $this->height);

Imagejpeg ($newimg, $this->dstimg);

}

if ($ratio < $resize _ratio)

{

$newimg = Imagecreatetruecolor (($this->resize_height) * $ratio, $this->resize_height);

Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $ This->width, $this->height);

Imagejpeg ($newimg, $this->dstimg);

}

}

}

Initializing an image

function initi_img ()

{

if ($this->type== "jpg")

{

$this->im = imagecreatefromjpeg ($this->srcimg);

}

if ($this->type== "gif")

{

$this->im = imagecreatefromgif ($this->srcimg);

}

if ($this->type== "PNG")

{

$this->im = imagecreatefrompng ($this->srcimg);

}

}

Image Destination Address

function Dst_img ($dstpath)

{

$full _length = strlen ($this->srcimg);

$type _length = strlen ($this->type);

$name _length = $full _length-$type _length;

$name = substr ($this->srcimg,0, $name _length-1);

$this->dstimg = $dstpath;

Echo $this->dstimg;

}

}

http://www.bkjia.com/PHPjc/955275.html www.bkjia.com true http://www.bkjia.com/PHPjc/955275.html techarticle PHP generated images based on GD2 graphics library thumbnail class Code sharing this article mainly introduces the PHP generated image based on the GD2 graphics library thumbnail class code sharing, this article directly gives the implementation code and ...

  • 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.