99
1010namespace inhere \tools \files ;
1111
12+ use inhere \tools \exceptions \InvalidArgumentException ;
1213use inhere \tools \exceptions \InvalidConfigException ;
1314use inhere \tools \exceptions \ExtensionMissException ;
15+ use inhere \tools \exceptions \FileSystemException ;
1416
1517/**
1618 * Class Picture
@@ -67,8 +69,8 @@ class Picture
6769 'width ' => 150 ,
6870 // 缩略图高度
6971 'height ' => 100 ,
70- // 缩略图文件名前缀
71- 'prefix ' => 'thumb_ ' ,
72+ // 缩略图文件名前缀 thumb_
73+ 'prefix ' => '' ,
7274 // 缩略图文件名后缀
7375 'suffix ' => '' ,
7476 // 生成缩略图方式,
@@ -79,19 +81,16 @@ class Picture
7981 'path ' => ''
8082 ];
8183
82- private $ _error = '' ;
84+ private $ _error = null ;
8385
8486 /**
85- * 正在操作的原文件
87+ * 正在操作的文件记录
8688 * @var string
8789 */
88- private $ _workingRawFile = '' ;
89-
90- /**
91- * 正在操作的输出文件
92- * @var string
93- */
94- private $ _workingOutFile = '' ;
90+ public $ working = [
91+ 'raw ' => '' , // 正在操作的原文件
92+ 'out ' => '' , // 正在操作的输出文件
93+ ];
9594
9695 private $ _result = [];
9796
@@ -249,11 +248,13 @@ public function watermark($img, $outPath = '', $pos = '', $waterImg = '', $alpha
249248 imagedestroy ($ resThumb );
250249 }
251250
252- $ this ->_workingRawFile = $ img ;
253- $ this ->_workingOutFile = $ outFile ;
251+ $ this ->working = [
252+ 'raw ' => $ img ,
253+ 'out ' => $ outFile ,
254+ ];
254255
255- $ this ->_result ['rawFile ' ] = $ img ;
256- $ this ->_result ['outFile ' ] = $ outFile ;
256+ $ this ->_result ['workWater ' ][ ' rawFile ' ] = $ img ;
257+ $ this ->_result ['workWater ' ][ ' outFile ' ] = $ outFile ;
257258
258259 return $ this ;
259260 }
@@ -279,21 +280,21 @@ public function thumb($img, $outFile = '', $path = '', $thumbWidth = '', $thumbH
279280 /**
280281 * 图片裁切处理(制作缩略图)
281282 * @param string $img 操作的图片文件路径(原图)
282- * @param string $outFilename 另存文件名
283283 * @param string $outPath 文件存放路径
284+ * @param string $outFilename 另存文件名
284285 * @param string $thumbWidth 缩略图宽度
285286 * @param string $thumbHeight 缩略图高度
286287 * @param string $thumbType 裁切图片的方式
287288 * @return static
288289 */
289- public function thumbnail ($ img , $ outFilename = '' , $ outPath = '' , $ thumbWidth = '' , $ thumbHeight = '' , $ thumbType = '' )
290+ public function thumbnail ($ img , $ outPath = '' , $ outFilename = '' , $ thumbWidth = '' , $ thumbHeight = '' , $ thumbType = '' )
290291 {
291292 if (!$ this ->_checkImage ($ img ) || $ this ->hasError ()) {
292293 return $ this ;
293294 }
294295
295296 $ imgInfo = pathinfo ($ img );
296- $ imgType = $ imgInfo ['extension ' ];
297+ $ rawImgType = $ imgInfo ['extension ' ];
297298
298299 //基础配置
299300 $ thumbType = $ thumbType ? : $ this ->thumbOptions ['type ' ];
@@ -303,7 +304,7 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
303304
304305 //获得图像信息
305306 list ($ imgWidth , $ imgHeight ) = getimagesize ($ img );
306- $ imgType = $ this ->_handleImageType ($ imgType );
307+ $ imgType = $ this ->_handleImageType ($ rawImgType );
307308
308309 //获得相关尺寸
309310 $ thumbSize = $ this ->_calcThumbSize ($ imgWidth , $ imgHeight , $ thumbWidth , $ thumbHeight , $ thumbType );
@@ -331,7 +332,7 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
331332 }
332333
333334 //配置输出文件名
334- $ outFilename = $ outFilename ?: $ this ->thumbOptions ['prefix ' ] . $ imgInfo ['filename ' ] . $ this ->thumbOptions ['suffix ' ] . '. ' . $ imgType ;
335+ $ outFilename = $ outFilename ?: $ this ->thumbOptions ['prefix ' ] . $ imgInfo ['filename ' ] . $ this ->thumbOptions ['suffix ' ] . '. ' . $ rawImgType ;
335336 $ outFile = $ outPath . DIRECTORY_SEPARATOR . $ outFilename ;
336337
337338 if ( ! Directory::create ($ outPath ) ) {
@@ -350,15 +351,59 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
350351 imagedestroy ($ resThumb );
351352 }
352353
353- $ this ->_workingRawFile = $ img ;
354- $ this ->_workingOutFile = $ outFile ;
354+ $ this ->working = [
355+ 'raw ' => $ img ,
356+ 'out ' => $ outFile ,
357+ ];
355358
356- $ this ->_result ['rawFile ' ] = $ img ;
357- $ this ->_result ['outFile ' ] = $ outFile ;
359+ $ this ->_result ['workThumb ' ][ ' rawFile ' ] = $ img ;
360+ $ this ->_result ['workThumb ' ][ ' outFile ' ] = $ outFile ;
358361
359362 return $ this ;
360363 }
361364
365+ /**
366+ * 显示 image file 到浏览器
367+ * @param string $img 图片文件
368+ * @param string $type 图片格式 jpeg|png|gif
369+ * @return bool
370+ */
371+ public static function show ($ img )
372+ {
373+ if ( !is_file ($ img ) || !is_readable ($ img )) {
374+ throw new FileSystemException ('image file don \'t exists or file is not readable! ' );
375+ }
376+
377+ $ type = pathinfo ($ img , PATHINFO_EXTENSION );
378+ $ type = $ type === self ::IMAGE_JPG ? self ::IMAGE_JPEG : $ type ;
379+
380+ if ( !static ::isSupportedType ($ type ) ) {
381+ throw new InvalidArgumentException ("image type [ $ type] is not supported! " , 1 );
382+ }
383+
384+ /** @var resource $resImg */
385+ $ resImg = call_user_func ("imagecreatefrom {$ type }" , $ img );
386+
387+ // 保持png图片的透明度
388+ if ( $ type === self ::IMAGE_PNG ) {
389+ // 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息。
390+ imagesavealpha ($ resImg , true );
391+ }
392+
393+ header ('Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate ' );
394+ header ('Content-type: image/ ' .$ type .';charset=utf8 ' ); // 生成图片格式 png jpeg 。。。
395+
396+ ob_clean ();
397+ //生成图片,在浏览器中进行显示-格式 $type ,与上面的header声明对应
398+ // e.g. imagepng($resImg);
399+ $ success = call_user_func ("image {$ type }" , $ resImg );
400+
401+ // 已经显示图片后,可销毁,释放内存(可选)
402+ imagedestroy ($ resImg );
403+
404+ return $ success ;
405+ }
406+
362407 /*********************************************************************************
363408 * helper method
364409 *********************************************************************************/
0 commit comments