压缩图片的工具类:
<?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); } } ?>
测试:
<?PHPrequire '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按一定比例压缩图片(保持清晰度)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)