Code sharing for generating image thumbnails in PHP based on GD2 graphics library _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
Code sharing for generating image thumbnails based on PHP of GD2 graphics library. Code sharing for generating image thumbnails using PHP in GD2 graphics library this article mainly introduces code sharing for generating image thumbnails using PHP in GD2 graphics library, this article provides implementation code and code sharing for generating image thumbnails based on PHP of GD2 graphics library.

This article mainly introduces how to share the code for generating image thumbnails using PHP based on the GD2 graphics library. This article provides the implementation code and usage instructions. For more information, see

To use PHP to generate image thumbnails, make sure that the GD2 graphics library is installed on your PHP server to generate image thumbnails using a class.

1. usage

?

1

2

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

// A thumbnail can be generated with only one sentence above. the source file and the thumbnail address can be the same, and 200,100 represents the width and height respectively.

2. thumbnail code

?

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

118

119

// Use the following classes to generate image thumbnails,

Class resizeimage

{

// Image type

Var $ type;

// Actual width

Var $ width;

// Actual height

Var $ height;

// Change the width

Var $ resize_width;

// The height after the change

Var $ resize_height;

// Whether to cut the image

Var $ cut;

// Source image

Var $ srcimg;

// Target Image address

Var $ dstimg;

// Temporarily created image

Var $ im;

Function resizeimage ($ img, $ wid, $ hei, $ c, $ dstpath)

{

$ This-> srcimg = $ img;

$ This-> resize_width = $ wid;

$ This-> resize_height = $ hei;

$ This-> cut = $ c;

// Image type

$ This-> type = strtolower (substr (strrchr ($ this-> srcimg, "."), 1 ));

// Initialize the image

$ This-> initi_img ();

// Target Image address

$ This-> dst_img ($ dstpath );

//--

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

$ This-> height = imagesy ($ this-> im );

// Generate an image

$ This-> newimg ();

ImageDestroy ($ this-> im );

}

Function newimg ()

{

// Ratio of the changed image

$ Resize_ratio = ($ this-> resize_width)/($ this-> resize_height );

// Ratio of the actual image

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

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

// Cut the image

{

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)

// The width is preferred.

{

$ 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

// No cut Chart

{

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 );

}

}

}

// Initialize the 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 );

}

}

// Target Image 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;

}

}

This article mainly introduces how to share the code of generating image thumbnails using PHP based on the GD2 graphics library. This article provides 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.