ShearPhoto创始人明哥写的PHP压缩类,包括根据图片坐标裁剪、生成缩略图等功能,php超好用的图片压缩类。
/*
* 作者:明哥
* 明哥身份:全栈高级总工程师
* 明哥图片处理类
* 开发时间2017/8/3
* 版本 1.0
*/
class Image {
protected $img_mime;
protected $img_from;
protected $img_to;
protected $img_target;
protected $error = false;
final function __destruct() {
$this -> unImg();
}
final function __call($function_name, $args) {
}
/*
* 裁入图片
* @param $img 图路径
*/
public function open($img) {
$type = gettype($img);
$this -> unImg();
if ($type == "string") {
$this -> img_mime = @getimagesize($img);
if (is_array($this -> img_mime)) {
$type = $this -> img_mime[2];
$this -> img_from = $this -> imagecreatefrom($img, $type);
$this -> img_mime["type"] = image_type_to_extension($type);
$this -> error = false;
return $this;
}
}
$this -> error = true;
return $this;
}
/*
* 核心处理
* @param $w 图像处理后最终宽度
* @param $h 图像处理后最终高度
* @param $corpType 剪裁类型 ,只有设置为4 X 和Y才有效
* @param $x 剪裁的X
* @param $y 剪裁的Y
*/
protected function core($w, $h, $cropType, $x = 0, $y = 0) {
if (!$this -> img_from && !$this -> img_target) {
return $this;
}
$w = (int)trim($w);
$h = (int)trim($h);
if ($this -> img_to and ($this -> img_to[0] != $this -> img_target[0])) {
imagedestroy($this -> img_to[0]);
$this -> img_to = null;
}
if (!$w || !$h) {
$this -> error = true;
return $this;
}
$this -> handleImg($w, $h, $cropType, $x, $y);
return $this;
}
/*
* 缩略图
* @param $w 图像处理后最终宽度
* @param $h 图像处理后最终高度
* @param $cropType 剪裁的类型
*/
public function thumb($w, $h, $cropType = 3) {
return $this -> core($w, $h, $cropType);
}
/*
* 剪裁
* @param $w 图像处理后最终宽度
* @param $h 图像处理后最终高度
* @param $x 剪裁的X
* @param $y 剪裁的Y
*/
public function crop($w, $h, $x = 0, $y = 0) {
return $this -> core($w, $h, 4, $x, $y);
}
/*
* 剪裁
* @param $saveUrl 图像url
* @param $quality 图像质量
*/
public function save($saveUrl, $quality = 85) {
if (!$this -> img_to or empty($saveUrl)) {
$this -> error = true;
return $this;
}
$img_to = $this -> img_to;
$GD = $this -> getGD($saveUrl);
if ($GD == "imagejpeg") {
$is = @call_user_func($GD, $img_to[0], $saveUrl, $quality);
} else {
$is = @call_user_func($GD, $img_to[0], $saveUrl);
}
$this -> error = !$is;
return $this;
}
public function getError() {
return $this -> error;
}
public function unImg() {
if ($this -> img_from) {
imagedestroy($this -> img_from[0]);
$this -> img_from = null;
}
$isTwins = false;
if ($this -> img_to) {
$isTwins = $this -> img_target[0] == $this -> img_to[0];
imagedestroy($this -> img_to[0]);
$this -> img_to = null;
}
if ($this -> img_target) {
if (!$isTwins) {
imagedestroy($this -> img_target[0]);
}
$this -> img_target = null;
}
$this -> img_mime = null;
$this -> error = false;
return $this;
}
public function get_img_from() {
return $this -> img_from ? $this -> img_from : null;
}
public function get_img_to() {
return $this -> img_to;
}
public function width() {
return $this -> img_mime ? $this -> img_mime[0] : null;
}
public function height() {
return $this -> img_mime ? $this -> img_mime[1] : null;
}
public function type() {
return $this -> img_mime ? $this -> img_mime["type"] : null;
}
public function mime() {
return $this -> img_mime ? $this -> img_mime : null;
}
public function size() {
list($w, $h) = $this -> img_mime;
return array($w, $h);
}
public function target($int = 2) {
$this -> img_target = $int == 2 ? $this -> img_to : null;
return $this;
}
protected function getGD($saveUrl) {
$ext = pathinfo($saveUrl, PATHINFO_EXTENSION);
switch ($ext) {
case 'jpg' :
case 'jpeg' :
return "imagejpeg";
case 'gif' :
return "imagegif";
case 'png' :
return "imagepng";
default :
return $this -> img_from[1];
}
}
protected function imagecreatefrom($url, $type) {
switch ($type) {
case 1 :
$src = @imagecreatefromgif($url);
$GdFun = "imagegif";
break;
case 2 :
$src = @imagecreatefromjpeg($url);
$GdFun = "imagejpeg";
break;
case 3 :
$src = @imagecreatefrompng($url);
$GdFun = "imagepng";
break;
default :
$this -> error = true;
return false;
}
return array($src, $GdFun);
}
protected function createBg($im, $r, $g, $b) {
imagefill($im, 0, 0, imagecolorallocate($im, $r, $g, $b));
}
protected function handleImg($w, $h, $cropType = 3, $x = 0, $y = 0) {
$img_to = @imagecreatetruecolor($w, $h);
$this -> createBg($img_to, 255, 255, 255);
if ($this -> img_target) {
$dest = $this -> img_target[0];
$dw = $this -> img_target[1];
$dh = $this -> img_target[2];
} else {
$dest = $this -> img_from[0];
list($dw, $dh) = $this -> img_mime;
}
list($cropW, $cropH, $l, $t) = $this -> getCrop($w, $h, $dw, $dh, $cropType, $x, $y);
$imagecopy = @imagecopyresampled($img_to, $dest, $l, $t, 0, 0, $cropW, $cropH, $dw, $dh);
if ($imagecopy) {
$this -> img_to = array($img_to, $w, $h);
$this -> error = false;
} else {
$this -> error = true;
}
}
protected function cropCenter($w, $h, $dw, $dh) {
$proportion = bcdiv($dw, $dh);
$cropH = $h;
$cropW = bcmul($cropH, $proportion);
if ($cropW < $w) {
$cropW = $w;
$cropH = bcdiv($w, $proportion);
}
return array($cropW, $cropH);
}
protected function getCrop($w, $h, $dw, $dh, $type, $x = 0, $y = 0) {
switch ($type) {
case 1 :
//拉伸剪裁
return array($w, $h, 0, 0);
case 2 :
//向左剪裁
list($cropW, $cropH) = $this -> cropCenter($w, $h, $dw, $dh);
return array($cropW, $cropH, 0, 0);
case 4 :
// 坐标剪裁
return array($dw, $dh, $x, $y);
case 3 :
//居中剪裁
default :
list($cropW, $cropH) = $this -> cropCenter($w, $h, $dw, $dh);
return array($cropW, $cropH, intval(bcdiv(bcsub($w, $cropW), 2)), intval(bcdiv(bcsub($h, $cropH), 2)));
}
}
}
系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791