search
HomeBackend DevelopmentPHP TutorialCode sharing for generating picture thumbnails with PHP based on GD2 graphics library_PHP tutorial

Sharing code for generating image thumbnails with PHP based on GD2 graphics library

This article mainly introduces sharing code for generating image thumbnails with PHP based on GD2 graphics library. This article directly The implementation code and usage methods are given, friends in need can refer to it

To use PHP to generate image thumbnails, make sure your PHP server has the GD2 graphics library installed. Use a class to generate image thumbnails

1. How to use

?

1

2

$resizeimage = new resizeimage("图片源文件地址", "200", "100", "0","缩略图地址");

//就只用上面的一句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高

1 2
$resizeimage = new resizeimage("Image source file address", "200", "100", "0","Thumbnail address"); // Just use the above sentence to generate a thumbnail. The source file and thumbnail addresses can be the same, 200 and 100 represent 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 class to generate image thumbnails,

class resizeimage

{

//Picture type

var $type;

//Actual width

var $width;

//Actual height

var $height;

//Changed width

var $resize_width;

//Changed height

var $resize_height;

//Whether to crop 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;

//Type of picture

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

//Initialize image

$this->initi_img();

//Target image address

$this -> dst_img($dstpath);

//--

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

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

//Generate image

$this->newimg();

ImageDestroy ($this->im);

}

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)=="1")

//裁图

{

if($ratio>=$resize_ratio)

//高度优先

{

$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

//宽度优先

{

$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

//不裁图

{

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

{

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

}

}

}

//初始化图象

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

}

}

//图象目标地址

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;

}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/955275.htmlTechArticle基于GD2图形库的PHP生成图片缩略图类代码分享 这篇文章主要介绍了基于GD2图形库的PHP生成图片缩略图类代码分享,本文直接给出实现代码和...
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
五大热门Go语言库汇总:开发必备利器五大热门Go语言库汇总:开发必备利器Feb 22, 2024 pm 02:33 PM

五大热门Go语言库汇总:开发必备利器,需要具体代码示例Go语言自从诞生以来,受到了广泛的关注和应用。作为一门新兴的高效、简洁的编程语言,Go的快速发展离不开丰富的开源库的支持。本文将介绍五大热门的Go语言库,这些库在Go开发中扮演了至关重要的角色,为开发者提供了强大的功能和便捷的开发体验。同时,为了更好地理解这些库的用途和功能,我们会结合具体的代码示例进行讲

学习Python,常用的这22个库怎能不掌握?学习Python,常用的这22个库怎能不掌握?Apr 12, 2023 am 10:25 AM

如今全球各个行业内 Python 的使用状况怎么样呢?这个问题就是我写这篇文章的初衷。我找出了22个最常用的 Python 包,希望能给你一些启发。首先我列出了最近一年内 PyPI 上下载量最高的 Python 包。我们来看看这些包的作用,它们的之间的关系,以及为什么会如此流行。1、Urllib38.93亿次下载Urllib3 是 Python 的 HTTP 客户端,它提供了许多 Python 标准库没有的功能。 线程安全 连接池 客户端 SSL/TLS 验证 使用 multipart 编码进行

轻松掌握Pillow库安装方法:指南分享轻松掌握Pillow库安装方法:指南分享Jan 17, 2024 am 08:56 AM

Pillow库是Python中一个非常强大的图像处理库,它基于PythonImagingLibrary(PIL)发展而来,并在其基础上进行了优化和扩展。Pillow库提供了丰富的图像处理功能,可以处理各种类型的图像文件,并进行图像的编辑、合并、滤镜处理等操作。本文将为大家提供一个Pillow库的安装指南,帮助你轻松掌握这个强大的图像处理工具。一、安装P

苹果M3 Ultra推出全新版本,新增32个CPU核心和80个GPU核心苹果M3 Ultra推出全新版本,新增32个CPU核心和80个GPU核心Nov 13, 2023 pm 11:13 PM

这款芯片可能会搭载高达80个GPU核心,进而成为M3系列中性能最强大的产品。Max两倍核心数量从M1与M2系列的发展模式来看,苹果的「Ultra」版芯片基本上是「Max」版本的两倍核心数量,这是因为苹果实际上将两颗Max芯片透过内部连接技术结合起来,形成了M1Ultra与M2Ultra。80个GPU核心M3Ultra可能拥有「高达80个图形处理核心」。这一预测基于苹果芯片的发展路径:从基础版到「Pro」版,再到图形核心数量翻倍的「Max」版,以及CPU和GPU核心都翻倍的「Ultra」版。举例来

PHP8.0中的国际化库PHP8.0中的国际化库May 14, 2023 pm 05:51 PM

PHP8.0中的国际化库:UnicodeCLDR和Intl扩展随着全球化的进程,开发跨语言、跨地域的应用程序变得越来越普遍。国际化是实现这一目标的重要组成部分。在PHP8.0中,引入了UnicodeCLDR和Intl扩展,这两个组件都为开发者提供了更好的国际化支持。UnicodeCLDRUnicodeCLDR(CommonLocaleDat

XML 解析的 Java 库对比:寻找最佳解决方案XML 解析的 Java 库对比:寻找最佳解决方案Mar 09, 2024 am 09:10 AM

简介XML(可扩展标记语言)是一种用于存储和传输数据的流行格式。在Java中解析XML是许多应用程序的一个必要任务,从数据交换到文档处理。为了有效地解析XML,开发人员可以使用各种Java库。本文将比较一些最流行的XML解析库,重点关注它们的特性、功能和性能,以帮助开发人员做出明智的选择。DOM(文档对象模型)解析库JavaXMLDOMAPI:由oracle提供的标准DOM实现。它提供了一个对象模型,允许开发人员访问和操作XML文档。DocumentBuilderFactoryfactory=D

PPT怎么组合两个图形PPT怎么组合两个图形Mar 20, 2024 pm 05:00 PM

大家好,今天我来给小伙伴们分享PPT怎么组合两个图形的具体操作步骤,大家按照这个步骤去做,一步一步就能学会了操作,以后就可以举一反三了,步骤详情就在下方,小伙伴们快来认真的看一看吧!1.首先,在电脑上打开一个PPT文档,然后新建一个PPT幻灯片,(如下图所示)。2.接着,在上方菜单栏项目【插入】中找到【形状】,并在形状的下拉框中选择需要导入的形状,(如下图红色圈出部分所示)。3.利用PPT的插入功能,依次将三角形和圆形两个形状插入至PPT中,并调整形状的大小和位置,(如下图红色箭头指向所示)。4

2020年amd显卡性能排名2020年amd显卡性能排名Jan 13, 2024 pm 08:54 PM

amd图形显卡排行1、Radeon需要重新写作的内容是:RX需要重新写作的内容是:6950XT2、Radeon需要重新写作的内容是:RX需要重新写作的内容是:6900XT需要重写的是:3、Radeon需要重新写作的内容是:RX需要重新写作的内容是:6800XT4、Radeon需要重新写作的内容是:RX需要重新写作的内容是:6800需要重写的是:5、Radeon需要重新写作的内容是:RX需要重新写作的内容是:6750XT6、Radeon需要重新写作的内容是:RX需要重新写作的内容是:6700XT7

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尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools