@@ -26,7 +26,7 @@ class Picture
2626
2727 // image types
2828 const IMAGE_BMP = 'bmp ' ;
29- const IMAGE_EBP = 'ebp ' ;
29+ // const IMAGE_EBP = 'ebp';
3030 const IMAGE_JPG = 'jpg ' ;
3131 const IMAGE_JPEG = 'jpeg ' ;
3232 const IMAGE_GIF = 'gif ' ;
@@ -205,14 +205,6 @@ protected function init()
205205 $ this ->waterAlpha = $ this ->waterOptions ['alpha ' ];
206206 $ this ->waterQuality = $ this ->waterOptions ['quality ' ];
207207
208- if ($ this ->waterOptions ['type ' ] === self ::WATER_IMAGE && !is_file ($ this ->waterOptions ['img ' ])) {
209- throw new InvalidConfigException ('请配置正确的水印图片资源路径 ' );
210- }
211-
212- if ($ this ->waterOptions ['type ' ] === self ::WATER_TEXT && !is_file ($ this ->waterOptions ['fontFile ' ])) {
213- throw new InvalidConfigException ('请配置正确的水印文字资源路径 ' );
214- }
215-
216208 $ this ->thumbType = $ this ->thumbOptions ['type ' ];
217209 $ this ->thumbPath = $ this ->thumbOptions ['path ' ];
218210 $ this ->thumbWidth = $ this ->thumbOptions ['width ' ];
@@ -237,46 +229,44 @@ protected function init()
237229 */
238230 public function watermark ($ img , $ outImg = '' , $ pos = '' , $ waterImg = '' , $ alpha = '' , $ text = '' )
239231 {
232+ $ imgType = pathinfo ($ img , PATHINFO_EXTENSION );
233+
240234 //验证原图像
241- if ( !$ this ->_checkImage ($ img ) ) {
235+ if ( !$ this ->_checkImage ($ img, $ imgType ) ) {
242236 return false ;
243237 }
244238
245- //验证水印图像
246- $ waterImg = $ waterImg ? : $ this ->waterImg ;
247- $ waterImgOn = $ this ->_checkImage ($ waterImg ) ? 1 : 0 ;
248-
249239 //判断另存图像
250240 $ outImg = $ outImg ? : $ img ;
251241 //水印位置
252242 $ pos = $ pos ? : $ this ->waterPos ;
253- //水印文字
254- $ text = $ text ? : $ this ->waterText ;
243+
255244 //水印透明度
256245 $ alpha = $ alpha ? : $ this ->waterAlpha ;
257246
258247 $ imgInfo = getimagesize ($ img );
259248 $ imgWidth = $ imgInfo [0 ];
260249 $ imgHeight = $ imgInfo [1 ];
261250
251+ //验证水印图像
252+ $ waterImg = $ waterImg ? : $ this ->waterImg ;
253+ $ waterImgType = pathinfo ($ waterImg , PATHINFO_EXTENSION );
254+
262255 //获得水印信息
263- if ($ waterImgOn ) {
264- $ waterInfo = getimagesize ($ waterImg );
265- $ waterWidth = $ waterInfo [0 ];
266- $ waterHeight = $ waterInfo [1 ];
256+ if ( $ waterImgOn = $ this ->_checkImage ($ waterImg , $ waterImgType ) ) {
257+ $ waterImgType = $ this ->_getImageType ($ imgType );
258+ $ waterInfo = getimagesize ($ waterImg );
259+ $ waterWidth = $ waterInfo [0 ];
260+ $ waterHeight = $ waterInfo [1 ];
261+ $ wImg = call_user_func ("imagecreatefrom {$ waterImgType }" , $ waterImg );
262+ } else {
263+ //水印文字
264+ $ text = $ text ?: $ this ->waterText ;
267265
268- switch ($ waterInfo [2 ]) {
269- case 1 :
270- $ wImg = imagecreatefromgif ($ waterImg );
271- break ;
272- case 2 :
273- $ wImg = imagecreatefromjpeg ($ waterImg );
274- break ;
275- case 3 :
276- $ wImg = imagecreatefrompng ($ waterImg );
277- break ;
266+ if ( !is_file ($ this ->waterOptions ['fontFile ' ]) ) {
267+ throw new InvalidConfigException ('请配置正确的水印文字资源路径 ' );
278268 }
279- } else {
269+
280270 if (!$ text || strlen ($ this ->waterOptions ['fontColor ' ]) !== 7 ) {
281271 return false ;
282272 }
@@ -291,18 +281,7 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
291281 return false ;
292282 }
293283
294- $ resImg = '' ;
295- switch ($ imgInfo [2 ]) {
296- case 1 :
297- $ resImg = imagecreatefromgif ($ img );
298- break ;
299- case 2 :
300- $ resImg = imagecreatefromjpeg ($ img );
301- break ;
302- case 3 :
303- $ resImg = imagecreatefrompng ($ img );
304- break ;
305- }
284+ $ resImg = call_user_func ("imagecreatefrom {$ imgType }" , $ img );
306285
307286 //水印位置处理方法
308287 switch ($ pos ) {
@@ -355,28 +334,22 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
355334 imagecopymerge ($ resImg , $ wImg , $ x , $ y , 0 , 0 , $ waterWidth , $ waterHeight , $ alpha );
356335 }
357336 } else {
358- $ r = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 1 , 2 ));
359- $ g = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 3 , 2 ));
360- $ b = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 5 , 2 ));
361- $ color = imagecolorallocate ($ resImg , $ r , $ g , $ b );
362- $ charset = 'UTF-8 ' ;
337+ $ r = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 1 , 2 ));
338+ $ g = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 3 , 2 ));
339+ $ b = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 5 , 2 ));
340+ $ color = imagecolorallocate ($ resImg , $ r , $ g , $ b );
341+ $ charset = 'UTF-8 ' ;
363342
364343 imagettftext (
365344 $ resImg , $ this ->waterOptions ['fontSize ' ], 0 , $ x , $ y ,
366345 $ color , $ this ->waterOptions ['fontFile ' ], iconv ($ charset , 'utf-8 ' , $ text )
367346 );
368347 }
369348
370- switch ($ imgInfo [2 ]) {
371- case 1 :
372- imagegif ($ resImg , $ outImg );
373- break ;
374- case 2 :
375- imagejpeg ($ resImg , $ outImg , $ this ->waterQuality );
376- break ;
377- case 3 :
378- imagepng ($ resImg , $ outImg );
379- break ;
349+ if ( $ imgType === IMAGE_JPEG ) {
350+ imagejpeg ($ resImg , $ outImg , $ this ->waterQuality );
351+ } else {
352+ call_user_func ("image {$ imgType }" , $ resThumb , $ outFile );
380353 }
381354
382355 if (isset ($ resImg )) {
@@ -411,7 +384,9 @@ public function thumb($img, $outFile = '', $path = '', $thumbWidth = '', $thumbH
411384 */
412385 public function thumbnail ($ img , $ outFile = '' , $ path = '' , $ thumbWidth = '' , $ thumbHeight = '' , $ thumbType = '' )
413386 {
414- if (!$ this ->_checkImage ($ img )) {
387+ $ imgType = pathinfo ($ img , PATHINFO_EXTENSION );
388+
389+ if (!$ this ->_checkImage ($ img , $ imgType )) {
415390 return false ;
416391 }
417392
@@ -425,14 +400,13 @@ public function thumbnail($img, $outFile = '', $path = '', $thumbWidth = '', $th
425400 $ imgInfo = getimagesize ($ img );
426401 $ imgWidth = $ imgInfo [0 ];
427402 $ imgHeight = $ imgInfo [1 ];
428- $ imgType = pathinfo ($ img , PATHINFO_EXTENSION );
429- $ imgType = $ this ->_handleImageType ($ imgType );
403+ $ imgType = $ this ->_getImageType ($ imgType );
430404
431405 //获得相关尺寸
432406 $ thumbSize = $ this ->calcThumbSize ($ imgWidth , $ imgHeight , $ thumbWidth , $ thumbHeight , $ thumbType );
433407
434408 //原始图像资源
435- // imagecreatefromgif() imagecreatefrompng() imagecreatefromjpeg() imagecreatefromwbmp() imagecreatefromwebp()
409+ // imagecreatefromgif() imagecreatefrompng() imagecreatefromjpeg() imagecreatefromwbmp()
436410 $ resImg = call_user_func ("imagecreatefrom {$ imgType }" , $ img );
437411
438412 //缩略图的资源
@@ -465,7 +439,7 @@ public function thumbnail($img, $outFile = '', $path = '', $thumbWidth = '', $th
465439
466440 Directory::create ($ uploadDir );
467441
468- // imagepng(), imagegif(), imagejpeg(), imagewbmp(), imagewebp()
442+ // imagepng(), imagegif(), imagejpeg(), imagewbmp()
469443 call_user_func ("image {$ imgType }" , $ resThumb , $ outFile );
470444
471445 if (isset ($ resImg )) {
@@ -486,23 +460,22 @@ public function thumbnail($img, $outFile = '', $path = '', $thumbWidth = '', $th
486460 /**
487461 * 环境验证
488462 * @param string $img 图像路径
463+ * @param string $imgType type of img e.g. jpg
489464 * @return bool
490465 */
491- private function _checkImage ($ img )
466+ private function _checkImage ($ img, $ imgType = '' )
492467 {
493- $ imgType = pathinfo ($ img , PATHINFO_EXTENSION );
468+ $ imgType = $ imgType ?: pathinfo ($ img , PATHINFO_EXTENSION );
494469
495470 return file_exists ($ img ) && in_array ($ imgType , static ::$ getImageTypes ());
496471 }
497472
498- private function _handleImageType ( $ type )
473+ private function _getImageType ( $ imgType )
499474 {
500475 if ( $ type === IMAGE_JPG ) {
501476 return IMAGE_JPEG ;
502477 } elseif ( $ type === IMAGE_BMP ) {
503478 return 'wbmp ' ;
504- } elseif ( $ type === IMAGE_EBP ) {
505- return 'webp ' ;
506479 }
507480
508481 return $ type ;
@@ -585,7 +558,6 @@ public static function getImageTypes()
585558 {
586559 return [
587560 self ::IMAGE_BMP ,
588- self ::IMAGE_EBP ,
589561 self ::IMAGE_JPEG ,
590562 self ::IMAGE_JPG ,
591563 self ::IMAGE_GIF ,
0 commit comments