찾다
php教程php手册PHP批量生成图片缩略图的方法

PHP批量生成图片缩略图的方法

Jun 13, 2016 am 09:01 AM
php공유하다그림일괄방법생성하다~의

PHP批量生成图片缩略图的方法

 本文实例讲述了PHP批量生成图片缩略图的方法。分享给大家供大家参考。具体如下:

?

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

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

//用PHP批量生成图片缩略图

function mkdirs($dirname,$mode=0777)

//创建目录(目录, [模式])

{

if(!is_dir($dirname))

{

mkdirs($dirname,$mode); //如果目录不存在,递归建立

return mkdir($dirname,$mode);

}

return true;

}

function savefile($filename,$content='')

//保存文件(文件, [内容])

{

if(function_exists(file_put_contents))

{

file_put_contents($filename,$content);

}

else

{

$fp=fopen($filename,"wb");

fwrite($fp,$content);

fclose($fp);

}

}

function getsuffix($filename) //获取文件名后缀

{

return end(explode(".",$filename));

}

function checksuffix($filename,$arr) //是否为允许类型(当前, 允许)

{

if(!is_array($arr))

{

$arr=explode(",",str_replace(" ","",$arr));

}

return in_array($filename,$arr) ? 1 : 0;

}

class image

{

var $src; //源地址

var $newsrc; //新图路径(本地化后)

var $allowtype=array(".gif",".jpg",".png",".jpeg"); //允许的图片类型

var $regif=0; //是否缩略GIF, 为0不处理

var $keep=0; //是否保留源文件(1为保留, 0为MD5)

var $over=0; //是否可以覆盖已存在的图片,为0则不可覆盖

var $dir; //图片源目录

var $newdir; //处理后的目录

function __construct($olddir=null,$newdir=null)

{

$this->dir=$olddir ? $olddir : "./images/temp";

$this->newdir=$newdir ? $newdir : "./images/s";

}

function reNames($src)

{

$md5file=substr(md5($src),10,10).strrchr($src,".");

//MD5文件名后(例如:3293okoe.gif)

$md5file=$this->w."_".$this->h."_".$md5file;

//处理后文件名

return $this->newdir."/".$md5file;

//将源图片,MD5文件名后保存到新的目录里

}

function Mini($src,$w,$h,$q=80)

//生成缩略图 Mini(图片地址, 宽度, 高度, 质量)

{

$this->src=$src;

$this->w=$w;

$this->h=$h;

if(strrchr($src,".")==".gif" && $this->regif==0)

//是否处理GIF图

{

return $this->src;

}

if($this->keep==0) //是否保留源文件,默认不保留

{

$newsrc=$this->reNames($src); //改名后的文件地址

}

else //保持原名

{

$src=str_replace("\\","/",$src);

$newsrc=$this->newdir.strrchr($src,"/");

}

if(file_exists($newsrc) && $this->over==0)

//如果已存在,直接返回地址

{

return $newsrc;

}

if(strstr($src,"http://") && !strstr($src,$_SERVER['HTTP_HOST']))

//如果是网络文件,先保存

{

$src=$this->getimg($src);

}

$arr=getimagesize($src); //获取图片属性

$width=$arr[0];

$height=$arr[1];

$type=$arr[2];

switch($type)

{

case 1: //1 = GIF,

$im=imagecreatefromgif($src);

break;

case 2: //2 = JPG

$im=imagecreatefromjpeg($src);

break;

case 3: //3 = PNG

$im=imagecreatefrompng($src);

break;

default:

return 0;

}

//处理缩略图

$nim=imagecreatetruecolor($w,$h);

$k1=round($h/$w,2);

$k2=round($height/$width,2);

if($k1

{

$width_a=$width;

$height_a=round($width*$k1);

$sw=0;

$sh=($height-$height_a)/2;

}

else

{

$width_a=$height/$k1;

$height_a=$height;

$sw=($width-$width_a)/2;

$sh = 0;

}

//生成图片

if(function_exists(imagecopyresampled))

{

imagecopyresampled($nim,$im,0,0,$sw,$sh,$w,$h,$width_a,$height_a);

}

else

{

imagecopyresized($nim,$im,0,0,$sw,$sh,$w,$h,$width_a,$height_a);

}

if(!is_dir($this->newdir))

{

mkdir($this->newdir);

}

switch($type) //保存图片

{

case 1:

$rs=imagegif($nim,$newsrc);

break;

case 2:

$rs=imagejpeg($nim,$newsrc,$q);

break;

case 3:

$rs=imagepng($nim,$newsrc);

break;

default:

return 0;

}

return $newsrc; //返回处理后路径

}

function getimg($filename)

{

$md5file=$this->dir."/".substr(md5($filename),10,10).strrchr($filename,".");

if(file_exists($md5file))

{

return $md5file;

}

//开始获取文件,并返回新路径

$img=file_get_contents($filename);

if($img)

{

if(!is_dir($this->dir))

{

mkdir($this->dir);

}

savefile($md5file,$img);

return $md5file;

}

}

function reImg($src,$w,$h,$q)

//转换缩略图(文件名和结构不变)

{

$this->keep=1;

return $this->Mini($src,$w,$h,$q);

//return 生成的地址

}

}

$image=new image();

echo $image->reImg("images/zht.jpg",75,75,80);

echo "
";

echo $image->reImg("images/m8920.jpg",75,75,80);

echo "
";

echo $image->getimg("./images/s/zht.jpg");

?>

希望本文所述对大家的php程序设计有所帮助。

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구