《璇》

图片压缩类

因为主流的编程思想是:OOP面向对象编程,文件的最新单位是类

思考:

有哪些属性:

原图文件

压缩文件保存的地址

 

有哪些成员方法:

制作压缩图像的方法



<?php /* * 图像压缩 */ class Thumb { //成员属性 private $file; //原图文件 private $thumb_path; //压缩文本件保存的地址 public function __set($p,$v) { if(property_exists($this, $p)){ $this->$p = $v; } } public function __get($p) { if(property_exists($this, $p)){ return $this->$p; } } //参数1:压缩的范围宽度 //参数2:压缩的范围的高度 function makeThumb($area_w,$area_h) { //参数2:原图资源(将该图片资源压缩之后,再保存到目的地画布中) $src_image = imagecreatefrompng($this->file); //参数3、4:目的地(画布的起点坐标) $dst_x = 0; $dst_y = 0; //参数5、6:原图的(起点坐标) $src_x = 0; $src_y = 0; //参数9、10:原图的宽度、高度 //通过imagesx()函数获得图像资源的宽度、imagesy()获得图像资源的高度 $src_w = imagesx($src_image); $src_h = imagesy($src_image); //参数7、8:目的地(画布的宽度、高度) //计算压缩的比例 if($src_w / $area_w >= $src_h / $area_h){ $scale = $src_w / $area_w; }else{ $scale = $src_h / $area_h; } $dst_w = (int)$src_w / $scale; $dst_h = (int)$src_h / $scale; //参数1:目的地图像资源(通常指的是画布资源) $dst_image = imagecreatetruecolor($dst_w, $dst_h); $color = imagecolorallocate($dst_image, 22, 139, 0); imagefill($dst_image, 0, 0, $color); imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); //可以保存、也可以输出到浏览器 //1. imagepng()增加第二个参数表示保存文件 //通常会把压缩之后的图片保存到thumb子目录中,按照日期格式的子目录保存 $sub_path = date('Ymd').'/'; $path = $this -> thumb_path; if(!is_dir($path.$sub_path)){ mkdir($path.$sub_path,0777,true); } //thumb/20170402/ //压缩的图像的文件名,在原文件名的基础上增加前缀:thumb_bs.png $origin_filename = basename($this->file); $thumb_name = 'thumb_'.$origin_filename; //header("Content-Type:image/png"); imagepng($dst_image,$path.$sub_path.$thumb_name); //最后,一定要把文件地址返回(接收之后最后保存起来) return $sub_path.$thumb_name; } } $thumb = new Thumb(); $thumb -> file = 'bs.png'; $thumb -> thumb_path = 'thumb/'; $file = $thumb -> makeThumb(50, 50); var_dump($file);

 

posted @ 2018-10-08 19:09  醉风云  阅读(172)  评论(0编辑  收藏  举报