验证中...
Languages: PHP
Categories: Web开发技术
Latest update 2019-06-25 22:14
PictureCompressionTool.php
Raw Copy
<?php
/**
* 图片压缩类
* Class PictureCompressionTool
*/
class PictureCompressionTool{
//原图宽度
private $width=0;
//原图高度
private $height=0;
//压缩后的宽度
private $newWidth=0;
//压缩后的高度
private $newHeight=0;
//来源图片地址
private $imgSrc='';
//临时图片地址
private $imgTempPath='./.temp';
//是否是网络资源
private $isWebImg=false;
//资源类型
private $imgType = '';
//资源类型对应的后缀名
private $imgTypeList = [
1=>'gif',2=>'jpeg',3=>'png',4=>'swf',5=>'psd',6=>'bmp',
7=>'tiff',8=>'tiff',9=>'jpc',10=>'jp2',11=>'jpx',12=>'jb2',
13=>'swc',14=>'iff',15=>'wbmp',16=>'xbm'
];
//输出图片地址
private $imgSaveSrc='';
private static $obj='';
protected static function getObj(){
if (empty(self::$obj)) {
self::$obj = new self();
}
return self::$obj;
}
/**
* 初始化参数
* @param $imgSrc
* @param $imgSaveSrc
* @param $Proportion
* @throws Exception
*/
private function init($imgSrc,$imgSaveSrc, $Proportion) {
ini_set('memory_limit','1024M');
if ($this->isWebImg) {
$this->imgSrc = $this->imgTempPath;
} else {
$this->imgSrc = $imgSrc;
}
if (!file_exists($this->imgSrc)) {
throw new \Exception('图片资源不存在!');
}
list($this->width,$this->height,$this->imgType)=getimagesize($imgSrc);
$this->newWidth = $this->width;
$this->newHeight = $this->height;
while ($this->newWidth > 1920 || $this->newHeight > 1080) {
$this->newWidth *= 0.8;
$this->newHeight *= 0.8;
}
$this->newWidth *= $Proportion;
$this->newHeight *= $Proportion;
if ($imgSaveSrc) {
$this->imgSaveSrc = $imgSaveSrc;
} else {
$this->imgSaveSrc = 'ScarecrowTempImg.' . $this->imgTypeList[$this->imgType];
}
}
/**
* 获取网络地址图片
* @param $imgSrc
*/
private function getWebImageSource($imgSrc) {
$ext = substr($imgSrc, strrpos($imgSrc, '.'));
$this->imgTempPath .= $ext;
file_put_contents($this->imgTempPath, file_get_contents($imgSrc));
$this->isWebImg = true;
}
/**
* 检测地址是否是一个网络地址
* @param $imgSrc
* @return bool
*/
private function checkIsWebImg($imgSrc) {
return (bool)preg_match("/^(ftp|http(s)*):\/\/[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$/", $imgSrc);
}
/**
* 压缩图片
* @param $imgSrc 被压缩图片地址 (可以是网络地址)
* @param string $imgSaveSrc 被保存的地址 (绝对地址)
* @param string $Proportion 被压缩的比例(最终压缩的宽高比一定小于 1920:1080)
* @throws Exception
*/
public static function compressImg($imgSrc,$imgSaveSrc = '', $Proportion = '0.7') {
if (!extension_loaded('gd')) {
throw new \Exception('请安装GD库来进行操作支持');
}
$obj = self::getObj();
$obj->isWebImg = false;
if ($obj->checkIsWebImg($imgSrc)) {
$obj->getWebImageSource($imgSrc);
}
$obj->init($imgSrc, $imgSaveSrc, $Proportion);
$imageNewObj=imagecreatetruecolor($obj->newWidth, $obj->newHeight);
$imageObj = $obj->getImgObj($obj->imgSrc);
imagecopyresampled($imageNewObj, $imageObj, 0, 0, 0, 0, $obj->newWidth, $obj->newHeight, $obj->width, $obj->height);
imagejpeg($imageNewObj, $obj->imgSaveSrc,75);
imagedestroy($imageNewObj);
$obj->clearCache();
}
/**
* 获取图像对象
* @param $imgSrc
* @return resource
*/
private function getImgObj($imgSrc) {
switch ($this->imgType) {
case 1:
return imagecreatefromgif($imgSrc);
case 2:
return imagecreatefromjpeg($imgSrc);
case 3:
return imagecreatefrompng($imgSrc);
case 15:
return imagecreatefromwbmp($imgSrc);
case 16:
return imagecreatefromxbm($imgSrc);
default:
return imagecreatefromjpeg($imgSrc);
}
}
/**
* 清除缓存
*/
private function clearCache() {
if($this->isWebImg) {
@unlink($this->imgTempPath);
}
}
}
//使用方式
//PictureCompressionTool::compressImg('http://img.redocn.com/sheji/20141219/zhongguofengdaodeliyizhanbanzhijing_3744115.jpg','',2);
//echo "YES";

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025