├── README.md └── imagefilter.php /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Image Filter是什么? 3 | 一个php处理图片裁剪,压缩,水印的小代码 4 | 5 | ## Image Filter有哪些功能? 6 | 7 | * 裁剪 8 | * 压缩 9 | * 水印(支持图片和文字水印) 10 | 11 | ## 有问题反馈 12 | 在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流 13 | 14 | * 邮件(byron1655#163.com, 把#换成@) 15 | * QQ: 710049654 16 | 17 | ## 示例 18 | ```php 19 | $job = array('scaling'=>['size'=>"300,500"], 20 | 'clipping'=>['position'=>'0,0', 'size'=>'150,50'], 21 | 'watermark'=>['mark'=>'logo.png','position'=>0] 22 | 'imagetext'=>['text'=>'ouropera.net','fontsize'=>'10','fontfamily'=>'msyh']); 23 | $image = new ImageFilter("1.jpg", $job, "1_1.jpg"); 24 | $image->outimage(); 25 | ``` 26 | -------------------------------------------------------------------------------- /imagefilter.php: -------------------------------------------------------------------------------- 1 | ['size'=>"300,500"], 5 | * 'clipping'=>['position'=>'0,0', 'size'=>'150,50'], 6 | * 'watermark'=>['mark'=>'logo.png','position'=>0] 7 | * 'imagetext'=>['text'=>'ouropera.net','fontsize'=>'10','fontfamily'=>'msyh']); 8 | * $image = new ImageFilter("1.jpg", $job, "1_1.jpg"); 9 | * $image->outimage(); 10 | * 样例:end 11 | * 12 | * @authors byron (www.ouropera.net) 13 | * @date 2015-11-04 11:54:18 14 | * @version 1.2 15 | * 16 | */ 17 | 18 | class ImageFilter { 19 | 20 | //使用的功能编号,1为图片缩放功能 2为图片裁剪功能 3,为图片加图片水印功能 21 | private $job_types = array('scaling' => 1, 'clipping' => 2, 'watermark' => 3, 'imagetext' => 4); 22 | private $marktext = 'www.ouropera.net'; 23 | private $fontfamily = array('arial' => 'arial.ttf', 'msyh' => 'msyh.ttc'); 24 | private $fontsize = 10; 25 | private $allowFileType = array('gif' => 1, 'jpg' => 2, 'jpeg' => 2, 'png' => 3); 26 | //1=GIF,2=JPG,3=PNG,4=SWF,5=PSD,6=BMP,7=TIFF(intel byte order),8=TIFF(motorola byte order),9=JPC,10=JP2,11=JPX,12=JB2,13=SWC,14=IFF,15=WBMP,16=XBM 27 | private $imgtype; //图片的格式 28 | private $image; //图片资源 29 | private $width; //图片宽度 30 | private $height; //图片高度 31 | private $job; //将要对图片进行处理的工作 32 | private $scaling_width; //缩放功能:宽度 33 | private $scaling_height; //缩放功能:高度 34 | private $position_x; //裁剪功能:x 35 | private $position_y; //裁剪功能:y 36 | private $clipping_width; //裁剪功能:宽度 37 | private $clipping_height; //裁剪功能:高度 38 | private $sourceaddress; 39 | private $endaddress; //输出后的地址+文件名 40 | private $suffix = '_copy'; //词尾后缀 41 | private $target = array(); 42 | 43 | /** 44 | * 构造函数 45 | * @param [type] $sourceaddress [description] 46 | * @param [type] $job [description] 47 | * @param string $endaddress [description] 48 | */ 49 | function __construct($sourceaddress, $job = null, $endaddress = "") { 50 | $this->sourceaddress = $sourceaddress; 51 | $this->image = $this->imagesources($sourceaddress); 52 | if (empty($job) && count($job) == 0) { 53 | print 'no image filter job to do[0]!'; 54 | exit; 55 | } 56 | $this->width = $this->imageWidth(); 57 | $this->height = $this->imageHeight(); 58 | $this->job = $job; 59 | $this->endaddress = $this->targetImage($endaddress); 60 | } 61 | 62 | function __destruct() { 63 | imagedestroy($this->image); 64 | } 65 | 66 | /** 67 | * 检测目标图像后缀 68 | * @param [string] $endaddress 目标地址+文件名 69 | * @return [type] 目标地址+文件名 70 | */ 71 | private function targetImage($endaddress = "") { 72 | if (empty($endaddress)) { 73 | $path = substr($this->sourceaddress, 0, strrpos($this->sourceaddress, '.')); 74 | $ext = substr($this->sourceaddress, strrpos($this->sourceaddress, '.') + 1); 75 | $endaddress = $path . $suffix . '.' . $ext; 76 | } else { 77 | $path = substr($endaddress, 0, strrpos($endaddress, '.')); 78 | $ext = substr($endaddress, strrpos($endaddress, '.') + 1); 79 | } 80 | 81 | $ext = strtolower($ext); 82 | 83 | if (array_key_exists($ext, $this->allowFileType)) { 84 | $this->target['path'] = $path; 85 | $this->target['ext'] = $ext; 86 | $this->target['type'] = $this->allowFileType[$ext]; 87 | } else { 88 | print 'Invalid file format upload attempt'; 89 | exit; 90 | } 91 | 92 | return $endaddress; 93 | 94 | } 95 | 96 | function outimage() { 97 | $i = 0; 98 | foreach ($this->job_types as $job_name => $job_level) { 99 | if (isset($this->job[$job_name])) { 100 | $this->image = $this->$job_name($this->job[$job_name]); 101 | $i++; 102 | } 103 | } 104 | if ($i > 0) { 105 | $this->output($this->image); 106 | } else { 107 | print 'no match image filter job to do!'; 108 | return false; 109 | } 110 | } 111 | 112 | /** 113 | * 图片裁剪功能 114 | * @param [obj] $args 参数对象 115 | * @return [obj] 返回图像对象 116 | */ 117 | private function clipping($args = null) { 118 | //将传进来的值分别赋给变量 119 | if (empty($args['position'])) { 120 | print 'no args:position'; 121 | exit; 122 | } 123 | if (empty($args['size'])) { 124 | print 'no args:size'; 125 | exit; 126 | } 127 | list($src_x, $src_y) = explode(",", $args['position']); 128 | list($dst_w, $dst_h) = explode(",", $args['size']); 129 | if ($this->width < $src_x + $dst_w || $this->height < $src_y + $dst_h) { 130 | //这个判断就是限制不能截取到图片外面去 131 | return false; 132 | } 133 | //创建新的画布资源 134 | $newimg = imagecreatetruecolor($dst_w, $dst_h); 135 | //进行裁剪 136 | imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h); 137 | //调用输出方法保存 138 | return $newimg; 139 | //$this->output($newimg); 140 | } 141 | 142 | /** 143 | * 图片缩放功能 144 | * @param [obj] $args 参数对象 145 | * @return [obj] 返回图像对象 146 | */ 147 | private function scaling($args = null) { 148 | if (empty($args['size'])) { 149 | print 'no args input!'; 150 | exit; 151 | } 152 | list($scaling_width, $scaling_height) = explode(',', $args['size']); 153 | 154 | $this->scaling_width = (int) $scaling_width; 155 | $this->scaling_height = (int) $scaling_height; 156 | 157 | //获取等比缩放的宽和高 158 | $this->proimagesize(); 159 | //根据参数进行缩放,并调用输出函数保存处理后的文件 160 | //$this->output($this->imagescaling()); 161 | return $this->imagescaling(); 162 | } 163 | 164 | /** 165 | * 获取图片类型并打开图像资源 166 | * @param [string] $imgad 图像地址 167 | * @return [type] [description] 168 | */ 169 | private function imagesources($imgad) { 170 | $imagearray = $this->getimagearr($imgad); 171 | switch ($imagearray[2]) { 172 | //1=GIF,2=JPG,3=PNG,4=SWF,5=PSD,6=BMP,7=TIFF(intel byte order),8=TIFF(motorola byte order),9=JPC,10=JP2,11=JPX,12=JB2,13=SWC,14=IFF,15=WBMP,16=XBM 173 | case 1: //gif 174 | $this->imgtype = 1; 175 | $img = imagecreatefromgif($imgad); 176 | break; 177 | case 2: //jpeg 178 | $this->imgtype = 2; 179 | $img = imagecreatefromjpeg($imgad); 180 | break; 181 | case 3: //png 182 | $this->imgtype = 3; 183 | $img = imagecreatefrompng($imgad); 184 | break; 185 | default: 186 | return false; 187 | } 188 | return $img; 189 | } 190 | 191 | /** 192 | * 加图片水印功能 193 | * @param [obj] $args 参数对象 194 | * @return [obj] 返回图像对象 195 | */ 196 | private function watermark($args = null) { 197 | //用函数获取水印文件的长和宽 198 | $mark_file = $args['mark']; 199 | $imagearrs = $this->getimagearr($mark_file); 200 | 201 | //调用函数计算出水印加载的位置 202 | $positionarr = $this->position($args['position'], $imagearrs[0], $imagearrs[1]); 203 | 204 | //加水印 205 | imagecopy($this->image, $this->imagesources($mark_file), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]); 206 | 207 | //调用输出方法保存 208 | return $this->image; 209 | //$this->output($this->image); 210 | } 211 | 212 | private function imagetext($args = null) { 213 | 214 | $text = empty($args['text']) ? $this->$marktext : $args['text']; 215 | $font = empty($args['fontfamily']) ? $this->fontfamily[0] : $this->fontfamily[$args['fontfamily']]; 216 | $fontsize = empty($args['fontsize']) ? $this->fontsize : (int) $args['fontsize']; 217 | 218 | $tb = imagettfbbox($fontsize, 0, $font, $text); 219 | // Create some colors 220 | $white = imagecolorallocate($this->image, 255, 255, 255); 221 | $grey = imagecolorallocate($this->image, 128, 128, 128); 222 | $black = imagecolorallocate($this->image, 0, 0, 0); 223 | // Add some shadow to the text 224 | imagettftext($this->image, $fontsize, 0, $this->imageWidth() - $tb[2] - 4, $this->imageHeight() - 5, $grey, $font, $text); 225 | 226 | // Add the text 227 | imagettftext($this->image, $fontsize, 0, $this->imageWidth() - $tb[2] - 5, $this->imageHeight() - 5, $white, $font, $text); 228 | //imagepng($this->image); 229 | return $this->image; 230 | } 231 | 232 | /** 233 | * 获得图片宽度 234 | * @return [type] [description] 235 | */ 236 | private function imageWidth() { 237 | return imagesx($this->image); 238 | } 239 | 240 | /** 241 | * 获取图片高度 242 | * @return [type] [description] 243 | */ 244 | private function imageHeight() { 245 | return imagesy($this->image); 246 | } 247 | 248 | /** 249 | * 计算等比缩放的图片的宽和高 250 | * @return [type] [description] 251 | */ 252 | private function proimagesize() { 253 | if ($this->scaling_height && ($this->width < $this->height)) { 254 | //等比缩放算法 255 | $this->scaling_width = round(($this->scaling_height / $this->height) * $this->width); 256 | } else { 257 | $this->scaling_height = round(($this->scaling_width / $this->width) * $this->height); 258 | } 259 | } 260 | 261 | /** 262 | * 图像缩放功能,返回处理后的图像资源 263 | * @return [type] [description] 264 | */ 265 | private function imagescaling() { 266 | $newimg = imagecreatetruecolor($this->scaling_width, $this->scaling_height); 267 | 268 | $tran = imagecolortransparent($this->image); //处理透明算法 269 | if ($tran >= 0 && $tran < imagecolorstotal($this->image)) { 270 | $tranarr = imagecolorsforindex($this->image, $tran); 271 | $newcolor = imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']); 272 | imagefill($newimg, 0, 0, $newcolor); 273 | imagecolortransparent($newimg, $newcolor); 274 | } 275 | 276 | imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->scaling_width, $this->scaling_height, $this->width, $this->height); 277 | return $newimg; 278 | } 279 | 280 | /** 281 | * 输出图像 282 | * @param [obj] $image 图像对象 283 | * @return [type] [description] 284 | */ 285 | private function output($image) { 286 | $targetImageType = $this->target['type']; 287 | switch ($targetImageType) { 288 | case 1: 289 | imagegif($image, $this->endaddress); 290 | break; 291 | case 2: 292 | imagejpeg($image, $this->endaddress, 95); 293 | break; 294 | case 3: 295 | imagepng($image, $this->endaddress); 296 | break; 297 | default: 298 | return false; 299 | } 300 | } 301 | 302 | /** 303 | * 返回图像属性数组方法 304 | * @param [type] $imagesou [description] 305 | * @return [type] [description] 306 | */ 307 | private function getimagearr($imagesou) { 308 | return getimagesize($imagesou); 309 | } 310 | 311 | /** 312 | * 根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高 313 | * @param [int] $num 位置 314 | * @param [int] $width 图像的宽 315 | * @param [int] $height 图像的高 316 | * @return [type] [description] 317 | */ 318 | private function position($num, $width, $height) { 319 | // 320 | switch ($num) { 321 | case 1: 322 | $positionarr[0] = 0; 323 | $positionarr[1] = 0; 324 | break; 325 | case 2: 326 | $positionarr[0] = ($this->width - $width) / 2; 327 | $positionarr[1] = 0; 328 | break; 329 | case 3: 330 | $positionarr[0] = $this->width - $width; 331 | $positionarr[1] = 0; 332 | break; 333 | case 4: 334 | $positionarr[0] = 0; 335 | $positionarr[1] = ($this->height - $height) / 2; 336 | break; 337 | case 5: 338 | $positionarr[0] = ($this->width - $width) / 2; 339 | $positionarr[1] = ($this->height - $height) / 2; 340 | break; 341 | case 6: 342 | $positionarr[0] = $this->width - $width; 343 | $positionarr[1] = ($this->height - $height) / 2; 344 | break; 345 | case 7: 346 | $positionarr[0] = 0; 347 | $positionarr[1] = $this->height - $height; 348 | break; 349 | case 8: 350 | $positionarr[0] = ($this->width - $width) / 2; 351 | $positionarr[1] = $this->height - $height; 352 | break; 353 | case 9: 354 | $positionarr[0] = $this->width - $width; 355 | $positionarr[1] = $this->height - $height; 356 | break; 357 | case 0: 358 | $positionarr[0] = rand(0, $this->width - $width); 359 | $positionarr[1] = rand(0, $this->height - $height); 360 | break; 361 | } 362 | return $positionarr; 363 | } 364 | 365 | } 366 | --------------------------------------------------------------------------------