PHP generated thumbnail class to support PNG transparent image sharing _php Tutorial

Source: Internet
Author: User
Tags transparent image

PHP generated thumbnail class for PNG transparent image sharing


This article mainly describes the support of PNG transparent images of PHP generated thumbnail class sharing, the code based on the GD2 graphics library, to enable PNG transparent images to generate thumbnails, the need for friends can refer to the next

Note: This feature relies on the GD2 graphics library

Recently to use PHP to generate thumbnails, find a bit online, found this article: PHP generated thumbnail image

After a trial, we found that there were several problems:

1. PNG images generated thumbnails are in JPG format

2. PNG images generated thumbnails without transparent (translucent) effect (filled with black background)

3, the code syntax is relatively old

So, on the basis of this version of the simple modification is optimized a bit.

PHP generated thumbnail class

?

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

/*

* Desc:resize Image (PNG, JPG, GIF)

* Author: Ten years later, brother Lou

* date:2014.11.13

*/

Class Resizeimage {

Type of picture

Private $type;

Actual width

Private $width;

Actual height

Private $height;

The width after the change

Private $resize _width;

The height after the change

Private $resize _height;

Whether or not to cut the map

Private $cut;

Source image

Private $srcimg;

Destination image Address

Private $dstimg;

Temporarily created images

Private $im;

function __construct ($imgPath, $width, $height, $isCut, $savePath) {

$this->srcimg = $imgPath;

$this->resize_width = $width;

$this->resize_height = $height;

$this->cut = $isCut;

Type of picture

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

Initializing an image

$this->initi_img ();

Destination image Address

$this-Dst_img ($savePath);

//--

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

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

Creating images

$this->newimg ();

Imagedestroy ($this->im);

}

Private 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) {

Crop chart

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

if ($this->type== "png") {

Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));

}

if ($ratio >= $resize _ratio) {

High priority

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

} else {

Width first

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

}

} else {

Do not cut the map

if ($ratio >= $resize _ratio) {

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

if ($this->type== "png") {

Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));

}

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

} else {

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

if ($this->type== "png") {

Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));

}

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

}

}

if ($this->type== "png") {

Imagesavealpha ($newimg, true);

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

} else {

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

}

}

Initializing an image

Private 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

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

}

}

?>

Use

When used, the constructor of the class is called directly, and the constructor is as follows:

$resizeimage = new Resizeimage ($imgPath, $width, $height, $isCut, $savePath);

Parameters
$imgPath: Original image Address

$width: Thumbnail width

$height: High thumbnail image

$isCut: Whether clipping, bool value

$savePath: Thumbnail address (can be the same as the original image address)

Example

?

1

2

3

4

5

6

7

8

9

10

Include "resizeimage.php";

Jpg

$jpgResize = new Resizeimage ("Img/test_1920_1200.jpg", "img/test_320_240.jpg");

Png

$pngResize = new Resizeimage ("Img/test_1024_746.png", "img/test_320_240.png");

?>

Effect

http://www.bkjia.com/PHPjc/955276.html www.bkjia.com true http://www.bkjia.com/PHPjc/955276.html techarticle support PNG transparent image of PHP generated thumbnail class sharing this article mainly introduces the support of PNG transparent images of PHP generated thumbnail class sharing, this article code based on the GD2 Graphics library, the implementation of PNG transparency support ...

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