@@ -42,6 +42,8 @@ class Picture
4242 'fontFile ' => '' ,
4343 // 水印图像
4444 'img ' => '' ,
45+ // 图像输出路径
46+ 'path ' => '' ,
4547 // 水印位置 1-9
4648 'pos ' => 1 ,
4749 // 水印透明度 transparency
@@ -79,6 +81,20 @@ class Picture
7981
8082 private $ _error = '' ;
8183
84+ /**
85+ * 正在操作的原文件
86+ * @var string
87+ */
88+ private $ _workingRawFile = '' ;
89+
90+ /**
91+ * 正在操作的输出文件
92+ * @var string
93+ */
94+ private $ _workingOutFile = '' ;
95+
96+ private $ _result = [];
97+
8298 /*********************************************************************************
8399 * build
84100 *********************************************************************************/
@@ -125,36 +141,34 @@ protected function init()
125141 /**
126142 * 水印处理
127143 * @param string $img 操作的图像
128- * @param string $outImg 另存的图像
144+ * @param string $outPath 另存的图像
129145 * @param string $pos 水印位置
130146 * @param string $waterImg 水印图片
131147 * @param string $alpha 透明度
132148 * @param string $text 文字水印内容
133149 * @return bool
134150 */
135- public function watermark ($ img , $ outImg = '' , $ pos = '' , $ waterImg = '' , $ alpha = '' , $ text = '' )
151+ public function watermark ($ img , $ outPath = '' , $ pos = '' , $ waterImg = '' , $ alpha = '' , $ text = '' )
136152 {
137- //验证原图像
138- if ( false === $ this ->_checkImage ($ img ) ) {
139- return false ;
153+ // 验证原图像 和 是否已有错误
154+ if ( false === $ this ->_checkImage ($ img ) || $ this -> hasError () ) {
155+ return $ this ;
140156 }
141157
142- $ imgInfo = pathinfo ($ img );
143- $ imgType = $ this ->_handleImageType ($ imgInfo ['extension ' ]);
144-
145- $ outImg = $ outImg ?: $ img ;
146- $ pos = $ pos ?: $ this ->waterOptions ['pos ' ];
147- $ alpha = $ alpha ?: $ this ->waterOptions ['alpha ' ];
158+ $ imgInfo = pathinfo ($ img );
159+ $ imgType = $ this ->_handleImageType ($ imgInfo ['extension ' ]);
160+ $ outPath = $ outPath ?: ( $ this -> waterOptions [ ' path ' ] ? : dirname ( $ img ) );
161+ $ pos = $ pos ?: $ this -> waterOptions [ ' pos ' ] ;
162+ $ alpha = $ alpha ?: $ this ->waterOptions ['alpha ' ];
163+ $ waterImg = $ waterImg ?: $ this ->waterOptions ['img ' ];
148164
149165 list ($ imgWidth , $ imgHeight ) = getimagesize ($ img );
150166
151- $ waterImg = $ waterImg ? : $ this ->waterOptions ['img ' ];
152-
153167 if ( $ waterImg ) {
154168
155169 // 验证水印图像
156170 if ( false === $ this ->_checkImage ($ waterImg ) ) {
157- return false ;
171+ return $ this ;
158172 }
159173
160174 $ waterImgType = $ this ->_handleImageType ( pathinfo ($ waterImg , PATHINFO_EXTENSION ) );
@@ -163,7 +177,7 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
163177 if ($ imgHeight < $ waterHeight || $ imgWidth < $ waterWidth ) {
164178 $ this ->_error = 'The image is too small. ' ;
165179
166- return false ;
180+ return $ this ;
167181 }
168182
169183 // create water image resource
@@ -177,59 +191,19 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
177191 }
178192
179193 if (!$ text || strlen ($ this ->waterOptions ['fontColor ' ]) !== 6 ) {
180- $ this ->_error = 'The watermark font color length must equal to 6. ' ;
181- return false ;
194+ throw new InvalidConfigException ('The watermark font color length must equal to 6. ' );
182195 }
183196
184- $ textInfo = imagettfbbox ($ this ->waterOptions ['fontSize ' ], 0 , $ this ->waterOptions ['fontFile ' ], $ text );
185- $ waterWidth = $ textInfo [2 ] - $ textInfo [6 ];
186- $ waterHeight = $ textInfo [3 ] - $ textInfo [7 ];
197+ $ textInfo = imagettfbbox ($ this ->waterOptions ['fontSize ' ], 0 , $ this ->waterOptions ['fontFile ' ], $ text );
198+ $ waterWidth = $ textInfo [2 ] - $ textInfo [6 ];
199+ $ waterHeight = $ textInfo [3 ] - $ textInfo [7 ];
187200 }
188201
189202 // create image resource 建立原图资源
190203 $ resImg = call_user_func ("imagecreatefrom {$ imgType }" , $ img );
191204
192- //水印位置处理方法
193- switch ($ pos ) {
194- case 1 :
195- $ x = $ y = 25 ;
196- break ;
197- case 2 :
198- $ x = ($ imgWidth - $ waterWidth ) / 2 ;
199- $ y = 25 ;
200- break ;
201- case 3 :
202- $ x = $ imgWidth - $ waterWidth ;
203- $ y = 25 ;
204- break ;
205- case 4 :
206- $ x = 25 ;
207- $ y = ($ imgHeight - $ waterHeight ) / 2 ;
208- break ;
209- case 5 :
210- $ x = ($ imgWidth - $ waterWidth ) / 2 ;
211- $ y = ($ imgHeight - $ waterHeight ) / 2 ;
212- break ;
213- case 6 :
214- $ x = $ imgWidth - $ waterWidth ;
215- $ y = ($ imgHeight - $ waterHeight ) / 2 ;
216- break ;
217- case 7 :
218- $ x = 25 ;
219- $ y = $ imgHeight - $ waterHeight ;
220- break ;
221- case 8 :
222- $ x = ($ imgWidth - $ waterWidth ) / 2 ;
223- $ y = $ imgHeight - $ waterHeight ;
224- break ;
225- case 9 :
226- $ x = $ imgWidth - $ waterWidth - 10 ;
227- $ y = $ imgHeight - $ waterHeight ;
228- break ;
229- default :
230- $ x = mt_rand (25 , $ imgWidth - $ waterWidth );
231- $ y = mt_rand (25 , $ imgHeight - $ waterHeight );
232- }
205+ //水印位置处理
206+ list ($ x , $ y ) = $ this ->_calcWaterCoords ($ pos , $ imgWidth , $ waterWidth , $ imgHeight , $ waterHeight );
233207
234208 if ($ waterImg && isset ($ waterImgType ) && isset ($ resWaterImg )) {
235209
@@ -240,9 +214,9 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
240214 imagecopymerge ($ resImg , $ resWaterImg , $ x , $ y , 0 , 0 , $ waterWidth , $ waterHeight , $ alpha );
241215 }
242216 } else {
243- $ r = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 1 , 2 ));
244- $ g = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 3 , 2 ));
245- $ b = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 5 , 2 ));
217+ $ r = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 0 , 2 ));
218+ $ g = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 2 , 2 ));
219+ $ b = hexdec (substr ($ this ->waterOptions ['fontColor ' ], 4 , 2 ));
246220 $ color = imagecolorallocate ($ resImg , $ r , $ g , $ b );
247221 $ charset = 'UTF-8 ' ;
248222
@@ -252,12 +226,19 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
252226 );
253227 }
254228
229+ if ( ! Directory::create ($ outPath ) ) {
230+ $ this ->_error = 'Failed to create the output directory path!. OUT-PATH: ' . $ outPath ;
231+ return $ this ;
232+ }
233+
234+ $ outFile = $ outPath . '/ ' . $ imgInfo ['basename ' ];
235+
255236 if ( $ imgType === self ::IMAGE_JPEG ) {
256- imagejpeg ($ resImg , $ outImg , $ this ->waterOptions ['quality ' ]);
237+ imagejpeg ($ resImg , $ outFile , $ this ->waterOptions ['quality ' ]);
257238 } elseif ( $ imgType === self ::IMAGE_PNG ) {
258- imagepng ($ resImg , $ outImg , ceil ($ this ->waterOptions ['quality ' ]/10 ));
239+ imagepng ($ resImg , $ outFile , ceil ($ this ->waterOptions ['quality ' ]/10 ));
259240 } else {
260- call_user_func ("image {$ imgType }" , $ resImg , $ outImg );
241+ call_user_func ("image {$ imgType }" , $ resImg , $ outFile );
261242 }
262243
263244 if (isset ($ resImg )) {
@@ -268,7 +249,13 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
268249 imagedestroy ($ resThumb );
269250 }
270251
271- return true ;
252+ $ this ->_workingRawFile = $ img ;
253+ $ this ->_workingOutFile = $ outFile ;
254+
255+ $ this ->_result ['rawFile ' ] = $ img ;
256+ $ this ->_result ['outFile ' ] = $ outFile ;
257+
258+ return $ this ;
272259 }
273260
274261 /*********************************************************************************
@@ -282,7 +269,7 @@ public function watermark($img, $outImg = '', $pos = '', $waterImg = '', $alpha
282269 * @param string $thumbWidth
283270 * @param string $thumbHeight
284271 * @param string $thumbType
285- * @return bool|string
272+ * @return static
286273 */
287274 public function thumb ($ img , $ outFile = '' , $ path = '' , $ thumbWidth = '' , $ thumbHeight = '' , $ thumbType = '' )
288275 {
@@ -297,12 +284,12 @@ public function thumb($img, $outFile = '', $path = '', $thumbWidth = '', $thumbH
297284 * @param string $thumbWidth 缩略图宽度
298285 * @param string $thumbHeight 缩略图高度
299286 * @param string $thumbType 裁切图片的方式
300- * @return bool|string
287+ * @return static
301288 */
302289 public function thumbnail ($ img , $ outFilename = '' , $ outPath = '' , $ thumbWidth = '' , $ thumbHeight = '' , $ thumbType = '' )
303290 {
304- if (!$ this ->_checkImage ($ img )) {
305- return false ;
291+ if (!$ this ->_checkImage ($ img ) || $ this -> hasError () ) {
292+ return $ this ;
306293 }
307294
308295 $ imgInfo = pathinfo ($ img );
@@ -312,7 +299,7 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
312299 $ thumbType = $ thumbType ? : $ this ->thumbOptions ['type ' ];
313300 $ thumbWidth = $ thumbWidth ? : $ this ->thumbOptions ['width ' ];
314301 $ thumbHeight = $ thumbHeight ? : $ this ->thumbOptions ['height ' ];
315- $ outPath = $ outPath ? : $ this ->thumbOptions ['path ' ];
302+ $ outPath = $ outPath ? : ( $ this ->thumbOptions ['path ' ] ? : dirname ( $ img ) ) ;
316303
317304 //获得图像信息
318305 list ($ imgWidth , $ imgHeight ) = getimagesize ($ img );
@@ -345,10 +332,12 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
345332
346333 //配置输出文件名
347334 $ outFilename = $ outFilename ?: $ this ->thumbOptions ['prefix ' ] . $ imgInfo ['filename ' ] . $ this ->thumbOptions ['suffix ' ] . '. ' . $ imgType ;
348- $ uploadDir = $ outPath ? : dirname ($ img );
349- $ outFile = $ uploadDir . DIRECTORY_SEPARATOR . $ outFilename ;
335+ $ outFile = $ outPath . DIRECTORY_SEPARATOR . $ outFilename ;
350336
351- Directory::create ($ uploadDir );
337+ if ( ! Directory::create ($ outPath ) ) {
338+ $ this ->_error = 'Failed to create the output directory path!. OUT-PATH: ' . $ outPath ;
339+ return $ this ;
340+ }
352341
353342 // generate image to dst file. imagepng(), imagegif(), imagejpeg(), imagewbmp()
354343 call_user_func ("image {$ imgType }" , $ resThumb , $ outFile );
@@ -361,7 +350,13 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
361350 imagedestroy ($ resThumb );
362351 }
363352
364- return $ outFile ;
353+ $ this ->_workingRawFile = $ img ;
354+ $ this ->_workingOutFile = $ outFile ;
355+
356+ $ this ->_result ['rawFile ' ] = $ img ;
357+ $ this ->_result ['outFile ' ] = $ outFile ;
358+
359+ return $ this ;
365360 }
366361
367362 /*********************************************************************************
@@ -397,6 +392,52 @@ private function _handleImageType($type)
397392 return $ type ;
398393 }
399394
395+ protected function _calcWaterCoords ($ pos , $ imgWidth , $ waterWidth , $ imgHeight , $ waterHeight )
396+ {
397+ switch ($ pos ) {
398+ case 1 :
399+ $ x = $ y = 25 ;
400+ break ;
401+ case 2 :
402+ $ x = ($ imgWidth - $ waterWidth ) / 2 ;
403+ $ y = 25 ;
404+ break ;
405+ case 3 :
406+ $ x = $ imgWidth - $ waterWidth ;
407+ $ y = 25 ;
408+ break ;
409+ case 4 :
410+ $ x = 25 ;
411+ $ y = ($ imgHeight - $ waterHeight ) / 2 ;
412+ break ;
413+ case 5 :
414+ $ x = ($ imgWidth - $ waterWidth ) / 2 ;
415+ $ y = ($ imgHeight - $ waterHeight ) / 2 ;
416+ break ;
417+ case 6 :
418+ $ x = $ imgWidth - $ waterWidth ;
419+ $ y = ($ imgHeight - $ waterHeight ) / 2 ;
420+ break ;
421+ case 7 :
422+ $ x = 25 ;
423+ $ y = $ imgHeight - $ waterHeight ;
424+ break ;
425+ case 8 :
426+ $ x = ($ imgWidth - $ waterWidth ) / 2 ;
427+ $ y = $ imgHeight - $ waterHeight ;
428+ break ;
429+ case 9 :
430+ $ x = $ imgWidth - $ waterWidth - 10 ;
431+ $ y = $ imgHeight - $ waterHeight ;
432+ break ;
433+ default :
434+ $ x = mt_rand (25 , $ imgWidth - $ waterWidth );
435+ $ y = mt_rand (25 , $ imgHeight - $ waterHeight );
436+ }
437+
438+ return [$ x , $ y ];
439+ }
440+
400441 /**
401442 *
402443 * 计算获得缩略图的尺寸信息
@@ -557,7 +598,7 @@ public function getThumbOption($name, $default = null)
557598 */
558599 public function hasError ()
559600 {
560- return $ this ->_error !== '' ;
601+ return $ this ->_error !== null ;
561602 }
562603
563604 /**
@@ -568,6 +609,15 @@ public function getError()
568609 return $ this ->_error ;
569610 }
570611
612+ public function getResult ()
613+ {
614+ return $ this ->_result ;
615+ }
616+
617+ /*********************************************************************************
618+ * other
619+ *********************************************************************************/
620+
571621 public function png2gif ($ pngImg , $ outPath = '' )
572622 {
573623 // Load the PNG
0 commit comments