Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit a258f4f

Browse files
author
xiajianjun
committed
complete logic
1 parent 81d5b67 commit a258f4f

File tree

1 file changed

+81
-53
lines changed

1 file changed

+81
-53
lines changed

src/files/Picture.php

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ class Picture
2424
# 2 文字水印
2525
const WATER_TEXT = 2;
2626

27+
// image types
28+
const IMAGE_BMP = 'bmp';
29+
const IMAGE_EBP = 'ebp';
30+
const IMAGE_JPG = 'jpg';
31+
const IMAGE_JPEG = 'jpeg';
32+
const IMAGE_GIF = 'gif';
33+
const IMAGE_PNG = 'png';
34+
2735
/**
2836
* 1 图片水印 2 文字水印
2937
* @var int
@@ -147,11 +155,6 @@ class Picture
147155
,'path' => '' # 缩略图存放路径
148156
];
149157

150-
/**
151-
* @var array
152-
*/
153-
protected static $types = ['.jpg', '.jpeg', '.png', '.gif', '.bmp'];
154-
155158
/**
156159
* @param array $waterOptions
157160
* @param array $thumbOptions
@@ -253,24 +256,24 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
253256
$alpha = $alpha ? : $this->waterAlpha;
254257

255258
$imgInfo = getimagesize($img);
256-
$imgWidth = $imgInfo [0];
257-
$imgHeight = $imgInfo [1];
259+
$imgWidth = $imgInfo[0];
260+
$imgHeight = $imgInfo[1];
258261

259262
//获得水印信息
260263
if ($waterImgOn) {
261264
$waterInfo = getimagesize($waterImg);
262-
$waterWidth = $waterInfo [0];
263-
$waterHeight = $waterInfo [1];
265+
$waterWidth = $waterInfo[0];
266+
$waterHeight = $waterInfo[1];
264267

265-
switch ($waterInfo [2]) {
268+
switch ($waterInfo[2]) {
266269
case 1 :
267-
$w_img = imagecreatefromgif($waterImg);
270+
$wImg = imagecreatefromgif($waterImg);
268271
break;
269272
case 2 :
270-
$w_img = imagecreatefromjpeg($waterImg);
273+
$wImg = imagecreatefromjpeg($waterImg);
271274
break;
272275
case 3 :
273-
$w_img = imagecreatefrompng($waterImg);
276+
$wImg = imagecreatefrompng($waterImg);
274277
break;
275278
}
276279
} else {
@@ -291,7 +294,7 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
291294
$resImg = '';
292295
switch ($imgInfo[2]) {
293296
case 1 :
294-
$resImg = imagecreatefromgif ($img);
297+
$resImg = imagecreatefromgif($img);
295298
break;
296299
case 2 :
297300
$resImg = imagecreatefromjpeg($img);
@@ -343,13 +346,13 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
343346
$y = mt_rand(25, $imgHeight - $waterHeight);
344347
}
345348

346-
if ($waterImgOn && isset($resImg) && isset($w_img)) {
349+
if ($waterImgOn && isset($resImg) && isset($wImg)) {
347350
$waterInfo = getimagesize($waterImg);
348351

349352
if ($waterInfo[2] === 3) {
350-
imagecopy($resImg, $w_img, $x, $y, 0, 0, $waterWidth, $waterHeight);
353+
imagecopy($resImg, $wImg, $x, $y, 0, 0, $waterWidth, $waterHeight);
351354
} else {
352-
imagecopymerge($resImg, $w_img, $x, $y, 0, 0, $waterWidth, $waterHeight, $alpha);
355+
imagecopymerge($resImg, $wImg, $x, $y, 0, 0, $waterWidth, $waterHeight, $alpha);
353356
}
354357
} else {
355358
$r = hexdec(substr($this->waterOptions['fontColor'], 1, 2));
@@ -380,8 +383,8 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
380383
imagedestroy($resImg);
381384
}
382385

383-
if (isset($res_thumb)) {
384-
imagedestroy($res_thumb);
386+
if (isset($resThumb)) {
387+
imagedestroy($resThumb);
385388
}
386389

387390
return true;
@@ -398,12 +401,12 @@ public function thumb($img, $outFile = '', $path = '', $thumbWidth = '', $thumbH
398401

399402
/**
400403
* 图片裁切处理(制作缩略图)
401-
* @param string| $img 操作的图片文件路径(原图)
402-
* @param string| $outFile 另存文件名
403-
* @param string| $path 文件存放路径
404-
* @param string| $thumbWidth 缩略图宽度
405-
* @param string| $thumbHeight 缩略图高度
406-
* @param string| $thumbType 裁切图片的方式
404+
* @param string $img 操作的图片文件路径(原图)
405+
* @param string $outFile 另存文件名
406+
* @param string $path 文件存放路径
407+
* @param string $thumbWidth 缩略图宽度
408+
* @param string $thumbHeight 缩略图高度
409+
* @param string $thumbType 裁切图片的方式
407410
* @return bool|string
408411
*/
409412
public function thumbnail($img, $outFile = '', $path = '', $thumbWidth = '', $thumbHeight = '', $thumbType = '')
@@ -419,58 +422,58 @@ public function thumbnail($img, $outFile = '', $path = '', $thumbWidth = '', $th
419422
$path = $path ? : $this->thumbOptions['path'];
420423

421424
//获得图像信息
422-
$imgInfo = getimagesize($img);
423-
$imgWidth = $imgInfo [0];
424-
$imgHeight = $imgInfo [1];
425-
$imgType = image_type_to_extension($imgInfo [2]);
425+
$imgInfo = getimagesize($img);
426+
$imgWidth = $imgInfo[0];
427+
$imgHeight = $imgInfo[1];
428+
$imgType = pathinfo($img, PATHINFO_EXTENSION);
429+
$imgType = $this->_handleImageType($imgType);
426430

427431
//获得相关尺寸
428432
$thumbSize = $this->calcThumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType);
429433

430434
//原始图像资源
431-
$func = 'imagecreatefrom' . substr($imgType, 1);
432-
$resImg = $func($img);
435+
// imagecreatefromgif() imagecreatefrompng() imagecreatefromjpeg() imagecreatefromwbmp() imagecreatefromwebp()
436+
$resImg = call_user_func("imagecreatefrom{$imgType}" , $img);
433437

434438
//缩略图的资源
435-
if ($imgType === '.gif') {
436-
$res_thumb = imagecreate($thumbSize[0], $thumbSize[1]);
437-
$color = imagecolorallocate($res_thumb, 255, 0, 0);
439+
if ($imgType === static::IMAGE_GIF) {
440+
$resThumb = imagecreate($thumbSize[0], $thumbSize[1]);
441+
$color = imagecolorallocate($resThumb, 255, 0, 0);
438442
} else {
439-
$res_thumb = imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
440-
imagealphablending($res_thumb, false); //关闭混色
441-
imagesavealpha($res_thumb, true); //储存透明通道
443+
$resThumb = imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
444+
imagealphablending($resThumb, false); //关闭混色
445+
imagesavealpha($resThumb, true); //储存透明通道
442446
}
443447

444448
//绘制缩略图X
445449
if (function_exists('imagecopyresampled')) {
446-
imagecopyresampled($res_thumb, $resImg, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $thumbSize[2], $thumbSize[3]);
450+
imagecopyresampled($resThumb, $resImg, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $thumbSize[2], $thumbSize[3]);
447451
} else {
448-
imagecopyresized($res_thumb, $resImg, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $thumbSize[2], $thumbSize[3]);
452+
imagecopyresized($resThumb, $resImg, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $thumbSize[2], $thumbSize[3]);
449453
}
450454

451455
//处理透明色
452-
if ($imgType === '.gif') {
453-
/** @var $color string */
454-
imagecolortransparent($res_thumb, $color);
456+
if ($imgType === static::IMAGE_GIF) {
457+
imagecolortransparent($resThumb, $color);
455458
}
456459

457460
//配置输出文件名
458-
$imgInfo = pathinfo($img);
459-
$outFile = $outFile ? : $this->thumbOptions['prefix'] . $imgInfo['filename'] . $this->thumbOptions['suffix'] . '.' . $imgInfo['extension'];
460-
$upload_dir = $path ? : dirname($img);
461+
$imgInfo = pathinfo($img);
462+
$outFile = $outFile ? : $this->thumbOptions['prefix'] . $imgInfo['filename'] . $this->thumbOptions['suffix'] . '.' . $imgInfo['extension'];
463+
$uploadDir = $path ? : dirname($img);
464+
$outFile = $uploadDir . DIRECTORY_SEPARATOR . $outFile;
461465

462-
Directory::create($upload_dir);
466+
Directory::create($uploadDir);
463467

464-
$outFile = $upload_dir . '/' . $outFile;
465-
$func = 'image' . substr($imgType, 1);
466-
$func($res_thumb, $outFile);
468+
// imagepng(), imagegif(), imagejpeg(), imagewbmp(), imagewebp()
469+
call_user_func("image{$imgType}", $resThumb, $outFile);
467470

468471
if (isset($resImg)) {
469472
imagedestroy($resImg);
470473
}
471474

472-
if (isset($res_thumb)) {
473-
imagedestroy($res_thumb);
475+
if (isset($resThumb)) {
476+
imagedestroy($resThumb);
474477
}
475478

476479
return $outFile;
@@ -487,9 +490,22 @@ public function thumbnail($img, $outFile = '', $path = '', $thumbWidth = '', $th
487490
*/
488491
private function _checkImage($img)
489492
{
490-
$imgType = strtolower(strrchr($img, '.'));
493+
$imgType = pathinfo($img, PATHINFO_EXTENSION);
491494

492-
return file_exists($img) && in_array($imgType, static::$types);
495+
return file_exists($img) && in_array($imgType, static::$getImageTypes());
496+
}
497+
498+
private function _handleImageType($type)
499+
{
500+
if ( $type === IMAGE_JPG ) {
501+
return IMAGE_JPEG;
502+
} elseif ( $type === IMAGE_BMP ) {
503+
return 'wbmp';
504+
} elseif ( $type === IMAGE_EBP ) {
505+
return 'webp';
506+
}
507+
508+
return $type;
493509
}
494510

495511
/**
@@ -565,6 +581,18 @@ private function calcThumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight,
565581
* getter/setter
566582
*********************************************************************************/
567583

584+
public static function getImageTypes()
585+
{
586+
return [
587+
self::IMAGE_BMP,
588+
self::IMAGE_EBP,
589+
self::IMAGE_JPEG,
590+
self::IMAGE_JPG,
591+
self::IMAGE_GIF,
592+
self::IMAGE_PNG,
593+
];
594+
}
595+
568596
/**
569597
* @param string $name
570598
* @param string $type water|cutting

0 commit comments

Comments
 (0)