<?php
/**
* GDBasic.php
* author: F.X
* date: 2017
* description GD基礎類
*/
namespace Imooc\Lib;
class GDBasic
{
protected static $_check =false;
//檢查服務器環境中gd庫
public static function check()
{
//當靜態變量不爲false
if(static::$_check)
{
return true;
}
//檢查gd庫是否加載
if(!function_exists("gd_info"))
{
throw new \Exception('GD is not exists');
}
//檢查gd庫版本
$version = '';
$info = gd_info();
if(preg_match("/\\d+\\.\\d+(?:\\.\\d+)?/", $info["GD Version"], $matches))
{
$version = $matches[0];
}
//當gd庫版本小於2.0.1
if(!version_compare($version,'2.0.1','>='))
{
throw new \Exception("GD requires GD version '2.0.1' or greater, you have " . $version);
}
self::$_check = true;
return self::$_check;
}
}
GDBasic.php
Image.php
<?php
/*
* @Description: 圖像類
* @Author: your name
* @Date: 2019-07-02 09:19:45
* @LastEditTime: 2019-07-02 10:13:06
* @LastEditors: Please set LastEditors
*/
namespace Imooc\Lib;
require_once 'GDBasic.php';
class Image extends GDBasic
{
protected $_width;
protected $_height;
protected $_type;
protected $_im;
protected $_mime;
protected $_real_path;
public function __construct($file)
{
//檢查GD庫
self::check();
$imageInfo = $this->createImageByFile($file);
$this->_width = $imageInfo['width'];
$this->_height = $imageInfo['height'];
$this->_type = $imageInfo['type'];
$this->_im = $imageInfo['im'];
$this->_mime = $imageInfo['mime'];
$this->_real_path = $imageInfo['real_path'];
}
/**
* @根據文件創建圖像:
* @param $file
* @return array
* @throws \Exception
*/
public function createImageByFile($file)
{
//檢查文件是否存在
if(!file_exists($file))
{
throw new \Exception('file is not exists');
}
//獲取圖像信息
$imageInfo = getimagesize($file);
$realpath = realpath($file);
if(!$imageInfo)
{
throw new \Exception('file is not image file');
}
switch($imageInfo[2])
{
case IMAGETYPE_GIF:
$im = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$im = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$im = imagecreatefrompng($file);
break;
default:
throw new \Exception('image file is not gif,png,jpeg');
}
return array(
'width'=>$imageInfo[0],
'height'=>$imageInfo[1],
'type'=>$imageInfo[2],
'mime'=>$imageInfo['mime'],
'im'=>$im,
'real_path'=>$realpath
);
}
/**
* @縮略圖:
* @param int $width 縮略圖寬度
* @param int $height 縮略圖高度
* @return: $this
* @throws \Exception
*/
public function resize($width,$height)
{
if(!is_numeric($width) || !is_numeric($height))
{
throw new \Exception('width and height is not number');
}
//根據傳參的寬高獲取最終的寬高
$srcW = $this->_width;
$srcH = $this->_height;
if($width<=0 ||$height<=0)
{
$desW = $srcW;
$desH = $srcH;
}
else
{
$srcP = $srcW/$srcH;
$desP = $width/$height;
if($width > $srcW)
{
if($height > $srcH)
{
$desW = $srcW;
$desH = $srcH;
}
else
{
$desH = $height;
$desW = round($desH * $srcp);
}
}
else
{
if($desP > $srcP)
{
$desW = $width;
$desH = round($desW/$srcP);
}
else
{
$desH = $height;
$desw = round($desH * $srcP);
}
}
}
//檢測php版本是否小於php5.5
if(version_compare(PHP_VERSION,'5.5.0','<'))
{
$desIm = imagecreatetruecolor($desW,$desH);
if(imagecopyresampled($desIm,$this->_im,0,0,0,0,$desW,$desH,$srcW,$srcH))
{
imagedestory($this->_im);
$this->_im = $desIm;
$this->_width = imagesx($this->_im);
$this->_height = imagesy($this->_im);
}
}
else
{
if($desIm = imagescale($this->_im,$desW,$desH))
{
$this->_im = $desIm;
$this->_width = imagesx($this->_im);
$this->_height = imagesy($this->_im);
}
}
return $this;
}
/**
* @圖像輸出:
* @param {type}
* @return: bool
*/
public function show()
{
header('Content-Type:'.$this->_mime);
if($this->_type == 1)
{
imagegif($this->_im);
return true;
}
if($this->_type ==2)
{
imagejpeg($this->_im);
return true;
}
if($this->_type == 3)
{
imagepng($this->_im);
return true;
}
}
}
?>
show.php
<?php
require_once './lib/Image.php';
$image = new \Imooc\Lib\Image('./images/b.png');
$image->resize(200,100)->show();
?>