| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  | <?php | 
					
						
							|  |  |  |  | namespace app\service; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | use think\Image as TImage; | 
					
						
							|  |  |  |  | use app\model\System; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class Image extends File | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 对图片进行重置大小,并对宽度大于max宽度的等比缩放为宽度为1920 | 
					
						
							|  |  |  |  |      * milo | 
					
						
							|  |  |  |  |      * 2019-10-24修改 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public static function resize($src) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         $max = 1920; | 
					
						
							| 
									
										
										
										
											2024-11-20 16:24:45 +08:00
										 |  |  |  |         $realPath = app()->getRootPath() . 'public/' . ltrim($src,'/'); | 
					
						
							| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  |         if(is_file($realPath)){ | 
					
						
							|  |  |  |  |             $img = TImage::open($realPath); | 
					
						
							|  |  |  |  |             list($img_w,$img_h) = $img->size(); | 
					
						
							|  |  |  |  |             if($max > 0 && $img_w > $max){ | 
					
						
							|  |  |  |  |                 $img->thumb($max, $max * ($img_h / $img_w))->save($realPath); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 添加水印 | 
					
						
							|  |  |  |  |      * milo | 
					
						
							|  |  |  |  |      * 2018-01-17 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public static function mark($src) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         $rootPath = app()->getRootPath(); | 
					
						
							|  |  |  |  |         if(!empty($src)){ | 
					
						
							|  |  |  |  |             $system = System::getSystem(); | 
					
						
							| 
									
										
										
										
											2024-11-20 16:24:45 +08:00
										 |  |  |  |             $realPath = $rootPath . 'public/' . ltrim($src, '/'); | 
					
						
							| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  |             if(is_file($realPath)){ | 
					
						
							|  |  |  |  |                 if($system['is_mark']){ | 
					
						
							|  |  |  |  |                     $mark = $rootPath . ltrim($system['mark_img'], '/'); | 
					
						
							|  |  |  |  |                     if(is_file($mark)){ | 
					
						
							|  |  |  |  |                         $mark_position = $system['mark_position']??5; | 
					
						
							|  |  |  |  |                         $mark_opacity = $system['mark_opacity']??50; | 
					
						
							|  |  |  |  |                         $img = TImage::Open($realPath); | 
					
						
							|  |  |  |  |                         $img->water($mark,$mark_position,$mark_opacity)->save($realPath); | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-11-20 16:24:45 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  |     //获取水印位置键值对
 | 
					
						
							|  |  |  |  |     public static function getMarkPosition() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return [ | 
					
						
							|  |  |  |  |             "1" => '上左', | 
					
						
							|  |  |  |  |             "2" => '上中', | 
					
						
							|  |  |  |  |             "3" => '上右', | 
					
						
							|  |  |  |  |             "4" => '中左', | 
					
						
							|  |  |  |  |             "5" => '正中', | 
					
						
							|  |  |  |  |             "6" => '中右', | 
					
						
							|  |  |  |  |             "7" => '下左', | 
					
						
							|  |  |  |  |             "8" => '下中', | 
					
						
							|  |  |  |  |             "9" => '下右' | 
					
						
							|  |  |  |  |         ]; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 删除图片 | 
					
						
							|  |  |  |  |      * milo | 
					
						
							|  |  |  |  |      * 2018-01-15 | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public static function delImg($src) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if(!empty(trim($src))){ | 
					
						
							| 
									
										
										
										
											2024-11-20 16:24:45 +08:00
										 |  |  |  |             $realPath = app()->getRootPath() . 'public/' . ltrim($src, '/'); | 
					
						
							| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  |             if (file_exists($realPath)) { | 
					
						
							|  |  |  |  |                 $info = pathinfo($realPath); | 
					
						
							|  |  |  |  |                 $source = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '*.' . $info['extension']; | 
					
						
							|  |  |  |  |                 foreach(glob($source) as $filename){ | 
					
						
							|  |  |  |  |                     if(is_file($filename)){ | 
					
						
							|  |  |  |  |                         unlink($filename); | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                 clearstatcache();// 清除缓存
 | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     /** | 
					
						
							|  |  |  |  |      * 获取缩略图 | 
					
						
							|  |  |  |  |      * milo | 
					
						
							|  |  |  |  |      * 2019-10-24修改 | 
					
						
							|  |  |  |  |      * 避免跨平台出错,目录分隔符全部转换为'/' | 
					
						
							|  |  |  |  |      * app()->getRuntimePath() = app()->getRootPath().'runtime/当前应用模块(api)/' | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public static function getThumb($src,$width=0,$height=0,$type = TImage::THUMB_CENTER) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if(empty($src)){ | 
					
						
							|  |  |  |  |             return ''; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         $rootPath = app()->getRootPath(); | 
					
						
							| 
									
										
										
										
											2024-11-20 16:24:45 +08:00
										 |  |  |  |         $realPath = $rootPath . 'public/' . ltrim($src, '/'); | 
					
						
							| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  |         $realPath = str_replace('\\', '/', $realPath); | 
					
						
							|  |  |  |  |         if(!file_exists($realPath)){ | 
					
						
							|  |  |  |  |             return ''; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         $info = pathinfo($src); | 
					
						
							|  |  |  |  |         if($width <= 0 && $height <= 0){    //高宽都小于或等于0,则返回原图片
 | 
					
						
							|  |  |  |  |             return $src; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         $image = TImage::open($realPath); | 
					
						
							|  |  |  |  |         list($imageWidth, $imageHeight) = $image->size(); | 
					
						
							|  |  |  |  |         if($width <= 0){ | 
					
						
							|  |  |  |  |             $width = floor($height * ($imageWidth / $imageHeight)); | 
					
						
							|  |  |  |  |         }elseif($height <= 0){ | 
					
						
							|  |  |  |  |             $height = floor($width * ($imageHeight / $imageWidth)); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         if($width >= $imageWidth || $height >= $imageHeight){ | 
					
						
							|  |  |  |  |             return $src; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         $thumbName = $info['dirname']. DIRECTORY_SEPARATOR .$info['filename'].'_'.$width.'_'.$height.'.'.$info['extension']; | 
					
						
							| 
									
										
										
										
											2024-11-20 16:24:45 +08:00
										 |  |  |  |         $realThumbName = $rootPath . 'public/' . ltrim($thumbName, '/'); | 
					
						
							| 
									
										
										
										
											2022-09-16 11:44:40 +08:00
										 |  |  |  |         $realThumbName = str_replace('\\', '/', $realThumbName); | 
					
						
							|  |  |  |  |         if(!file_exists($realThumbName)){ | 
					
						
							|  |  |  |  |             $image = TImage::open($realPath); | 
					
						
							|  |  |  |  |             $image->thumb($width, $height, $type)->save($realThumbName); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         return str_replace('\\', '/', $thumbName); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |