目錄
常用的php图片处理类
首頁 php教程 php手册 常用的php图片处理类

常用的php图片处理类

Jun 13, 2016 am 09:00 AM
php 主要 圖片 處理 文章 匯總 用的 類別

常用的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

//PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类。

class Image_process{

public $source; //原图

public $source_width; //原图宽度

public $source_height; //原图高度

public $source_type_id;

public $orign_name;

public $orign_dirname;

 

//传入原图路径

public function __construct($source){

$this->typeList = array(1=>'gif',2=>'jpg',3=>'png');

$ginfo = getimagesize($source);

$this->source_width = $ginfo[0];

$this->source_height = $ginfo[1];

$this->source_type_id = $ginfo[2];

$this->orign_url = $source;

$this->orign_name = basename($source);

$this->orign_dirname = dirname($source);

}

 

//判断图片的文件的格式,返回PHP可识别的编码

public function judgeType($type,$source){

if($type == 1){

return imagecreatefromgif($source); //gif

}else if($type == 2){

return imagecreatefromjpeg($source); //jpg

}else if($type == 3){

return imagecreatefrompng($source); //png

}else{

return false;

}

}

 

//生成水印图片

public function waterMakeImage($logo){

$linfo = getimagesize($logo);

$logo_width = $linfo[0];

$logo_height = $linfo[1];

$logo_type_id = $linfo[2];

$sourceHandle = $this->judgeType($this->source_type_id,$this->orign_url);

$logoHandle = $this->judgeType($logo_type_id,$logo);

if(!$sourceHandle || !$logoHandle){

return false;

}

$x = ($this->source_width - $logo_width)/2;

$y = ($this->source_height - $logo_height)/2;

imagecopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_height);

$newPic = $this->orign_dirname.'\water_'.time().'.'.$this->typeList[$this->source_type_id];

if($this->saveImage($sourceHandle,$newPic)){

imagedestroy($sourceHandle);

imagedestroy($logoHandle);

}

}

 

//固定高度宽度

public function fixSizeImage($width,$height){

if($width > $this->source_width) $this->source_width;

if($height > $this->source_height) $this->source_height;

if($width === false){

$width = floor($this->source_width / ($this->source_height / $height));

}

if($height === false){

$height = floor($this->source_height / ($this->source_width / $width));

}

$this->tinyImage($width,$height);

}

 

//等比例缩放图片

public function scaleImage($scale){

$width = floor($this->source_width * $scale);

$height = floor($this->source_height * $scale);

$this->tinyImage($width, $height);

}

 

//创建缩略图

public function tinyImage($width,$height){

$tinyImage = imagecreatetruecolor($width,$height);

$handle = $this->judgeType($this->source_type_id,$this->orign_url);

if(function_exists('imagecopyresampled')){

imagecopyresampled($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height);

}else{

imagecopyresized($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height);

}

$newPic = $this->orign_dirname.'\thumb_'.time().'_'.$width."_".$height.".".$this->typeList[$this->source_type_id];

if($this->saveImage($tinyImage,$newPic)){

imagedestroy($tinyImage);

imagedestroy($handle);

}

}

//保存图片

private function saveImage($image,$url){

if(imagejpeg($image,$url)){

return true;

}

}

}

$imgHandle = new Image_process('D:\AppServ\www\test\getimg\14061907445601.jpg');

//$imgHandle->waterMakeImage('D:\AppServ\www\test\getimg\shougongke.png'); //生成水印图片

//$imgHandle->fixSizeImage(200,150); //固定长度图片

$imgHandle->scaleImage(0.2); //等比例缩放

?>

  示例二:

  ?

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

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

/**

*

* 图像处理类

* @author FC_LAMP

* @internal功能包含:水印,缩略图

*/

class Img

{

//图片格式

private $exts = array ('jpg', 'jpeg', 'gif', 'bmp', 'png' );

 

/**

*

*

* @throws Exception

*/

public function __construct()

{

if (! function_exists ( 'gd_info' ))

{

throw new Exception ( '加载GD库失败!' );

}

}

 

/**

*

* 裁剪压缩

* @param $src_img 图片

* @param $save_img 生成后的图片

* @param $option 参数选项,包括: $maxwidth 宽 $maxheight 高

* array('width'=>xx,'height'=>xxx)

* @internal

* 我们一般的压缩图片方法,在图片过长或过宽时生成的图片

* 都会被“压扁”,针对这个应采用先裁剪后按比例压缩的方法

*/

public function thumb_img($src_img, $save_img = '', $option)

{

 

if (empty ( $option ['width'] ) or empty ( $option ['height'] ))

{

return array ('flag' => False, 'msg' => '原图长度与宽度不能小于0' );

}

$org_ext = $this->is_img ( $src_img );

if (! $org_ext ['flag'])

{

return $org_ext;

}

 

//如果有保存路径,则确定路径是否正确

if (! empty ( $save_img ))

{

$f = $this->check_dir ( $save_img );

if (! $f ['flag'])

{

return $f;

}

}

 

//获取出相应的方法

$org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );

 

//获取原大小

$source = $org_funcs ['create_func'] ( $src_img );

$src_w = imagesx ( $source );

$src_h = imagesy ( $source );

 

//调整原始图像(保持图片原形状裁剪图像)

$dst_scale = $option ['height'] / $option ['width']; //目标图像长宽比

$src_scale = $src_h / $src_w; // 原图长宽比

if ($src_scale >= $dst_scale)

{ // 过高

$w = intval ( $src_w );

$h = intval ( $dst_scale * $w );

 

$x = 0;

$y = ($src_h - $h) / 3;

} else

{ // 过宽

$h = intval ( $src_h );

$w = intval ( $h / $dst_scale );

 

$x = ($src_w - $w) / 2;

$y = 0;

}

// 剪裁

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

imagecopy ( $croped, $source, 0, 0, $x, $y, $src_w, $src_h );

// 缩放

$scale = $option ['width'] / $w;

$target = imagecreatetruecolor ( $option ['width'], $option ['height'] );

$final_w = intval ( $w * $scale );

$final_h = intval ( $h * $scale );

imagecopyresampled ( $target, $croped, 0, 0, 0, 0, $final_w, $final_h, $w, $h );

imagedestroy ( $croped );

 

//输出(保存)图片

if (! empty ( $save_img ))

{

 

$org_funcs ['save_func'] ( $target, $save_img );

} else

{

header ( $org_funcs ['header'] );

$org_funcs ['save_func'] ( $target );

}

imagedestroy ( $target );

return array ('flag' => True, 'msg' => '' );

}

 

/**

*

* 等比例缩放图像

* @param $src_img 原图片

* @param $save_img 需要保存的地方

* @param $option 参数设置 array('width'=>xx,'height'=>xxx)

*

*/

function resize_image($src_img, $save_img = '', $option)

{

$org_ext = $this->is_img ( $src_img );

if (! $org_ext ['flag'])

{

return $org_ext;

}

 

//如果有保存路径,则确定路径是否正确

if (! empty ( $save_img ))

{

$f = $this->check_dir ( $save_img );

if (! $f ['flag'])

{

return $f;

}

}

 

//获取出相应的方法

$org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );

 

//获取原大小

$source = $org_funcs ['create_func'] ( $src_img );

$src_w = imagesx ( $source );

$src_h = imagesy ( $source );

 

if (($option ['width'] && $src_w > $option ['width']) || ($option ['height'] && $src_h > $option ['height']))

{

if ($option ['width'] && $src_w > $option ['width'])

{

$widthratio = $option ['width'] / $src_w;

$resizewidth_tag = true;

}

 

if ($option ['height'] && $src_h > $option ['height'])

{

$heightratio = $option ['height'] / $src_h;

$resizeheight_tag = true;

}

 

if ($resizewidth_tag && $resizeheight_tag)

{

if ($widthratio

$ratio = $widthratio;

else

$ratio = $heightratio;

}

 

if ($resizewidth_tag && ! $resizeheight_tag)

$ratio = $widthratio;

if ($resizeheight_tag && ! $resizewidth_tag)

$ratio = $heightratio;

 

$newwidth = $src_w * $ratio;

$newheight = $src_h * $ratio;

 

if (function_exists ( "imagecopyresampled" ))

{

$newim = imagecreatetruecolor ( $newwidth, $newheight );

imagecopyresampled ( $newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );

} else

{

$newim = imagecreate ( $newwidth, $newheight );

imagecopyresized ( $newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );

}

}

//输出(保存)图片

if (! empty ( $save_img ))

{

 

$org_funcs ['save_func'] ( $newim, $save_img );

} else

{

header ( $org_funcs ['header'] );

$org_funcs ['save_func'] ( $newim );

}

imagedestroy ( $newim );

return array ('flag' => True, 'msg' => '' );

}

 

/**

*

* 生成水印图片

* @param $org_img 原图像

* @param $mark_img 水印标记图像

* @param $save_img 当其目录不存在时,会试着创建目录

* @param array $option 为水印的一些基本设置包含:

* x:水印的水平位置,默认为减去水印图宽度后的值

* y:水印的垂直位置,默认为减去水印图高度后的值

* alpha:alpha值(控制透明度),默认为50

*/

public function water_mark($org_img, $mark_img, $save_img = '', $option = array())

{

//检查图片

$org_ext = $this->is_img ( $org_img );

if (! $org_ext ['flag'])

{

return $org_ext;

}

$mark_ext = $this->is_img ( $mark_img );

if (! $mark_ext ['flag'])

{

return $mark_ext;

}

//如果有保存路径,则确定路径是否正确

if (! empty ( $save_img ))

{

$f = $this->check_dir ( $save_img );

if (! $f ['flag'])

{

return $f;

}

}

 

//获取相应画布

$org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );

$org_img_im = $org_funcs ['create_func'] ( $org_img );

 

$mark_funcs = $this->get_img_funcs ( $mark_ext ['msg'] );

$mark_img_im = $mark_funcs ['create_func'] ( $mark_img );

 

//拷贝水印图片坐标

$mark_img_im_x = 0;

$mark_img_im_y = 0;

//拷贝水印图片高宽

$mark_img_w = imagesx ( $mark_img_im );

$mark_img_h = imagesy ( $mark_img_im );

 

$org_img_w = imagesx ( $org_img_im );

$org_img_h = imagesx ( $org_img_im );

 

//合成生成点坐标

$x = $org_img_w - $mark_img_w;

$org_img_im_x = isset ( $option ['x'] ) ? $option ['x'] : $x;

$org_img_im_x = ($org_img_im_x > $org_img_w or $org_img_im_x

$y = $org_img_h - $mark_img_h;

$org_img_im_y = isset ( $option ['y'] ) ? $option ['y'] : $y;

$org_img_im_y = ($org_img_im_y > $org_img_h or $org_img_im_y

 

//alpha

$alpha = isset ( $option ['alpha'] ) ? $option ['alpha'] : 50;

$alpha = ($alpha > 100 or $alpha

 

//合并图片

imagecopymerge ( $org_img_im, $mark_img_im, $org_img_im_x, $org_img_im_y, $mark_img_im_x, $mark_img_im_y, $mark_img_w, $mark_img_h, $alpha );

 

//输出(保存)图片

if (! empty ( $save_img ))

{

 

$org_funcs ['save_func'] ( $org_img_im, $save_img );

} else

{

header ( $org_funcs ['header'] );

$org_funcs ['save_func'] ( $org_img_im );

}

//销毁画布

imagedestroy ( $org_img_im );

imagedestroy ( $mark_img_im );

return array ('flag' => True, 'msg' => '' );

 

}

 

/**

*

* 检查图片

* @param unknown_type $img_path

* @return array('flag'=>true/false,'msg'=>ext/错误信息)

*/

private function is_img($img_path)

{

if (! file_exists ( $img_path ))

{

return array ('flag' => False, 'msg' => "加载图片 $img_path 失败!" );

}

$ext = explode ( '.', $img_path );

$ext = strtolower ( end ( $ext ) );

if (! in_array ( $ext, $this->exts ))

{

return array ('flag' => False, 'msg' => "图片 $img_path 格式不正确!" );

}

return array ('flag' => True, 'msg' => $ext );

}

 

/**

*

* 返回正确的图片函数

* @param unknown_type $ext

*/

private function get_img_funcs($ext)

{

//选择

switch ($ext)

{

case 'jpg' :

$header = 'Content-Type:image/jpeg';

$createfunc = 'imagecreatefromjpeg';

$savefunc = 'imagejpeg';

break;

case 'jpeg' :

$header = 'Content-Type:image/jpeg';

$createfunc = 'imagecreatefromjpeg';

$savefunc = 'imagejpeg';

break;

case 'gif' :

$header = 'Content-Type:image/gif';

$createfunc = 'imagecreatefromgif';

$savefunc = 'imagegif';

break;

case 'bmp' :

$header = 'Content-Type:image/bmp';

$createfunc = 'imagecreatefrombmp';

$savefunc = 'imagebmp';

break;

default :

$header = 'Content-Type:image/png';

$createfunc = 'imagecreatefrompng';

$savefunc = 'imagepng';

}

return array ('save_func' => $savefunc, 'create_func' => $createfunc, 'header' => $header );

}

 

/**

*

* 检查并试着创建目录

* @param $save_img

*/

private function check_dir($save_img)

{

$dir = dirname ( $save_img );

if (! is_dir ( $dir ))

{

if (! mkdir ( $dir, 0777, true ))

{

return array ('flag' => False, 'msg' => "图片保存目录 $dir 无法创建!" );

}

}

return array ('flag' => True, 'msg' => '' );

}

}

 

if (! empty ( $_FILES ['test'] ['tmp_name'] ))

{

//例子

$img = new Img ();

//原图

$name = explode ( '.', $_FILES ['test'] ['name'] );

$org_img = 'D:/test.' . end ( $name );

move_uploaded_file ( $_FILES ['test'] ['tmp_name'], $org_img );

$option = array ('width' => $_POST ['width'], 'height' => $_POST ['height'] );

if ($_POST ['type'] == 1)

{

$s = $img->resize_image ( $org_img, '', $option );

} else

{

$img->thumb_img ( $org_img, '', $option );

}

unlink ( $org_img );

}

  以上所述就是本文的全部内容了,希望大家能够喜欢。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

php程序在字符串中計數元音 php程序在字符串中計數元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

您如何在PHP中解析和處理HTML/XML? 您如何在PHP中解析和處理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例? 什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

See all articles