间接展现具体代码:
收费学习视频教程分享:php视频教程
<?php /** * 图片紧缩类:经过缩放来紧缩。假如要放弃源图比例,把参数$percent放弃为1便可。 * 即便原比例紧缩,也可年夜幅度减少。数码相机4M图片。也能够缩为700KB阁下。假如减少比例,则体积会更小。 * 后果:可保留、可间接显示。 */ class imgCompression{ private $src; private $image; private $imageinfo; private $percent = 0.5; /** * 图片紧缩 * @param $src 源图 * @param float $percent 紧缩比例 */ public function __construct($src, $percent=1) { $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,0,0,0,0,$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); } } // eg: $source = 'img.jpg';//原图 $dst_img = 'img1.jpg'; //可加寄存门路 $percent = 1; //紧缩比例 原图紧缩,没有缩放 $image = new imgCompression($source,$percent); $image->compressImg($dst_img); ?>
相干文章教程保举:php教程
以上就是php若何完成图片紧缩的同时放弃明晰度的具体内容,更多请存眷资源魔其它相干文章!
标签: php 实现 压缩 图片 php教程 php故障解决 php使用问题 保持 清晰度
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!