mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
注释和规范化调整及完善
注释和规范化调整及完善,以及部分代码优化
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace think\image\driver;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Gd
|
||||
{
|
||||
/**
|
||||
@@ -18,6 +20,9 @@ class Gd
|
||||
* @var resource
|
||||
*/
|
||||
private $im;
|
||||
/**
|
||||
* @var $git \think\image\driver\Gif
|
||||
*/
|
||||
private $gif;
|
||||
/**
|
||||
* 图像信息,包括width,height,type,mime,size
|
||||
@@ -36,7 +41,11 @@ class Gd
|
||||
|
||||
/**
|
||||
* 打开一张图像
|
||||
*
|
||||
* @param string $imgname 图像路径
|
||||
*
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function open($imgname)
|
||||
{
|
||||
@@ -50,7 +59,7 @@ class Gd
|
||||
|
||||
//检测图像合法性
|
||||
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
|
||||
throw new \Exception('非法图像文件');
|
||||
throw new Exception('非法图像文件');
|
||||
}
|
||||
|
||||
//设置图像信息
|
||||
@@ -78,14 +87,18 @@ class Gd
|
||||
|
||||
/**
|
||||
* 保存图像
|
||||
*
|
||||
* @param string $imgname 图像保存名称
|
||||
* @param string $type 图像类型
|
||||
* @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function save($imgname, $type = null, $interlace = true)
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有可以被保存的图像资源');
|
||||
throw new Exception('没有可以被保存的图像资源');
|
||||
}
|
||||
|
||||
//自动获取图像类型
|
||||
@@ -113,12 +126,13 @@ class Gd
|
||||
|
||||
/**
|
||||
* 返回图像宽度
|
||||
* @return integer 图像宽度
|
||||
* @return int 图像宽度
|
||||
* @throws Exception
|
||||
*/
|
||||
public function width()
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有指定图像资源');
|
||||
throw new Exception('没有指定图像资源');
|
||||
}
|
||||
|
||||
return $this->info['width'];
|
||||
@@ -126,12 +140,13 @@ class Gd
|
||||
|
||||
/**
|
||||
* 返回图像高度
|
||||
* @return integer 图像高度
|
||||
* @return int 图像高度
|
||||
* @throws Exception
|
||||
*/
|
||||
public function height()
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有指定图像资源');
|
||||
throw new Exception('没有指定图像资源');
|
||||
}
|
||||
|
||||
return $this->info['height'];
|
||||
@@ -140,11 +155,12 @@ class Gd
|
||||
/**
|
||||
* 返回图像类型
|
||||
* @return string 图像类型
|
||||
* @throws Exception
|
||||
*/
|
||||
public function type()
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有指定图像资源');
|
||||
throw new Exception('没有指定图像资源');
|
||||
}
|
||||
|
||||
return $this->info['type'];
|
||||
@@ -153,11 +169,12 @@ class Gd
|
||||
/**
|
||||
* 返回图像MIME类型
|
||||
* @return string 图像MIME类型
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mime()
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有指定图像资源');
|
||||
throw new Exception('没有指定图像资源');
|
||||
}
|
||||
|
||||
return $this->info['mime'];
|
||||
@@ -166,11 +183,12 @@ class Gd
|
||||
/**
|
||||
* 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
|
||||
* @return array 图像尺寸
|
||||
* @throws Exception
|
||||
*/
|
||||
public function size()
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有指定图像资源');
|
||||
throw new Exception('没有指定图像资源');
|
||||
}
|
||||
|
||||
return [$this->info['width'], $this->info['height']];
|
||||
@@ -178,17 +196,21 @@ class Gd
|
||||
|
||||
/**
|
||||
* 裁剪图像
|
||||
*
|
||||
* @param integer $w 裁剪区域宽度
|
||||
* @param integer $h 裁剪区域高度
|
||||
* @param integer $x 裁剪区域x坐标
|
||||
* @param integer $y 裁剪区域y坐标
|
||||
* @param integer $width 图像保存宽度
|
||||
* @param integer $height 图像保存高度
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有可以被裁剪的图像资源');
|
||||
throw new Exception('没有可以被裁剪的图像资源');
|
||||
}
|
||||
|
||||
//设置保存尺寸
|
||||
@@ -217,14 +239,18 @@ class Gd
|
||||
|
||||
/**
|
||||
* 生成缩略图
|
||||
*
|
||||
* @param integer $width 缩略图最大宽度
|
||||
* @param integer $height 缩略图最大高度
|
||||
* @param integer $type 缩略图裁剪类型
|
||||
* @param int $type 缩略图裁剪类型
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE)
|
||||
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALING)
|
||||
{
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有可以被缩略的图像资源');
|
||||
throw new Exception('没有可以被缩略的图像资源');
|
||||
}
|
||||
|
||||
//原图宽度和高度
|
||||
@@ -237,7 +263,7 @@ class Gd
|
||||
case THINKIMAGE_THUMB_SCALING:
|
||||
//原图尺寸小于缩略图尺寸则不进行缩略
|
||||
if ($w < $width && $h < $height) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
//计算缩放比例
|
||||
@@ -296,6 +322,8 @@ class Gd
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$neww = $w * $scale;
|
||||
$newh = $h * $scale;
|
||||
$x = $this->info['width'] - $w;
|
||||
$y = $this->info['height'] - $h;
|
||||
$posx = ($width - $w * $scale) / 2;
|
||||
$posy = ($height - $h * $scale) / 2;
|
||||
|
||||
@@ -322,7 +350,7 @@ class Gd
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('不支持的缩略图裁剪类型');
|
||||
throw new Exception('不支持的缩略图裁剪类型');
|
||||
}
|
||||
|
||||
/* 裁剪图像 */
|
||||
@@ -332,25 +360,29 @@ class Gd
|
||||
|
||||
/**
|
||||
* 添加水印
|
||||
* @param string $source 水印图片路径
|
||||
* @param integer $locate 水印位置
|
||||
* @param integer $alpha 水印透明度
|
||||
*
|
||||
* @param string $source 水印图片路径
|
||||
* @param int $locate 水印位置
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @internal param int $alpha 水印透明度
|
||||
*/
|
||||
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST)
|
||||
{
|
||||
//资源检测
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有可以被添加水印的图像资源');
|
||||
throw new Exception('没有可以被添加水印的图像资源');
|
||||
}
|
||||
|
||||
if (!is_file($source)) {
|
||||
throw new \Exception('水印图像不存在');
|
||||
throw new Exception('水印图像不存在');
|
||||
}
|
||||
|
||||
//获取水印图像信息
|
||||
$info = getimagesize($source);
|
||||
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
|
||||
throw new \Exception('非法水印文件');
|
||||
throw new Exception('非法水印文件');
|
||||
}
|
||||
|
||||
//创建水印图像资源
|
||||
@@ -420,7 +452,7 @@ class Gd
|
||||
if (is_array($locate)) {
|
||||
list($x, $y) = $locate;
|
||||
} else {
|
||||
throw new \Exception('不支持的水印位置类型');
|
||||
throw new Exception('不支持的水印位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -446,23 +478,27 @@ class Gd
|
||||
|
||||
/**
|
||||
* 图像添加文字
|
||||
*
|
||||
* @param string $text 添加的文字
|
||||
* @param string $font 字体路径
|
||||
* @param integer $size 字号
|
||||
* @param string $color 文字颜色
|
||||
* @param integer $locate 文字写入位置
|
||||
* @param int $locate 文字写入位置
|
||||
* @param integer $offset 文字相对当前位置的偏移量
|
||||
* @param integer $angle 文字倾斜角度
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function text($text, $font, $size, $color = '#00000000',
|
||||
$locate = THINKIMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0) {
|
||||
//资源检测
|
||||
if (empty($this->im)) {
|
||||
throw new \Exception('没有可以被写入文字的图像资源');
|
||||
throw new Exception('没有可以被写入文字的图像资源');
|
||||
}
|
||||
|
||||
if (!is_file($font)) {
|
||||
throw new \Exception("不存在的字体文件:{$font}");
|
||||
throw new Exception("不存在的字体文件:{$font}");
|
||||
}
|
||||
|
||||
//获取文字信息
|
||||
@@ -536,7 +572,7 @@ class Gd
|
||||
$x += $posx;
|
||||
$y += $posy;
|
||||
} else {
|
||||
throw new \Exception('不支持的文字位置类型');
|
||||
throw new Exception('不支持的文字位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,7 +593,7 @@ class Gd
|
||||
$color[3] = 0;
|
||||
}
|
||||
} elseif (!is_array($color)) {
|
||||
throw new \Exception('错误的颜色值');
|
||||
throw new Exception('错误的颜色值');
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
@@ -10,9 +10,6 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\image\driver;
|
||||
|
||||
use think\image\driver\GIFDecoder as GIFDecoder;
|
||||
use think\image\driver\GIFEncoder as GIFEncoder;
|
||||
class Gif
|
||||
{
|
||||
/**
|
||||
@@ -44,7 +41,7 @@ class Gif
|
||||
$de = new GIFDecoder($src);
|
||||
$this->frames = $de->GIFGetFrames();
|
||||
$this->delays = $de->GIFGetDelays();
|
||||
} catch (Exception $e) {
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception("解码GIF图片出错");
|
||||
}
|
||||
}
|
||||
@@ -53,7 +50,7 @@ class Gif
|
||||
/**
|
||||
* 设置或获取当前帧的数据
|
||||
* @param string $stream 二进制数据流
|
||||
* @return boolean 获取到的数据
|
||||
* @return mixed 获取到的数据
|
||||
*/
|
||||
public function image($stream = null)
|
||||
{
|
||||
@@ -85,7 +82,6 @@ class Gif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
@@ -110,8 +106,7 @@ class Gif
|
||||
::
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
*/
|
||||
|
||||
class GIFEncoder
|
||||
class GIFEncode
|
||||
{
|
||||
public $GIF = "GIF89a"; /* GIF header 6 bytes */
|
||||
public $VER = "GIFEncoder V2.05"; /* Encoder version */
|
||||
@@ -139,14 +134,14 @@ class GIFEncoder
|
||||
$GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
|
||||
$GIF_red, $GIF_grn, $GIF_blu, $GIF_mod
|
||||
) {
|
||||
if (!is_array($GIF_src) && !is_array($GIF_tim)) {
|
||||
if (!is_array($GIF_src)) {
|
||||
printf("%s: %s", $this->VER, $this->ERR['ERR00']);
|
||||
exit(0);
|
||||
}
|
||||
$this->LOP = ($GIF_lop > -1) ? $GIF_lop : 0;
|
||||
$this->DIS = ($GIF_dis > -1) ? (($GIF_dis < 3) ? $GIF_dis : 3) : 2;
|
||||
$this->COL = ($GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1) ?
|
||||
($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
|
||||
($GIF_red | ($GIF_grn << 8) | ($GIF_blu << 16)) : -1;
|
||||
|
||||
for ($i = 0; $i < count($GIF_src); $i++) {
|
||||
if (strToLower($GIF_mod) == "url") {
|
||||
@@ -163,23 +158,23 @@ class GIFEncoder
|
||||
}
|
||||
for ($j = (13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07))), $k = true; $k; $j++) {
|
||||
switch ($this->BUF[$i]{ $j}) {
|
||||
case "!":
|
||||
if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE") {
|
||||
printf("%s: %s ( %s source )!", $this->VER, $this->ERR['ERR03'], ($i + 1));
|
||||
exit(0);
|
||||
}
|
||||
break;
|
||||
case ";":
|
||||
$k = false;
|
||||
break;
|
||||
case "!":
|
||||
if ((substr($this->BUF[$i], ($j + 3), 8)) == "NETSCAPE") {
|
||||
printf("%s: %s ( %s source )!", $this->VER, $this->ERR['ERR03'], ($i + 1));
|
||||
exit(0);
|
||||
}
|
||||
break;
|
||||
case ";":
|
||||
$k = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
GIFEncoder::GIFAddHeader();
|
||||
self::GIFAddHeader();
|
||||
for ($i = 0; $i < count($this->BUF); $i++) {
|
||||
GIFEncoder::GIFAddFrames($i, $GIF_dly[$i]);
|
||||
self::GIFAddFrames($i, $GIF_dly[$i]);
|
||||
}
|
||||
GIFEncoder::GIFAddFooter();
|
||||
self::GIFAddFooter();
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
@@ -189,14 +184,13 @@ class GIFEncoder
|
||||
*/
|
||||
public function GIFAddHeader()
|
||||
{
|
||||
$cmap = 0;
|
||||
|
||||
if (ord($this->BUF[0]{10}) & 0x80) {
|
||||
$cmap = 3 * (2 << (ord($this->BUF[0]{10}) & 0x07));
|
||||
|
||||
$this->GIF .= substr($this->BUF[0], 6, 7);
|
||||
$this->GIF .= substr($this->BUF[0], 13, $cmap);
|
||||
$this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord($this->LOP) . "\0";
|
||||
$this->GIF .= "!\377\13NETSCAPE2.0\3\1" . self::GIFWord($this->LOP) . "\0";
|
||||
}
|
||||
}
|
||||
/*
|
||||
@@ -207,7 +201,7 @@ class GIFEncoder
|
||||
*/
|
||||
public function GIFAddFrames($i, $d)
|
||||
{
|
||||
|
||||
$Locals_img='';
|
||||
$Locals_str = 13 + 3 * (2 << (ord($this->BUF[$i]{10}) & 0x07));
|
||||
|
||||
$Locals_end = strlen($this->BUF[$i]) - $Locals_str - 1;
|
||||
@@ -217,12 +211,12 @@ class GIFEncoder
|
||||
$Locals_len = 2 << (ord($this->BUF[$i]{10}) & 0x07);
|
||||
|
||||
$Global_rgb = substr($this->BUF[0], 13,
|
||||
3 * (2 << (ord($this->BUF[0]{10}) & 0x07)));
|
||||
3 * (2 << (ord($this->BUF[0]{10}) & 0x07)));
|
||||
$Locals_rgb = substr($this->BUF[$i], 13,
|
||||
3 * (2 << (ord($this->BUF[$i]{10}) & 0x07)));
|
||||
3 * (2 << (ord($this->BUF[$i]{10}) & 0x07)));
|
||||
|
||||
$Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 0) .
|
||||
chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
|
||||
chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . "\x0\x0";
|
||||
|
||||
if ($this->COL > -1 && ord($this->BUF[$i]{10}) & 0x80) {
|
||||
for ($j = 0; $j < (2 << (ord($this->BUF[$i]{10}) & 0x07)); $j++) {
|
||||
@@ -232,24 +226,27 @@ class GIFEncoder
|
||||
ord($Locals_rgb{3 * $j + 2}) == (($this->COL >> 0) & 0xFF)
|
||||
) {
|
||||
$Locals_ext = "!\xF9\x04" . chr(($this->DIS << 2) + 1) .
|
||||
chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
|
||||
chr(($d >> 0) & 0xFF) . chr(($d >> 8) & 0xFF) . chr($j) . "\x0";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
switch ($Locals_tmp{0}) {
|
||||
case "!":
|
||||
$Locals_img = substr($Locals_tmp, 8, 10);
|
||||
$Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
|
||||
break;
|
||||
case ",":
|
||||
$Locals_img = substr($Locals_tmp, 0, 10);
|
||||
$Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
|
||||
break;
|
||||
case "!":
|
||||
/**
|
||||
* @var string $Locals_img;
|
||||
*/
|
||||
$Locals_img = substr($Locals_tmp, 8, 10);
|
||||
$Locals_tmp = substr($Locals_tmp, 18, strlen($Locals_tmp) - 18);
|
||||
break;
|
||||
case ",":
|
||||
$Locals_img = substr($Locals_tmp, 0, 10);
|
||||
$Locals_tmp = substr($Locals_tmp, 10, strlen($Locals_tmp) - 10);
|
||||
break;
|
||||
}
|
||||
if (ord($this->BUF[$i]{10}) & 0x80 && $this->IMG > -1) {
|
||||
if ($Global_len == $Locals_len) {
|
||||
if (GIFEncoder::GIFBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
|
||||
if (self::GIFBlockCompare($Global_rgb, $Locals_rgb, $Global_len)) {
|
||||
$this->GIF .= ($Locals_ext . $Locals_img . $Locals_tmp);
|
||||
} else {
|
||||
$byte = ord($Locals_img{9});
|
||||
@@ -460,7 +457,6 @@ class GIFDecoder
|
||||
*/
|
||||
public function GIFReadDescriptor()
|
||||
{
|
||||
$GIF_screen = array();
|
||||
|
||||
GIFDecoder::GIFGetByte(9);
|
||||
$GIF_screen = $this->GIF_buffer;
|
||||
|
||||
Reference in New Issue
Block a user