图片紧缩是咱们一样平常开发中常常应用的操作,正在现在需要不少的状况往往,上传的一张图片会被紧缩成没有同比例的图片,每一次去操作也是一件十分繁琐的事件,于是进行了封装了一个紧缩图片的操作类,心愿各人遇到后,不必再为写不少紧缩图片代码懊恼了。
紧缩图片的对象类:
<?php /** 图片紧缩操作类 v1.0 */ class Image{ private $src; private $imageinfo; private $image; public $percent = 0.1; public function __construct($src){ $this->src = $src; } /** 关上图片 */ public function openImage(){ list($width, $height, $type, $attr) = getimagesize($this->src); $this->imageinfo = array( 'width'=>$width, 'height'=>$height, 'type'=>image_type_to_extension($type,false), 'attr'=>$attr ); $fun = "imagecreatefrom".$this->imageinfo['type']; $this->image = $fun($this->src); } /** 操作图片 */ public function thumpImage(){ $new_width = $this->imageinfo['width'] * $this->percent; $new_height = $this->imageinfo['height'] * $this->percent; $image_thump = imagecreatetruecolor($new_width,$new_height); //将原图复制带图片载体下面,而且依照肯定比例紧缩,极年夜的放弃了明晰度 imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']); imagedestroy($this->image); $this->image = $image_thump; } /** 输入图片 */ public function showImage(){ header('Content-Type: image/'.$this->imageinfo['type']); $funcs = "image".$this->imageinfo['type']; $funcs($this->image); } /** 保留图片到硬盘 */ public function saveImage($name){ $funcs = "image".$this->imageinfo['type']; $funcs($this->image,$name.'.'.$this->imageinfo['type']); } /** 销毁图片 */ public function __destruct(){ imagedestroy($this->image); } } ?>
测试:
<?php require 'image.class.php'; $src = "001.jpg"; $image = new Image($src); $image->percent = 0.2; $image->openImage(); $image->thumpImage(); $image->showImage(); $image->saveImage(md5("aa123")); ?>
后果:
更多PHP相干常识,请拜访PHP教程!
以上就是PHP按肯定比例紧缩图片(放弃明晰度)的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!