php怎样完成网站的图片紧缩
我们常常会用到把上传的大图片紧缩,特别是体积,在微信等APP应用上,也默许都是有紧缩的,那末,怎样对图片大幅度紧缩却仍能坚持较高的清晰度呢?
紧缩通常是有按比例缩放,和指定宽度紧缩的,效果很不错,一个数码相机拍的4M图片,紧缩后坚持了较高的清晰度和原图宽高值,只要700K。
下面是代码(有两个文件,imgcompress.class.php 类,及compress.php)
1、建立一个imgcompress类,imgcompress.class.php
<?php /** * 图片紧缩类:经由过程缩放来紧缩。 * 假如要坚持源图比例,把参数$percent坚持为即可。 * 纵然原比例紧缩,也可大幅度减少。数码相机M图片。也能够缩为KB摆布。假如减少比例,则体积会更小。 * * 效果:可保留、可直接显现。 */ class imgcompress{ private $src; private $image; private $imageinfo; private $percent = .; /** * 图片紧缩 * @param $src 源图 * @param float $percent 紧缩比例 */ public function __construct($src, $percent=) { $this->src = $src; $this->percent = $percent; } /** 高清紧缩图片 * @param string $saveName 供应图片名(可不带扩展名,用源图扩展名)用于保留。或不供应文件名直接显现 */ public function compressImg($saveName='') { $this->_openImage(); if(!empty($saveName)) $this->_saveImage($saveName); //保留 else $this->_showImage(); } /** * 内部:翻开图片 */ private 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); $this->_thumpImage(); } /** * 内部:操纵图片 */ private 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,,,,,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']); imagedestroy($this->image); $this->image = $image_thump; } /** * 输出图片:保留图片则用saveImage() */ private function _showImage() { header('Content-Type: image/'.$this->imageinfo['type']); $funcs = "image".$this->imageinfo['type']; $funcs($this->image); } /** * 保留图片到硬盘: * @param string $dstImgName 、可指定字符串不带后缀的称号,运用源图扩展名 。、直接指定目的图片名带扩展名。 */ private function _saveImage($dstImgName) { if(empty($dstImgName)) return false; $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif']; //假如目的图片名有后缀就用目的图片扩展名 后缀,假如没有,则用源图的扩展名 $dstExt = strrchr($dstImgName ,"."); $sourseExt = strrchr($this->src ,"."); if(!empty($dstExt)) $dstExt =strtolower($dstExt); if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt); //有指定目的名扩展名 if(!empty($dstExt) && in_array($dstExt,$allowImgs)){ $dstName = $dstImgName; }elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){ $dstName = $dstImgName.$sourseExt; }else{ $dstName = $dstImgName.$this->imageinfo['type']; } $funcs = "image".$this->imageinfo['type']; $funcs($this->image,$dstName); } /** * 烧毁图片 */ public function __destruct(){ imagedestroy($this->image); } }
2、运用,compress.php
<?php require_once 'imgcompress.class.php'; $source = 'test.png';//原图文件名 $dst_img = 'test_.png';//保留图片的文件名 $percent = ; #原图紧缩,不缩放,但体积大大下降 $image = (new imgcompress($source,$percent))->compressImg($dst_img);
运用以后个人感觉 $percent 设置为0.5 摆布就不错了,紧缩后的图片与原图质量基础一样。
更多PHP相干学问,请接见ki4网!
以上就是php怎样完成网站的图片紧缩的细致内容,更多请关注ki4网别的相干文章!