自定义MVC框架之工具类-图像处理类

2018-06-22 05:36:20来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

截止目前已经改造了4个类:

ubuntu:通过封装验证码类库一步步安装php的gd扩展

自定义MVC框架之工具类-分页类的封装

自定义MVC框架之工具类-文件上传类

图像处理类:

1,图片加水印处理( 支持任意位置与九宫格位置 )

2,图片缩放处理( 等比缩放,不变形裁剪 )

  1 <?php
  2 
  3 class Image{
  4     //文件路径
  5     protected $path;
  6     //是否允许随机文件名称
  7     protected $enableRandName;
  8     //文件类型
  9     protected $type;
 10     //错误号
 11     protected $errorNo;
 12     //错误信息
 13     protected $errorInfo;
 14 
 15     public function __construct( $_path = './', $_enableRandName = true ){
 16         $this->path = $_path;
 17         $this->enableRandName = $_enableRandName;
 18     }
 19 
 20     public function __get( $key ) {
 21         if( $key == 'errorNo' ) {
 22             return $this->errorNo;
 23         }else if( $key == 'errorInfo' ) {
 24             return $this->getErrorInfo();
 25         }
 26     } 
 27     protected function getErrorInfo(){
 28         $info = '';
 29         switch( $this->errorNo ){
 30             case 1000:
 31                 $info = "水印或者图片源文件不存在";                
 32                 break;
 33             case 1001:
 34                 $info = '水印图片的尺寸(宽或者高)大于图片本身';
 35                 break;
 36         }
 37         return $info;
 38     }
 39 
 40     //获取图片信息
 41     public static function getImageInfo( $imgPath ){
 42         $data = getimagesize( $imgPath );
 43         $info['width'] = $data[0];
 44         $info['height'] = $data[1];
 45         $info['mime'] = $data['mime'];
 46         return $info;
 47     }
 48 
 49     /*
 50      *    @srcImg 原图片
 51      *    @waterImg 水印图片
 52      *    @pos 水印位置
 53      *    @alpha 透明度
 54      *    @prefix 水印处理之后的图片前缀
 55      * */
 56     public function waterMark( $srcImg, $waterImg, $pos, $alpha, $prefix = 'ghostwu_' ){
 57         //检查图片和水印是否存在
 58         if( !file_exists( $srcImg ) 
 59             || !file_exists( $waterImg ) ) {
 60             $this->errorNo = 1000;
 61             return false;
 62         } 
 63 
 64         //获取图片和水印信息
 65         $srcImageInfo = self::getImageInfo( $srcImg );
 66         $waterImageInfo = self::getImageInfo( $waterImg );
 67 
 68         $this->type = explode( '/', $srcImageInfo['mime'] )[1];
 69 
 70         //检查水印和图片的尺寸, 水印是否能加在图片上
 71         if( !$this->check( $srcImageInfo, $waterImageInfo ) ) {
 72             $this->errorNo = 1001;
 73             return false;
 74         }
 75 
 76         //打开图片文件和水印文件资源
 77         $imgRes = self::openImage( $srcImg );
 78         $waterRes = self::openImage( $waterImg );
 79 
 80         //计算水印图片位置    
 81         $waterPos = $this->calcPosition( $pos, $srcImageInfo, $waterImageInfo );
 82 
 83         //合成水印图片
 84         imagecopymerge ( $imgRes, $waterRes, $waterPos['x'], $waterPos['y'], 0, 0, $waterImageInfo['width'], $waterImageInfo['height'], $alpha );
 85 
 86         //得到文件的保存名字
 87         $saveName = $this->getFileName( $srcImg, $prefix );
 88 
 89         //拼凑完整的文件名称
 90         $fullPath = rtrim( $this->path, '/' ) . '/' . $saveName;
 91         
 92         //保存添加水印后的文件
 93         $this->saveImage( $imgRes, $fullPath );
 94 
 95         imagedestroy( $imgRes );
 96         imagedestroy( $waterRes );
 97 
 98         return $fullPath;
 99     }
100 
101     protected function saveImage( $imgRes, $fullPath ){
102         $func = 'image' . $this->type;
103         $func( $imgRes, $fullPath );
104     }
105 
106     protected function getFileName( $imgPath, $prefix ){
107         if ( $this->enableRandName ) {
108             $saveName = $prefix . uniqid() . '.' . $this->type;
109         }else {
110             $saveName = $prefix . pathinfo( $imgPath )['filename'] . '.' . $this->type;
111         }
112         return $saveName;
113     }
114 
115     protected function calcPosition( $pos, $imageInfo, $waterImageInfo ){
116         $x = 0; $y = 0;
117         switch( $pos ) {
118             case 1: //左上角
119                 $x = 0; $y = 0;
120                 break;
121             case 2: //上中
122                 $x = ( $imageInfo['width'] - $waterImageInfo['width'] ) / 2;
123                 $y = 0;
124                 break;
125             case 3: //右上角
126                 $x = $imageInfo['width'] - $waterImageInfo['width'];
127                 $y = 0;
128                 break;
129             case 4: //左边中间
130                 $x = 0;
131                 $y = ( $imageInfo['height'] - $waterImageInfo['height'] ) / 2;
132                 break;
133             case 5: //正中间
134                 $x = ( $imageInfo['width'] - $waterImageInfo['width'] ) / 2;
135                 $y = ( $imageInfo['height'] - $waterImageInfo['height'] ) / 2;
136                 break;
137             case 6: //右边中间
138                 $x = $imageInfo['width'] - $waterImageInfo['width'];
139                 $y = ( $imageInfo['height'] - $waterImageInfo['height'] ) / 2;
140                 break;
141             case 7: //左下角
142                 $x = 0;
143                 $y = $imageInfo['height'] - $waterImageInfo['height'];
144                 break;
145             case 8: //中间下面
146                 $x = ( $imageInfo['width'] - $waterImageInfo['width'] ) / 2;
147                 $y = $imageInfo['height'] - $waterInfo['height'];
148                 break;
149             case 9: //右下角
150                 $x = $imageInfo['width'] - $waterImageInfo['width'];
151                 $y = $imageInfo['height'] - $waterImageInfo['height'];
152                 break;
153             case 0: //随机位置
154                 $x = mt_rand( 0, $imageInfo['width'] - $waterImageInfo['width'] );
155                 $y = mt_rand( 0, $imageInfo['height'] - $waterImageInfo['height'] );
156                 break;
157         }
158         return [ 'x' => $x, 'y' => $y ];
159     }
160 
161     public static function openImage( $imagePath ){
162         $mime = self::getImageInfo( $imagePath )['mime'];
163         switch( $mime ){
164             case 'image/gif':
165                 $im = imagecreatefromgif( $imagePath );
166                 break;
167             case 'image/jpeg':
168                 $im = imagecreatefromjpeg( $imagePath );
169                 break;
170             case 'image/png':
171                 $im = imagecreatefrompng( $imagePath );
172                 break;
173             case 'image/wbmp':
174                 $im = imagecreatefromwbmp( $imagePath );
175                 break;
176         }
177         return $im;
178     }
179 
180     public function check( $imgInfo, $waterInfo ){
181         if( $imgInfo['width'] < $waterInfo['width']
182            || $imgInfo['height'] < $waterInfo['height'] ){
183             return false;
184         }
185         return true;
186     }
187 
188     /*
189      * @srcImg 原图片路径
190      * @dstImg 目标图片路径
191      * @newWidth 缩放后新的图片宽度
192      * @newHeight 缩放后新的图片高度
193      * */
194     public function thumb( $srcImg, $newWidth, $newHeight, $prefix="thumb_" ){
195         $srcIm = self::openImage( $srcImg );
196         $srcInfo = self::getImageInfo( $srcImg );
197         $srcW = $srcInfo['width'];
198         $srcH = $srcInfo['height'];
199         $this->type = explode( '/', $srcInfo['mime'] )[1];
200         $ratio_w = $newWidth / $srcW;
201         $ratio_h = $newHeight / $srcH;
202         $ratio = 1.0;
203 
204         //缩放后的图片 宽度/高度 都比原图大或者小
205         if( ( $ratio_w < 1 && $ratio_h < 1 ) || ( $ratio_w > 1 && $ratio_h > 1 ) ) {
206             if( $ratio_w < $ratio_h ) {
207                 $ratio = $ratio_h;
208             }else {
209                 $ratio = $ratio_w;
210             }
211             $tmp_w = (int)( $newWidth / $ratio );
212             $tmp_h = (int)( $newHeight / $ratio );
213             $tmp_im = imagecreatetruecolor( $tmp_w, $tmp_h );
214             //生成一个以原图片最大边长,比例为ratio的中间图像
215             imagecopy( $tmp_im, $srcIm, 0, 0, 0, 0, $tmp_w, $tmp_h );
216             //定义一个目标尺寸的图像
217             $newIm = imagecreatetruecolor( $newWidth, $newHeight );
218             imagecopyresampled( $newIm, $tmp_im, 0, 0, 0, 0, $newWidth, $newHeight, $tmp_w, $tmp_h );
219         }else {
220         //缩放后的图片 宽度/高度 一个比原图大, 另一个比原图小
221             $ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; //取比例大的那个值
222 
223             $tmp_w=(int)($srcW * $ratio);
224             $tmp_h=(int)($srcH * $ratio);
225             $tmp_im = imagecreatetruecolor($tmp_w, $tmp_h);
226 
227             imagecopyresampled($tmp_im, $srcIm, 0, 0, 0, 0, $tmp_w, $tmp_h, $srcW, $srcH );
228 
229             $newIm = imagecreatetruecolor($newWidth, $newHeight);
230             imagecopy($newIm, $tmp_im, 0, 0, 0, 0, $newWidth, $newHeight);
231         }
232         //处理后的文件的保存名字
233         $saveName = $this->getFileName( $newIm, $prefix );
234 
235         //拼凑完整的文件名称
236         $fullPath = rtrim( $this->path, '/' ) . '/' . $saveName;
237         
238 
239         $this->saveImage( $newIm, $fullPath );
240 
241         imagedestroy( $newIm );
242 
243         return $fullPath;
244     }
245 }
246 
247 $img = new Image();
248 //$img->waterMark( '1.jpg', 'logo.png', 5, 30 );
249 echo $img->thumb( '1.jpg', 330, 495 );
250 
251 
252 ?>
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:[日常] PHP设置 include_path 配置选项

下一篇:自定义MVC框架之工具类-模型类