search
HomeBackend DevelopmentPHP TutorialPHP that supports png transparent images to generate thumbnail class sharing_PHP tutorial

Sharing of the php generated thumbnail class that supports png transparent images

This article mainly introduces the sharing of the php generated thumbnail class that supports png transparent images. The code of this article is based on the GD2 graphics library , support the generation of thumbnails from png transparent images, friends in need can refer to

Note: This function depends on the GD2 graphics library

I recently wanted to use PHP to generate thumbnails. I searched online and found this article: PHP generates image thumbnails

After trying it out, I found the following problems:

1. The thumbnails generated from png images are in jpg format

2. The thumbnail generated from the png image has no transparent (semi-transparent) effect (filled with black background)

3. The code syntax is relatively old

Therefore, we simply modified and optimized it based on this version.

PHP generate thumbnail class

?

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

/*

* desc: Resize Image(png, jpg, gif)

* author: Brother Lu ten years later

* date: 2014.11.13

*/

class ResizeImage {

//Picture type

private $type;

//Actual width

private $width;

//Actual height

private $height;

//Changed width

private $resize_width;

//Changed height

private $resize_height;

//Whether to crop the image

private $cut;

//Source image

private $srcimg;

//Target image address

private $dstimg;

//Temporarily created image

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

//Initialize image

$this->initi_img();

//Target image address

$this -> dst_img($savePath);

//--

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

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

//Generate image

$this->newimg();

ImageDestroy ($this->im);

}

private function newimg() {

//The proportion of the changed image

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

//Proportion of actual image

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

if($this->cut) {

//Crop image

$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 priority

imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this-> ;width)/$resize_ratio));

}

} else {

//No cropping

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

}

}

//Initialize 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 target 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 using it, just call the constructor of the class directly. The constructor is as follows:

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

Parameters
$imgPath: original image address

$width: thumbnail width

$height: Thumbnail height

$isCut: Whether to cut, 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", 320, 240, false, "img/test_320_240.jpg");

 

//png

$pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");

 

?>

1 2

3

4 5

6 7

89 10
include "ResizeImage.php"; //jpg $jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg");
//png $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png"); ?>
Effect http://www.bkjia.com/PHPjc/955276.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955276.htmlTechArticleSupport the php generation of thumbnail class sharing for png transparent images. This article mainly introduces the php generation that supports png transparent images. Thumbnail sharing, the code in this article is based on the GD2 graphics library, which supports png transparency...
Statement
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
如何解决Windows Hello不支持的摄像头问题如何解决Windows Hello不支持的摄像头问题Jan 05, 2024 pm 05:38 PM

使用windowshello中,找不到支持的摄像头,常见的原因是使用的摄像头不支持人脸识别、摄像头驱动安装不正确导致的,那么接下来让我们一起去看一下怎么去设置。windowshello找不到支持的摄像头教程:原因一:摄像头驱动安装不对1、一般来说Win10系统可以自动为大部分摄像头安装驱动程序,如下,插上摄像头之后会有通知;2、这时我们打开设备管理器看看,摄像头驱动是否安装好,没有的话就需要手动操作一下。WIN+X,然后选择设备管理器;3、设备管理器窗口中,展开照相机选项,会显示摄像头的驱动型号

PyCharm社区版支持的插件足够吗?PyCharm社区版支持的插件足够吗?Feb 20, 2024 pm 04:42 PM

PyCharm社区版支持的插件足够吗?需要具体代码示例随着Python语言在软件开发领域的应用越来越广泛,PyCharm作为一款专业的Python集成开发环境(IDE),备受开发者青睐。PyCharm分为专业版和社区版两个版本,其中社区版是免费提供的,但其插件支持相对专业版有所限制。那么问题来了,PyCharm社区版支持的插件足够吗?本文将通过具体的代码示例

优缺点分析:深入了解开源软件的利弊优缺点分析:深入了解开源软件的利弊Feb 23, 2024 pm 11:00 PM

开源软件的利与弊:了解开源项目的优劣势,需要具体代码示例在当今数字化时代,开源软件越来越受到关注和推崇。作为一种基于合作和分享精神的软件开发模式,开源软件在不同领域都有着广泛的应用。然而,尽管开源软件具有诸多优势,但也存在一些挑战和限制。本文将深入探讨开源软件的利与弊,并通过具体的代码示例展示开源项目的优劣势。一、开源软件的优势1.1开放性和透明性开源软件

华硕TUF Z790 Plus兼容华硕MCP79内存的频率华硕TUF Z790 Plus兼容华硕MCP79内存的频率Jan 03, 2024 pm 04:18 PM

华硕tufz790plus支持内存频率华硕TUFZ790-PLUS主板是一款高性能主板,支持双通道DDR4内存,最大支持64GB内存。它的内存频率非常强大,最高可达4800MHz。具体支持的内存频率包括2133MHz、2400MHz、2666MHz、2800MHz、3000MHz、3200MHz、3600MHz、3733MHz、3866MHz、4000MHz、4133MHz、4266MHz、4400MHz、4533MHz、4600MHz、4733MHz和4800MHz。无论是日常使用还是高性能需

GTX960与XP系统的兼容性及相关说明GTX960与XP系统的兼容性及相关说明Dec 28, 2023 pm 10:22 PM

有一些用户使用xp系统,想要将他们的显卡升级为gtx960,但不确定gtx960是否支持xp系统。实际上,gtx960是支持xp系统的。我们只需在官网下载适用于xp系统的驱动程序,就可以使用gtx960了。下面让我们一起来看看具体的步骤吧。gtx960支持xp系统吗:GTX960可以与XP系统兼容。只需要下载并安装驱动程序,你就可以开始使用了。首先,我们需要打开NVIDIA官网并导航到主页。然后,我们需要在页面上方找到一个标签或按钮,它可能会被标记为“驱动程序”。一旦找到了这个选项,我们需要点击

如何使用Flask-Babel实现多语言支持如何使用Flask-Babel实现多语言支持Aug 02, 2023 am 08:55 AM

如何使用Flask-Babel实现多语言支持引言:随着互联网的不断发展,多语言支持成为了大多数网站和应用的一个必要功能。Flask-Babel是一个方便易用的Flask扩展,它提供了基于Babel库的多语言支持。本文将介绍如何使用Flask-Babel来实现多语言支持,并附上代码示例。一、安装Flask-Babel在开始之前,我们需要先安装Flask-Bab

ios18支持哪几款机型ios18支持哪几款机型Jan 07, 2024 pm 01:21 PM

有可靠内部渠道传来的神秘消息,告诉人们iOS18将会带来一系列颠覆想象的重大更新,甚至计划推出震撼大众的潜在生成式人工智能!那么它支持的机型都有哪些呢?ios18支持哪几款机型答:ios18可能支持iPhone11及以上的机型。针对备受关注却仍旧保密甚严的iOS18系统,虽然目前披露的相关细节甚少,但根据传言指出,苹果正在投入大量资源研究人工智能服务与功能,并且预计最早将会在2024年底才能和大家见面。据相关消息称,苹果正在该领域内部自主研发AppleGPT,以对话式、图像生成、多模型为主打,是

找不到支持windows的指纹识别器怎么办找不到支持windows的指纹识别器怎么办Feb 27, 2024 am 08:30 AM

现在很多win10笔记本电脑带有指纹识别功能,可以通过指纹直接解锁设备,但由于生物设备驱动出现问题,导致找不到支持Windows的指纹识别器,今天小编帮助大家解决该问题。找不到支持windows的指纹识别器怎么办1、首先右击桌面上的“计算机”图标,在弹出的选项框中点击“管理”进入计算机管理页面,随后点击“设备管理器”。2、在展开列表中找到“生物识别设备”,将其展开后,能够发现故障的指纹识别器前有着感叹号警告。3、然后右击该设备,在弹出的窗口中点击“属性”,这时会提示“该设备无法启动”。4、接着右

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment