图片紧缩是咱们一样平常开发中常常应用的操作,正在现在需要不少的状况往往,上传的一张图片会被紧缩成没有同比例的图片,每一次去操作也是一件十分繁琐的事件,于是进行了封装了一个紧缩图片的操作类,心愿各人遇到后,不必再为写不少紧缩图片代码懊恼了。
紧缩图片的对象类:
/**
图片紧缩操作类
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);
}
}
?>
测试:
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按肯定比例紧缩图片(放弃明晰度)的具体内容,更多请存眷资源魔其它相干文章!
本网站(www.ziyuanmo.com)刊载的所有内容,包括文字、图片、音频、视频、软件、程序、以及网页版式设计等均由用户发表。
虚拟收费资源付款后概不退款,请考虑在三后支付!如有侵权可联系邮箱:ziyuanmo@admin.com,会在第一时间解决!
资源猫 » PHP按一定比例压缩图片(保持清晰度)-php教程
分享到: