Home > Backend Development > PHP Tutorial > General image processing class implemented in PHP_PHP tutorial

General image processing class implemented in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:00:42
Original
835 people have browsed it

General image processing class implemented by PHP

This article mainly introduces the general image processing class implemented by PHP, which can realize zooming, cutting, photo frame, watermark, Functions such as sharpening, rotating, flipping, transparency, and inverting colors have certain reference value. Friends in need can refer to it

The example in this article describes the general image processing class implemented by PHP. Share it with everyone for your reference. The details are as follows:

The image processing function functions: zoom, cut, photo frame, watermark, sharpen, rotate, flip, transparency, invert color, the idea of ​​processing and saving historical records: automatically generate a new image when a picture is changed , the naming method can consider adding steps based on the original picture, for example: picture name __ what step. In some web applications that require advanced image processing functions, you can refer to this 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

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

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

class picture

{

var $PICTURE_URL;//Picture to be processed

var $DEST_URL="temp__01.jpg";//Generate target image location

var $PICTURE_CREATE;//Picture to be created

var $TURE_COLOR;//Create a new true color image

var $PICTURE_WIDTH; //Original picture width

var $PICTURE_HEIGHT;//original picture height

/*

The type of watermark, the default is watermark text

*/

var $MARK_TYPE=1;

var $WORD;//Text after UTF-8

var $WORD_X;//text abscissa

var $WORD_Y;//text ordinate

var $FONT_TYPE;//Font type

var $FONT_SIZE="12";//Font size

var $FONT_WORD;//text

var $ANGLE=0;//The angle of the text, the default is 0

var $FONT_COLOR="#000000";//Text color

var $FONT_PATH="font/simkai.ttf";//Font library, the default is Song Dynasty

var $FORCE_URL;//watermark image

var $FORCE_X=0; //watermark abscissa

var $FORCE_Y=0;//Watermark vertical coordinate

var $FORCE_START_X=0;//The abscissa of the picture where the watermark is cut

var $FORCE_START_Y=0;//The ordinate of the picture where the watermark is cut

var $PICTURE_TYPE;//Picture type

var $PICTURE_MIME;//Output header

/*

If the scaling ratio is 1, scale according to the scaling height and width

*/

var $ZOOM=1;//Zoom type

var $ZOOM_MULTIPLE; //Zoom ratio

var $ZOOM_WIDTH; //Zoom width

var $ZOOM_HEIGHT;//Zoom height

/*

Crop, proportional and fixed length and width

*/

var $CUT_TYPE=1;//Cut type

var $CUT_X=0;//abscissa coordinate of cropping

var $CUT_Y=0;//The vertical coordinate of cropping

var $CUT_;//Cut width

var $CUT_HEIGHT=100; //Cutting height

/*

Sharpening

*/

var $SHARP="7.0";//sharpening degree

/*

Transparency processing

*/

var $ALPHA='100'; //Transparency is between 0-127

var $ALPHA_X="90";

var $ALPHA_Y="50";

/*

Rotate at any angle

*/

var $CIRCUMROTATE="90.0";//Note, it must be a floating point number

/*

Error message

*/

var $ERROR=array(

'unalviable'=>'No related pictures found!'

);

/*

Constructor: Function initialization

*/

function __construct($PICTURE_URL)

{

$this->get_info($PICTURE_URL);

}

function get_info($PICTURE_URL)

{

/*

Process the information of the original image, first check whether the image exists, if not, the corresponding information will be given

*/

@$SIZE=getimagesize($PICTURE_URL);

if(!$SIZE)

{

exit($this->ERROR['unalviable']);

}

//Get the information type, width, and height of the original image

$this->PICTURE_MIME=$SIZE['mime'];

$this->PICTURE_;

$this->PICTURE_HEIGHT=$SIZE[1];

//Create pictures

switch($SIZE[2])

{

case 1:

$this->PICTURE_CREATE=imagecreatefromgif($PICTURE_URL);

$this->PICTURE_TYPE="imagejpeg";

$this->PICTURE_EXT="jpg";

break;

case 2:

$this->PICTURE_CREATE=imagecreatefromjpeg($PICTURE_URL);

$this->PICTURE_TYPE="imagegif";

$this->PICTURE_EXT="gif";

break;

case 3:

$this->PICTURE_CREATE=imagecreatefrompng($PICTURE_URL);

$this->PICTURE_TYPE="imagepng";

$this->PICTURE_EXT="png";

break;

}

/*

Convert text color from hexadecimal to decimal

*/

preg_match_all("/([0-f]){2,2}/i",$this->FONT_COLOR,$MATCHES);

if(count($MATCHES)==3)

{

$this->RED=hexdec($MATCHES[0][0]);

$this->GREEN=hexdec($MATCHES[0][1]);

$this->BLUE=hexdec($MATCHES[0][2]);

}

}

//end of __construct

/*

Convert hexadecimal color to decimal (R, G, B)

*/

function hex2dec()

{

preg_match_all("/([0-f]){2,2}/i",$this->FONT_COLOR,$MATCHES);

if(count($MATCHES)==3)

{

$this->RED=hexdec($MATCHES[0][0]);

$this->GREEN=hexdec($MATCHES[0][1]);

$this->BLUE=hexdec($MATCHES[0][2]);

}

}

//Zoom type

function zoom_type($ZOOM_TYPE)

{

$this->ZOOM=$ZOOM_TYPE;

}

//Scale the image, if the height and width are not specified, it will be scaled

function zoom()

{

//Zoom size

if($this->ZOOM==0)

{

$this->ZOOM_;gt;PICTURE_WIDTH * $this->ZOOM_MULTIPLE;

$this->ZOOM_HEIGHT=$this->PICTURE_HEIGHT * $this->ZOOM_MULTIPLE;

}

//Create a new true color image

$this->TRUE_COLOR=imagecreatetruecolor($this->ZOOM_WIDTH,$this->ZOOM_HEIGHT);

$WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,255);

imagefilledrectangle($this->TRUE_COLOR,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$WHITE);

imagecopyresized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$this->PICTURE_WIDTH,$this ->PICTURE_HEIGHT);

}

//end of zoom

//Crop the picture, according to coordinates or automatically

function cut()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->CUT_WIDTH,$this->CUT_WIDTH);

imagecopy($this->TRUE_COLOR,$this->PICTURE_CREATE, 0, 0, $this->CUT_X, $this->CUT_Y,$this->CUT_WIDTH,$this->CUT_HEIGHT );

}

//end of cut

/*

Put text or pictures on the picture

Watermark text

*/

function _mark_text()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

$this->WORD=mb_convert_encoding($this->FONT_WORD,'utf-8','gb2312');

/*

Get the range of text using TrueType font

*/

$TEMP = imagettfbbox($this->FONT_SIZE,0,$this->FONT_PATH,$this->WORD);

$WORD_LENGTH=strlen($this->WORD);

$WORD_WIDTH =$TEMP[2] - $TEMP[6];

$WORD_HEIGHT =$TEMP[3] - $TEMP[7];

/*

The default position of the text watermark is the lower right corner

*/

if($this->WORD_X=="")

{

$this->WORD_X=$this->PICTURE_WIDTH-$WORD_WIDTH;

}

if($this->WORD_Y=="")

{

$this->WORD_Y=$this->PICTURE_HEIGHT-$WORD_HEIGHT;

}

imagesettile($this->TRUE_COLOR,$this->PICTURE_CREATE);

imagefilledrectangle($this->TRUE_COLOR,0,0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT,IMG_COLOR_TILED);

$TEXT2=imagecolorallocate($this->TRUE_COLOR,$this->RED,$this->GREEN,$this->Blue);

imagettftext($this->TRUE_COLOR,$this->FONT_SIZE,$this->ANGLE,$this->WORD_X,$this->WORD_Y,$TEXT2,$this->FONT_PATH,$this->WORD);

}

/*

水印图片

*/

function _mark_picture()

{

/*

获取水印图片的信息

*/

@$SIZE=getimagesize($this->FORCE_URL);

if(!$SIZE)

{

exit($this->ERROR['unalviable']);

}

$FORCE_PICTURE_;

$FORCE_PICTURE_HEIGHT=$SIZE[1];

//创建水印图片

switch($SIZE[2])

{

case 1:

$FORCE_PICTURE_CREATE=imagecreatefromgif($this->FORCE_URL);

$FORCE_PICTURE_TYPE="gif";

break;

case 2:

$FORCE_PICTURE_CREATE=imagecreatefromjpeg($this->FORCE_URL);

$FORCE_PICTURE_TYPE="jpg";

break;

case 3:

$FORCE_PICTURE_CREATE=imagecreatefrompng($this->FORCE_URL);

$FORCE_PICTURE_TYPE="png";

break;

}

/*

判断水印图片的大小,并生成目标图片的大小,如果水印比图片大,则生成图片大小为水印图片的大小。否则生成的图片大小为原图片大小。

*/

$this->NEW_PICTURE=$this->PICTURE_CREATE;

if($FORCE_PICTURE_WIDTH>$this->PICTURE_WIDTH)

{

$CREATE_;gt;FORCE_START_X;

}

else

{

$CREATE_;gt;PICTURE_WIDTH;

}

if($FORCE_PICTURE_HEIGHT>$this->PICTURE_HEIGHT)

{

$CREATE_HEIGHT=$FORCE_PICTURE_HEIGHT-$this->FORCE_START_Y;

}

else

{

$CREATE_HEIGHT=$this->PICTURE_HEIGHT;

}

/*

创建一个画布

*/

$NEW_PICTURE_CREATE=imagecreatetruecolor($CREATE_WIDTH,$CREATE_HEIGHT);

$WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);

/*

将背景图拷贝到画布中

*/

imagecopy($NEW_PICTURE_CREATE, $this->PICTURE_CREATE, 0, 0, 0, 0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

/*

将目标图片拷贝到背景图片上

*/

imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this->FORCE_X, $this->FORCE_Y, $this->FORCE_START_X, $this->FORCE_START_Y,$FORCE_PICTURE_WIDTH,$FORCE_PICTURE_HEIGHT);

$this->TRUE_COLOR=$NEW_PICTURE_CREATE;

}

//end of mark

function alpha_()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

$rgb="#CDCDCD";

$tran_color="#000000";

for($j=0;$j<=$this->PICTURE_HEIGHT-1;$j )

{

for ($i=0;$i<=$this->PICTURE_WIDTH-1;$i )

{

$rgb = imagecolorat($this->PICTURE_CREATE,$i,$j);

$r = ($rgb >> 16) & 0xFF;

$g = ($rgb >> 8) & 0xFF;

$b = $rgb & 0xFF;

$now_color=imagecolorallocate($this->PICTURE_CREATE,$r,$g,$b);

if ($now_color==$tran_color)

{

continue;

}

else

{

$color=imagecolorallocatealpha($this->PICTURE_CREATE,$r,$g,$b,$ALPHA);

imagesetpixel($this->PICTURE_CREATE,$ALPHA_X $i,$ALPHA_Y $j,$color);

}

$this->TRUE_COLOR=$this->PICTURE_CREATE;

}

}

}

/*

图片旋转:

沿y轴旋转

*/

function turn_y()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

for ($x = 0; $x < $this->PICTURE_WIDTH; $x )

{

imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, $this->PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this->PICTURE_HEIGHT);

}

}

/*

沿X轴旋转

*/

function turn_x()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

for ($y = 0; $y < $this->PICTURE_HEIGHT; $y )

{

imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, 0, $this->PICTURE_HEIGHT - $y - 1, 0, $y, $this->PICTURE_WIDTH, 1);

}

}

/*

任意角度旋转

*/

function turn()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

imageCopyResized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

$WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,255);

$this->TRUE_COLOR=imagerotate ($this->TRUE_COLOR, $this->CIRCUMROTATE, $WHITE);

}

/*

图片锐化

*/

function sharp()

{

$this->TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

$cnt=0;

for ($x=0; $x<$this->PICTURE_WIDTH; $x )

{

for ($y=0; $y<$this->PICTURE_HEIGHT; $y )

{

$src_clr1 = imagecolorsforindex($this->TRUE_COLOR, imagecolorat($this->PICTURE_CREATE, $x-1, $y-1));

$src_clr2 = imagecolorsforindex($this->TRUE_COLOR, imagecolorat($this->PICTURE_CREATE, $x, $y));

$r = intval($src_clr2["red"] $this->SHARP*($src_clr2["red"]-$src_clr1["red"]));

$g = intval($src_clr2["green"] $this->SHARP*($src_clr2["green"]-$src_clr1["green"]));

$b = intval($src_clr2["blue"] $this->SHARP*($src_clr2["blue"]-$src_clr1["blue"]));

$r = min(255, max($r, 0));

$g = min(255, max($g, 0));

$b = min(255, max($b, 0));

if (($DST_CLR=imagecolorexact($this->PICTURE_CREATE, $r, $g, $b))==-1)

$DST_CLR = imagecolorallocate($this->PICTURE_CREATE, $r, $g, $b);

$cnt ;

if ($DST_CLR==-1) die("color allocate faile at $x, $y ($cnt).");

imagesetpixel($this->TRUE_COLOR, $x, $y, $DST_CLR);

}

}

}

/*

将图片反色处理??

*/

function return_color()

{

/*

创建一个画布

*/

$NEW_PICTURE_CREATE=imagecreate($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

$WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);

/*

将背景图拷贝到画布中

*/

imagecopy($NEW_PICTURE_CREATE, $this->PICTURE_CREATE, 0, 0, 0, 0,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);

$this->TRUE_COLOR=$NEW_PICTURE_CREATE;

}

/*

生成目标图片并显示

*/

function show()

{

// 判断浏览器,若是IE就不发送头

if(isset($_SERVER['HTTP_USER_AGENT']))

{

$ua = strtoupper($_SERVER['HTTP_USER_AGENT']);

if(!preg_match('/^.*MSIE.*)$/i',$ua))

{

header("Content-type:$this->PICTURE_MIME");

}

}

$OUT=$this->PICTURE_TYPE;

$OUT($this->TRUE_COLOR);

}

/*

生成目标图片并保存

*/

function save_picture()

{

// 以 JPEG 格式将图像输出到浏览器或文件

$OUT=$this->PICTURE_TYPE;

if(function_exists($OUT))

{

// 判断浏览器,若是IE就不发送头

if(isset($_SERVER['HTTP_USER_AGENT']))

{

$ua = strtoupper($_SERVER['HTTP_USER_AGENT']);

if(!preg_match('/^.*MSIE.*)$/i',$ua))

{

header("Content-type:$this->PICTURE_MIME");

}

}

if(!$this->TRUE_COLOR)

{

exit($this->ERROR['unavilable']);

}

else

{

$OUT($this->TRUE_COLOR,$this->DEST_URL);

$OUT($this->TRUE_COLOR);

}

}

}

/*

析构函数:释放图片

*/

function __destruct()

{

/*释放图片*/

imagedestroy($this->TRUE_COLOR);

imagedestroy($this->PICTURE_CREATE);

}

//end of class

}

?>

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/973120.htmlTechArticlephp实现的通用图片处理类 这篇文章主要介绍了php实现的通用图片处理类,可实现针对图片的缩放、剪切、相框、水印、锐化、旋转、翻转、...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template