Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 5e76def

Browse files
author
xiajianjun
committed
update, change some logic
1 parent d88f2ae commit 5e76def

File tree

3 files changed

+115
-64
lines changed

3 files changed

+115
-64
lines changed

src/files/Captcha.php

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace inhere\tools\files;
1212

1313
use inhere\tools\exceptions\NotFoundException;
14+
use inhere\tools\exceptions\ExtensionMissException;
1415

1516
/**
1617
* Class Captcha
@@ -22,6 +23,7 @@ class Captcha
2223
public $width; // 画布宽
2324
public $height; // 画布高
2425
public $bgColor; // 背景色
26+
public $bgImage; // 背景图 $bgColor $bgImage 二选一
2527
public $font; // 字体
2628

2729
public $pixelNum; // 干扰点数量
@@ -56,14 +58,13 @@ public static function make($config=[])
5658
*/
5759
public function __construct(array $config=[])
5860
{
59-
$defaultConfig = $this->defaultConfig();
60-
61-
if ($config) {
62-
$config = array_merge( $defaultConfig, $config);
63-
} else {
64-
$config = $this->defaultConfig();
61+
if ( !$this->checkGd() ) {
62+
throw new ExtensionMissException('This tool required extension [gd].');
6563
}
6664

65+
$defaultConfig = $this->defaultConfig();
66+
$config = $config ? array_merge( $defaultConfig, $config) : $defaultConfig;
67+
6768
$this->config = $config;
6869
$this->font = $config['font'];
6970

@@ -75,30 +76,35 @@ public function __construct(array $config=[])
7576
static::$sessionKey = $config['sessionKey'];
7677
}
7778

78-
$this->codeStr = $config['rand_str'];
79-
80-
$this->fontSize = isset($config['font_size']) ? $config['font_size'] : $defaultConfig['font_size'];
81-
$this->charNum = isset($config['length']) ? $config['length'] : $defaultConfig['length'];
82-
$this->width = isset($config['width']) ? $config['width'] : $defaultConfig['width'];
83-
$this->height = isset($config['height']) ? $config['height'] : $defaultConfig['height'];
84-
$this->bgColor = isset($config['bg_color']) ? $config['bg_color'] : $defaultConfig['bg_color'];
85-
$this->pixelNum = isset($config['pixel_num']) ? $config['pixel_num'] : $defaultConfig['pixel_num'];
86-
$this->fontNum = isset($config['font_num']) ? $config['font_num'] : $defaultConfig['font_num'];
79+
$this->codeStr = $config['randStr'];
80+
$this->fontSize = $config['fontSize'];
81+
$this->charNum = $config['length'];
82+
$this->width = $config['width'];
83+
$this->height = $config['height'];
84+
$this->bgColor = $config['bgColor'];
85+
$this->bgImage = $config['bgImage'];
86+
$this->pixelNum = $config['pixelNum'];
87+
$this->fontNum = $config['fontNum'];
8788
}
8889

8990
public function defaultConfig()
9091
{
9192
return [
92-
'font' => dirname(__DIR__) . '/resources/fonts/Montserrat-Bold.ttf'#字体文件
93-
,'rand_str' => '23456789abcdefghigkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
94-
,'width' => '120'
95-
,'height' => '45'
96-
,'bg_color' => '#eeeeee'
97-
,'length' => '4'
98-
,'font_color' => ''
99-
,'font_size' => '24' #验证码字体大小
100-
,'pixel_num' => '10' #干扰点数量
101-
,'font_num' => '50' #干扰字符数量
93+
// 字体文件
94+
'font' => dirname(__DIR__) . '/resources/fonts/Montserrat-Bold.ttf',
95+
'randStr' => '23456789abcdefghigkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
96+
'width' => '120',
97+
'height' => '45',
98+
'bgColor' => '#eeeeee',
99+
'bgImage' => dirname(__DIR__) . '/resources/images/backgrounds/06.png',
100+
'length' => '4',
101+
'fontColor' => '',
102+
// 验证码字体大小
103+
'fontSize' => '24',
104+
// 干扰点数量
105+
'pixelNum' => '10',
106+
// 干扰字符数量
107+
'fontNum' => '50',
102108
];
103109
}
104110

@@ -205,23 +211,23 @@ private function drawChars()
205211
// 生成图像资源,Captcha-验证码
206212
public function create()
207213
{
208-
if (!$this->checkGd() ) {
209-
return false;
210-
}
214+
if ($this->bgImage && is_file($this->bgImage)) {
215+
$this->img = imagecreatefrompng($this->bgImage);
216+
} else {
217+
// 手动建立背景画布,图像资源
218+
$this->img = imagecreatetruecolor($this->width,$this->height);
211219

212-
//建立背景画布,图像资源
213-
$this->img = imagecreatetruecolor($this->width,$this->height);
214-
//给画布填充矩形的背景色rgb(230, 255, 230);
215-
$bgColor = $this->bgColor;
216-
//$bgColor=imagecolorallocate($this->img,230, 255, 230); //背景色
220+
//给画布填充矩形的背景色rgb(230, 255, 230);
221+
$bgColor = $this->bgColor;
217222

218-
//背景色
219-
$bgColor=imagecolorallocate(
220-
$this->img, hexdec(substr($bgColor, 1,2)),
221-
hexdec(substr($bgColor, 3,2)), hexdec(substr($bgColor, 5,2))
222-
);
223+
//背景色
224+
$bgColor=imagecolorallocate(
225+
$this->img, hexdec(substr($bgColor, 1,2)),
226+
hexdec(substr($bgColor, 3,2)), hexdec(substr($bgColor, 5,2))
227+
);
223228

224-
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$bgColor);
229+
imagefilledrectangle($this->img,0,0,$this->width,$this->height,$bgColor);
230+
}
225231

226232
//给资源画上边框-可选 rgb(153, 153, 255)
227233
// $borderColor = imagecolorallocate($this->img, 153, 153, 255); // 0-255

src/files/Picture.php

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
namespace inhere\tools\files;
1111

12+
use inhere\tools\exceptions\InvalidArgumentException;
1213
use inhere\tools\exceptions\InvalidConfigException;
1314
use inhere\tools\exceptions\ExtensionMissException;
15+
use inhere\tools\exceptions\FileSystemException;
1416

1517
/**
1618
* Class Picture
@@ -67,8 +69,8 @@ class Picture
6769
'width' => 150,
6870
// 缩略图高度
6971
'height' => 100,
70-
// 缩略图文件名前缀
71-
'prefix' => 'thumb_',
72+
// 缩略图文件名前缀 thumb_
73+
'prefix' => '',
7274
// 缩略图文件名后缀
7375
'suffix' => '',
7476
// 生成缩略图方式,
@@ -79,19 +81,16 @@ class Picture
7981
'path' => ''
8082
];
8183

82-
private $_error = '';
84+
private $_error = null;
8385

8486
/**
85-
* 正在操作的原文件
87+
* 正在操作的文件记录
8688
* @var string
8789
*/
88-
private $_workingRawFile = '';
89-
90-
/**
91-
* 正在操作的输出文件
92-
* @var string
93-
*/
94-
private $_workingOutFile = '';
90+
public $working = [
91+
'raw' => '', // 正在操作的原文件
92+
'out' => '', // 正在操作的输出文件
93+
];
9594

9695
private $_result = [];
9796

@@ -249,11 +248,13 @@ public function watermark($img, $outPath = '', $pos = '', $waterImg = '', $alpha
249248
imagedestroy($resThumb);
250249
}
251250

252-
$this->_workingRawFile = $img;
253-
$this->_workingOutFile = $outFile;
251+
$this->working = [
252+
'raw' => $img,
253+
'out' => $outFile,
254+
];
254255

255-
$this->_result['rawFile'] = $img;
256-
$this->_result['outFile'] = $outFile;
256+
$this->_result['workWater']['rawFile'] = $img;
257+
$this->_result['workWater']['outFile'] = $outFile;
257258

258259
return $this;
259260
}
@@ -279,21 +280,21 @@ public function thumb($img, $outFile = '', $path = '', $thumbWidth = '', $thumbH
279280
/**
280281
* 图片裁切处理(制作缩略图)
281282
* @param string $img 操作的图片文件路径(原图)
282-
* @param string $outFilename 另存文件名
283283
* @param string $outPath 文件存放路径
284+
* @param string $outFilename 另存文件名
284285
* @param string $thumbWidth 缩略图宽度
285286
* @param string $thumbHeight 缩略图高度
286287
* @param string $thumbType 裁切图片的方式
287288
* @return static
288289
*/
289-
public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth = '', $thumbHeight = '', $thumbType = '')
290+
public function thumbnail($img, $outPath = '', $outFilename = '', $thumbWidth = '', $thumbHeight = '', $thumbType = '')
290291
{
291292
if (!$this->_checkImage($img) || $this->hasError()) {
292293
return $this;
293294
}
294295

295296
$imgInfo = pathinfo($img);
296-
$imgType = $imgInfo['extension'];
297+
$rawImgType = $imgInfo['extension'];
297298

298299
//基础配置
299300
$thumbType = $thumbType ? : $this->thumbOptions['type'];
@@ -303,7 +304,7 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
303304

304305
//获得图像信息
305306
list($imgWidth, $imgHeight) = getimagesize($img);
306-
$imgType = $this->_handleImageType($imgType);
307+
$imgType = $this->_handleImageType($rawImgType);
307308

308309
//获得相关尺寸
309310
$thumbSize = $this->_calcThumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType);
@@ -331,7 +332,7 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
331332
}
332333

333334
//配置输出文件名
334-
$outFilename = $outFilename ?: $this->thumbOptions['prefix'] . $imgInfo['filename'] . $this->thumbOptions['suffix'] . '.' . $imgType;
335+
$outFilename = $outFilename ?: $this->thumbOptions['prefix'] . $imgInfo['filename'] . $this->thumbOptions['suffix'] . '.' . $rawImgType;
335336
$outFile = $outPath . DIRECTORY_SEPARATOR . $outFilename;
336337

337338
if ( ! Directory::create($outPath) ) {
@@ -350,15 +351,59 @@ public function thumbnail($img, $outFilename = '', $outPath = '', $thumbWidth =
350351
imagedestroy($resThumb);
351352
}
352353

353-
$this->_workingRawFile = $img;
354-
$this->_workingOutFile = $outFile;
354+
$this->working = [
355+
'raw' => $img,
356+
'out' => $outFile,
357+
];
355358

356-
$this->_result['rawFile'] = $img;
357-
$this->_result['outFile'] = $outFile;
359+
$this->_result['workThumb']['rawFile'] = $img;
360+
$this->_result['workThumb']['outFile'] = $outFile;
358361

359362
return $this;
360363
}
361364

365+
/**
366+
* 显示 image file 到浏览器
367+
* @param string $img 图片文件
368+
* @param string $type 图片格式 jpeg|png|gif
369+
* @return bool
370+
*/
371+
public static function show($img)
372+
{
373+
if ( !is_file($img) || !is_readable($img)) {
374+
throw new FileSystemException('image file don\'t exists or file is not readable!');
375+
}
376+
377+
$type = pathinfo($img, PATHINFO_EXTENSION);
378+
$type = $type === self::IMAGE_JPG ? self::IMAGE_JPEG : $type;
379+
380+
if ( !static::isSupportedType($type) ) {
381+
throw new InvalidArgumentException("image type [$type] is not supported!", 1);
382+
}
383+
384+
/** @var resource $resImg */
385+
$resImg = call_user_func("imagecreatefrom{$type}" , $img);
386+
387+
// 保持png图片的透明度
388+
if ( $type === self::IMAGE_PNG ) {
389+
// 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息。
390+
imagesavealpha($resImg, true);
391+
}
392+
393+
header('Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate');
394+
header('Content-type: image/'.$type.';charset=utf8'); // 生成图片格式 png jpeg 。。。
395+
396+
ob_clean();
397+
//生成图片,在浏览器中进行显示-格式 $type ,与上面的header声明对应
398+
// e.g. imagepng($resImg);
399+
$success = call_user_func("image{$type}" , $resImg);
400+
401+
// 已经显示图片后,可销毁,释放内存(可选)
402+
imagedestroy($resImg);
403+
404+
return $success;
405+
}
406+
362407
/*********************************************************************************
363408
* helper method
364409
*********************************************************************************/

src/files/Uploader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function makeThumb($filePath, $targetFile, &$result)
270270
$path = dirname($targetFile). '/thumb';
271271
$name = basename($targetFile);
272272

273-
$this->getPicture()->thumb($filePath, $name, $path);
273+
$this->getPicture()->thumb($filePath, $path, $name);
274274

275275
$result['thumbImage'] = $path . '/' . $name;
276276

0 commit comments

Comments
 (0)