Merge pull request 'preview' (#66) from preview into master
Reviewed-on: #66
@ -1,161 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain;
|
||||
|
||||
use Fastknife\Domain\Logic\BaseData;
|
||||
use Fastknife\Domain\Logic\BaseImage;
|
||||
use Fastknife\Domain\Logic\BlockImage;
|
||||
use Fastknife\Domain\Logic\Cache;
|
||||
use Fastknife\Domain\Logic\WordImage;
|
||||
use Fastknife\Domain\Logic\BlockData;
|
||||
use Fastknife\Domain\Logic\WordData;
|
||||
use Fastknife\Domain\Vo\ImageVo;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
|
||||
class Factory
|
||||
{
|
||||
protected $config;
|
||||
|
||||
protected $cacheInstance;
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BlockImage
|
||||
*/
|
||||
public function makeBlockImage(): BlockImage
|
||||
{
|
||||
$data = new BlockData();
|
||||
$image = new BlockImage();
|
||||
$this->setCommon($image, $data);
|
||||
$this->setBlock($image, $data);
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WordImage
|
||||
*/
|
||||
public function makeWordImage(): WordImage
|
||||
{
|
||||
$data = new WordData();
|
||||
$image = new WordImage();
|
||||
$this->setCommon($image, $data);
|
||||
$this->setWord($image, $data);
|
||||
return $image;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置公共配置
|
||||
* @param BaseImage $image
|
||||
* @param BaseData $data
|
||||
*/
|
||||
protected function setCommon(BaseImage $image, BaseData $data)
|
||||
{
|
||||
//固定驱动,少量图片处理场景gd性能远远大于imagick
|
||||
ImageManagerStatic::configure(['driver' => 'gd']);
|
||||
|
||||
//获得字体数据
|
||||
$fontFile = $data->getFontFile($this->config['font_file']);
|
||||
$image
|
||||
->setFontFile($fontFile)
|
||||
->setWatermark($this->config['watermark']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置滑动验证码的配置
|
||||
* @param BlockImage $image
|
||||
* @param BlockData $data
|
||||
*/
|
||||
protected function setBlock(BlockImage $image, BlockData $data)
|
||||
{
|
||||
//设置背景
|
||||
$backgroundVo = $data->getBackgroundVo($this->config['block_puzzle']['backgrounds']);
|
||||
$image->setBackgroundVo($backgroundVo);
|
||||
|
||||
$templateVo = $data->getTemplateVo($backgroundVo, $this->config['block_puzzle']['templates']);
|
||||
|
||||
$image->setTemplateVo($templateVo);
|
||||
|
||||
$pixelMaps = [$backgroundVo, $templateVo];
|
||||
if (
|
||||
isset($this->config['block_puzzle']['is_interfere']) &&
|
||||
$this->config['block_puzzle']['is_interfere'] == true
|
||||
) {
|
||||
$interfereVo = $data->getInterfereVo($backgroundVo, $templateVo, $this->config['block_puzzle']['templates']);
|
||||
$image->setInterfereVo($interfereVo);
|
||||
$pixelMaps[] = $interfereVo;
|
||||
}
|
||||
|
||||
if (
|
||||
isset($this->config['block_puzzle']['is_cache_pixel']) &&
|
||||
$this->config['block_puzzle']['is_cache_pixel'] === true
|
||||
) {
|
||||
$cache = $this->getCacheInstance();
|
||||
foreach ($pixelMaps as $vo) {
|
||||
/**@var ImageVo $vo * */
|
||||
$key = 'image_pixel_map_' . $vo->src;
|
||||
$result = $cache->get($key);
|
||||
if (!empty($result) && is_array($result)) {
|
||||
$vo->setPickMaps($result);
|
||||
} else {
|
||||
$vo->preparePickMaps();
|
||||
$vo->setFinishCallback(function (ImageVo $imageVo) use ($cache, $key) {
|
||||
$cache->set($key, $imageVo->getPickMaps(), 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文字验证码的配置
|
||||
* @param WordImage $image
|
||||
* @param WordData $data
|
||||
*/
|
||||
protected function setWord(WordImage $image, WordData $data)
|
||||
{
|
||||
//设置背景
|
||||
$backgroundVo = $data->getBackgroundVo($this->config['click_world']['backgrounds']);
|
||||
$image->setBackgroundVo($backgroundVo);
|
||||
|
||||
//随机文字坐标
|
||||
$pointList = $data->getPointList(
|
||||
$image->getBackgroundVo()->image->getWidth(),
|
||||
$image->getBackgroundVo()->image->getHeight(),
|
||||
3
|
||||
);
|
||||
$worldList = $data->getWordList(count($pointList));
|
||||
$image
|
||||
->setWordList($worldList)
|
||||
->setWordList($worldList)
|
||||
->setPoint($pointList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建缓存实体
|
||||
*/
|
||||
public function getCacheInstance(): Cache
|
||||
{
|
||||
if (empty($this->cacheInstance)) {
|
||||
$this->cacheInstance = new Cache($this->config['cache']);
|
||||
}
|
||||
return $this->cacheInstance;
|
||||
}
|
||||
|
||||
public function makeWordData(): WordData
|
||||
{
|
||||
return new WordData();
|
||||
}
|
||||
|
||||
public function makeBlockData(): BlockData
|
||||
{
|
||||
return (new BlockData())->setFaultOffset($this->config['block_puzzle']['offset']);
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
|
||||
use Fastknife\Domain\Vo\BackgroundVo;
|
||||
use Fastknife\Utils\RandomUtils;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
|
||||
class BaseData
|
||||
{
|
||||
public const FONTSIZE = 25;
|
||||
|
||||
protected $defaultBackgroundPath;
|
||||
|
||||
protected $defaultFontPath = '/resources/fonts/WenQuanZhengHei.ttf';
|
||||
|
||||
/**
|
||||
* 获取字体包文件
|
||||
* @param string $file
|
||||
* @return string
|
||||
*/
|
||||
public function getFontFile(string $file = ''): string
|
||||
{
|
||||
return $file && is_file($file) ?
|
||||
$file :
|
||||
dirname(__DIR__, 3) . $this->defaultFontPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得随机图片
|
||||
* @param $images
|
||||
* @return string
|
||||
*/
|
||||
protected function getRandImage($images): string
|
||||
{
|
||||
$index = RandomUtils::getRandomInt(0, count($images) - 1);
|
||||
return $images[$index];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认图片
|
||||
* @param $dir
|
||||
* @param $images
|
||||
* @return array|false
|
||||
*/
|
||||
protected function getDefaultImage($dir, $images)
|
||||
{
|
||||
if (!empty($images)) {
|
||||
if (is_array($images)) {
|
||||
return $images;
|
||||
}
|
||||
if (is_string($images)) {
|
||||
$dir = $images;
|
||||
}
|
||||
}
|
||||
return glob($dir . '*.png');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一张背景图地址
|
||||
* @param null $backgrounds 背景图库
|
||||
* @return BackgroundVo
|
||||
*/
|
||||
public function getBackgroundVo($backgrounds = null): BackgroundVo
|
||||
{
|
||||
$dir = dirname(__DIR__, 3). $this->defaultBackgroundPath;
|
||||
$backgrounds = $this->getDefaultImage($dir, $backgrounds);
|
||||
$src = $this->getRandImage($backgrounds);
|
||||
return new BackgroundVo($src);
|
||||
}
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
|
||||
use Fastknife\Domain\Vo\BackgroundVo;
|
||||
use Intervention\Image\AbstractFont as Font;
|
||||
use Intervention\Image\Image;
|
||||
|
||||
abstract class BaseImage
|
||||
{
|
||||
protected $watermark;
|
||||
|
||||
/**
|
||||
* @var BackgroundVo
|
||||
*/
|
||||
protected $backgroundVo;
|
||||
|
||||
protected $fontFile;
|
||||
protected $point;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPoint()
|
||||
{
|
||||
return $this->point;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $point
|
||||
* @return WordImage
|
||||
*/
|
||||
public function setPoint($point):self
|
||||
{
|
||||
$this->point = $point;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
protected function makeWatermark(Image $image)
|
||||
{
|
||||
if (! empty($this->watermark)) {
|
||||
$info = imagettfbbox($this->watermark['fontsize'], 0, $this->fontFile, $this->watermark['text']);
|
||||
$minX = min($info[0], $info[2], $info[4], $info[6]);
|
||||
$minY = min($info[1], $info[3], $info[5], $info[7]);
|
||||
$maxY = max($info[1], $info[3], $info[5], $info[7]);
|
||||
$x = $minX;
|
||||
$y = abs($minY);
|
||||
/* 计算文字初始坐标和尺寸 */
|
||||
$h = $maxY - $minY;
|
||||
$x += $image->getWidth() - $this->watermark['fontsize']/2; //留出半个单位字体像素的余白,不至于水印紧贴着右边
|
||||
$y += $image->getHeight() - $h;
|
||||
$image->text($this->watermark['text'], $x, $y, function (Font $font) {
|
||||
$font->file($this->fontFile);
|
||||
$font->size($this->watermark['fontsize']);
|
||||
$font->color($this->watermark['color']);
|
||||
$font->align('right');
|
||||
$font->valign('bottom');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $watermark
|
||||
* @return self
|
||||
*/
|
||||
public function setWatermark($watermark): self
|
||||
{
|
||||
$this->watermark = $watermark;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param BackgroundVo $backgroundVo
|
||||
* @return $this
|
||||
*/
|
||||
public function setBackgroundVo(BackgroundVo $backgroundVo):self
|
||||
{
|
||||
$this->backgroundVo = $backgroundVo;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BackgroundVo
|
||||
*/
|
||||
public function getBackgroundVo(): BackgroundVo
|
||||
{
|
||||
return $this->backgroundVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return static
|
||||
*/
|
||||
public function setFontFile($file): self
|
||||
{
|
||||
$this->fontFile = $file;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public abstract function run();
|
||||
}
|
@ -1,130 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
|
||||
use Fastknife\Domain\Vo\BackgroundVo;
|
||||
use Fastknife\Domain\Vo\OffsetVo;
|
||||
use Fastknife\Domain\Vo\TemplateVo;
|
||||
use Fastknife\Exception\BlockException;
|
||||
use Fastknife\Utils\RandomUtils;
|
||||
|
||||
class BlockData extends BaseData
|
||||
{
|
||||
|
||||
protected $defaultBackgroundPath = '/resources/defaultImages/jigsaw/original/';
|
||||
|
||||
protected $faultOffset;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFaultOffset()
|
||||
{
|
||||
return $this->faultOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $faultOffset
|
||||
*/
|
||||
public function setFaultOffset($faultOffset): self
|
||||
{
|
||||
$this->faultOffset = $faultOffset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取剪切模板Vo
|
||||
* @param BackgroundVo $backgroundVo
|
||||
* @param array $templates
|
||||
* @return TemplateVo
|
||||
*/
|
||||
public function getTemplateVo(BackgroundVo $backgroundVo, array $templates = []): TemplateVo
|
||||
{
|
||||
$background = $backgroundVo->image;
|
||||
//初始偏移量,让模板图在背景的右1/2位置
|
||||
$bgWidth = intval($background->getWidth() / 2);
|
||||
//随机获取一张图片
|
||||
$src = $this->getRandImage($this->getTemplateImages($templates));
|
||||
|
||||
$templateVo = new TemplateVo($src);
|
||||
|
||||
//随机获取偏移量
|
||||
$offset = RandomUtils::getRandomInt(0, $bgWidth - $templateVo->image->getWidth() - 1);
|
||||
|
||||
$templateVo->setOffset(new OffsetVo($offset + $bgWidth, 0));
|
||||
return $templateVo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getInterfereVo(BackgroundVo $backgroundVo, TemplateVo $templateVo, $templates = []): TemplateVo
|
||||
{
|
||||
//背景
|
||||
$background = $backgroundVo->image;
|
||||
//模板库去重
|
||||
$templates = $this->exclude($this->getTemplateImages($templates), $templateVo->src);
|
||||
|
||||
//随机获取一张模板图
|
||||
$src = $this->getRandImage($templates);
|
||||
|
||||
$interfereVo = new TemplateVo($src);
|
||||
|
||||
$maxOffsetX = intval($templateVo->image->getWidth()/2);
|
||||
do {
|
||||
//随机获取偏移量
|
||||
$offsetX = RandomUtils::getRandomInt(0, $background->getWidth() - $templateVo->image->getWidth() - 1);
|
||||
|
||||
//不与原模板重复
|
||||
if (
|
||||
abs($templateVo->offset->x - $offsetX) > $maxOffsetX
|
||||
) {
|
||||
$offsetVO = new OffsetVo($offsetX, 0);
|
||||
$interfereVo->setOffset($offsetVO);
|
||||
return $interfereVo;
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
|
||||
protected function getTemplateImages(array $templates = [])
|
||||
{
|
||||
$dir = dirname(__DIR__, 3) . '/resources/defaultImages/jigsaw/slidingBlock/';
|
||||
return $this->getDefaultImage($dir, $templates);
|
||||
}
|
||||
|
||||
/**
|
||||
* 排除
|
||||
* @param $templates
|
||||
* @param $exclude
|
||||
* @return array
|
||||
*/
|
||||
protected function exclude($templates, $exclude): array
|
||||
{
|
||||
if (false !== ($key = array_search($exclude, $templates))) {
|
||||
array_splice($templates,$key,1);
|
||||
}
|
||||
return $templates;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $originPoint
|
||||
* @param $targetPoint
|
||||
* @return void
|
||||
*/
|
||||
public function check($originPoint, $targetPoint)
|
||||
{
|
||||
if (
|
||||
abs($originPoint->x - $targetPoint->x) <= $this->faultOffset
|
||||
&& $originPoint->y == $targetPoint->y
|
||||
) {
|
||||
return;
|
||||
}
|
||||
throw new BlockException('验证失败!');
|
||||
}
|
||||
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
|
||||
use Fastknife\Domain\Vo\BackgroundVo;
|
||||
use Fastknife\Domain\Vo\ImageVo;
|
||||
use Fastknife\Domain\Vo\PointVo;
|
||||
use Fastknife\Domain\Vo\TemplateVo;
|
||||
|
||||
class BlockImage extends BaseImage
|
||||
{
|
||||
const WHITE = [255, 255, 255, 1];
|
||||
|
||||
/**
|
||||
* @var TemplateVo
|
||||
*/
|
||||
protected $templateVo;
|
||||
|
||||
/**
|
||||
* @var TemplateVo
|
||||
*/
|
||||
protected $interfereVo;
|
||||
|
||||
|
||||
/**
|
||||
* @return TemplateVo
|
||||
*/
|
||||
public function getTemplateVo(): TemplateVo
|
||||
{
|
||||
return $this->templateVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TemplateVo $templateVo
|
||||
* @return self
|
||||
*/
|
||||
public function setTemplateVo(TemplateVo $templateVo): self
|
||||
{
|
||||
$this->templateVo = $templateVo;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TemplateVo
|
||||
*/
|
||||
public function getInterfereVo(): TemplateVo
|
||||
{
|
||||
return $this->interfereVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TemplateVo $interfereVo
|
||||
* @return static
|
||||
*/
|
||||
public function setInterfereVo(TemplateVo $interfereVo): self
|
||||
{
|
||||
|
||||
$this->interfereVo = $interfereVo;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$flag = false;
|
||||
$this->cutByTemplate($this->templateVo, $this->backgroundVo, function ($param) use (&$flag) {
|
||||
if (!$flag) {
|
||||
//记录第一个点
|
||||
$this->setPoint(new PointVo($param[0], 5));//前端已将y值写死
|
||||
$flag = true;
|
||||
}
|
||||
});
|
||||
if (!empty($this->interfereVo)) {
|
||||
$this->cutByTemplate($this->interfereVo, $this->backgroundVo);
|
||||
}
|
||||
$this->makeWatermark($this->backgroundVo->image);
|
||||
}
|
||||
|
||||
|
||||
public function cutByTemplate(TemplateVo $templateVo, BackgroundVo $backgroundVo, $callable = null)
|
||||
{
|
||||
$template = $templateVo->image;
|
||||
$width = $template->getWidth();
|
||||
$height = $template->getHeight();
|
||||
for ($x = 0; $x < $width; $x++) {
|
||||
for ($y = 0; $y < $height; $y++) {
|
||||
//背景图对应的坐标
|
||||
$bgX = $x + $templateVo->offset->x;
|
||||
$bgY = $y + $templateVo->offset->y;
|
||||
//是否不透明
|
||||
$isOpacity = $templateVo->isOpacity($x, $y);
|
||||
if ($isOpacity) { //如果不透明
|
||||
if ($callable instanceof \Closure) {
|
||||
$callable([$bgX, $bgY]);
|
||||
}
|
||||
$backgroundVo->vagueImage($bgX, $bgY);//模糊背景图选区
|
||||
|
||||
$this->copyPickColor($backgroundVo, $bgX, $bgY, $templateVo, $x, $y);
|
||||
}
|
||||
if ($templateVo->isBoundary($isOpacity, $x, $y)) {
|
||||
$backgroundVo->setPixel(self::WHITE, $bgX, $bgY);
|
||||
$templateVo->setPixel(self::WHITE, $x, $y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 把$source的颜色复制到$target上
|
||||
* @param ImageVo $source
|
||||
* @param ImageVo $target
|
||||
*/
|
||||
|
||||
protected function copyPickColor(ImageVo $source, $sourceX, $sourceY, ImageVo $target, $targetX, $targetY)
|
||||
{
|
||||
$bgRgba = $source->getPickColor($sourceX, $sourceY);
|
||||
$target->setPixel($bgRgba, $targetX, $targetY);//复制背景图片给模板
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回前端需要的格式
|
||||
* @return false|string[]
|
||||
*/
|
||||
public function response($type = 'background')
|
||||
{
|
||||
$image = $type == 'background' ? $this->backgroundVo->image : $this->templateVo->image;
|
||||
$result = $image->encode('data-url')->getEncoded();
|
||||
//返回图片base64的第二部分
|
||||
return explode(',', $result)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 用来调试
|
||||
*/
|
||||
public function echo($type = 'background')
|
||||
{
|
||||
$image = $type == 'background' ? $this->backgroundVo->image : $this->templateVo->image;
|
||||
die($image->response());
|
||||
}
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
use Fastknife\Utils\CacheUtils;
|
||||
|
||||
class Cache
|
||||
{
|
||||
protected $config;
|
||||
|
||||
protected $driver;
|
||||
|
||||
protected $methodMap = [
|
||||
'get' => 'get',
|
||||
'set' => 'set',
|
||||
'delete' => 'delete',
|
||||
'has' => 'has'
|
||||
];
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
if (isset($config['method'])) {
|
||||
$this->methodMap = array_merge($this->methodMap, $config['method']);
|
||||
}
|
||||
$this->driver = $this->getDriver($config['constructor'], $config['options']??[]);
|
||||
}
|
||||
|
||||
public function getDriver($callback, $options)
|
||||
{
|
||||
if ($callback instanceof \Closure) {
|
||||
$result = $callback($options);
|
||||
} else if (is_object($callback)) {
|
||||
$result = $callback;
|
||||
} else if (is_array($callback)) {
|
||||
$result = call_user_func($callback, $options);
|
||||
} else if ($this->isSerialized($callback)) {
|
||||
$result = unserialize($callback);
|
||||
} else if (is_string($callback) && class_exists($callback)) {
|
||||
$result = new $callback($options);
|
||||
} else {
|
||||
$result = new CacheUtils($options);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否可以被反序列化
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
public function isSerialized($data): bool
|
||||
{
|
||||
if (!is_string($data)) {
|
||||
return false;
|
||||
}
|
||||
$data = trim($data);
|
||||
if ('N;' == $data) {
|
||||
return true;
|
||||
}
|
||||
if (!preg_match('/^([adObis]):/', $data, $badions)) {
|
||||
return false;
|
||||
}
|
||||
switch ($badions[1]) {
|
||||
case 'a' :
|
||||
case 'O' :
|
||||
case 's' :
|
||||
if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data))
|
||||
return true;
|
||||
break;
|
||||
case 'b' :
|
||||
case 'i' :
|
||||
case 'd' :
|
||||
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data))
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getDriverMethod($name)
|
||||
{
|
||||
return $this->methodMap[$name];
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$method = $this->getDriverMethod('get');
|
||||
return $this->execute($method, [$key,$default]);
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = null)
|
||||
{
|
||||
$method = $this->getDriverMethod('set');
|
||||
return $this->execute($method, [$key, $value, $ttl]);
|
||||
}
|
||||
|
||||
public function delete($key)
|
||||
{
|
||||
$method = $this->getDriverMethod('delete');
|
||||
return $this->execute($method, [$key]);
|
||||
}
|
||||
|
||||
public function has($key)
|
||||
{
|
||||
$method = $this->getDriverMethod('has');
|
||||
return $this->execute($method, [$key]);
|
||||
}
|
||||
|
||||
protected function execute(string $method, array $params){
|
||||
return $this->driver->$method(...$params);
|
||||
}
|
||||
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
use Fastknife\Domain\Vo\PointVo;
|
||||
use Fastknife\Exception\WordException;
|
||||
use Fastknife\Utils\RandomUtils;
|
||||
|
||||
/**
|
||||
* 文字码数据处理
|
||||
* Class WordDataEntity
|
||||
* @package Fastknife\Domain\Entity
|
||||
*/
|
||||
class WordData extends BaseData
|
||||
{
|
||||
protected $defaultBackgroundPath = '/resources/defaultImages/pic-click/';
|
||||
|
||||
|
||||
/**
|
||||
* @param $width
|
||||
* @param $height
|
||||
* @param $index
|
||||
* @param $wordCount
|
||||
* @return PointVo
|
||||
*/
|
||||
protected function getPoint($width, $height, $index, $wordCount)
|
||||
{
|
||||
$avgWidth = $width / ($wordCount + 1);
|
||||
if ($avgWidth < self::FONTSIZE) {
|
||||
$x = RandomUtils::getRandomInt(1 + self::FONTSIZE, $width);
|
||||
} else {
|
||||
if ($index == 0) {
|
||||
$x = RandomUtils::getRandomInt(1 + self::FONTSIZE, $avgWidth * ($index + 1) - self::FONTSIZE);
|
||||
} else {
|
||||
$x = RandomUtils::getRandomInt($avgWidth * $index + self::FONTSIZE, $avgWidth * ($index + 1) - self::FONTSIZE);
|
||||
}
|
||||
}
|
||||
$y = RandomUtils::getRandomInt(self::FONTSIZE, $height - self::FONTSIZE);
|
||||
return new PointVo($x, $y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $width
|
||||
* @param $height
|
||||
* @param int $number
|
||||
* @return array
|
||||
*/
|
||||
public function getPointList($width, $height, $number = 3): array
|
||||
{
|
||||
$pointList = [];
|
||||
for ($i = 0; $i < $number; $i++) {
|
||||
$pointList[] = $this->getPoint($width, $height, $i, $number);
|
||||
}
|
||||
shuffle($pointList); //随机排序
|
||||
return $pointList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $list
|
||||
* @return array
|
||||
*/
|
||||
public function array2Point($list): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($list as $item) {
|
||||
$result[] = new PointVo($item['x'], $item['y']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getWordList($number): array
|
||||
{
|
||||
return RandomUtils::getRandomChar($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验
|
||||
* @param array $originPointList
|
||||
* @param array $targetPointList
|
||||
*/
|
||||
public function check(array $originPointList, array $targetPointList)
|
||||
{
|
||||
foreach ($originPointList as $key => $originPoint) {
|
||||
$targetPoint = $targetPointList[$key];
|
||||
if ($targetPoint->x - self::FONTSIZE > $originPoint->x
|
||||
|| $targetPoint->x > $originPoint->x + self::FONTSIZE
|
||||
|| $targetPoint->y - self::FONTSIZE > $originPoint->y
|
||||
|| $targetPoint->y > $originPoint->y + self::FONTSIZE) {
|
||||
throw new WordException('验证失败!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Logic;
|
||||
|
||||
use Fastknife\Domain\Vo\PointVo;
|
||||
use Intervention\Image\ImageManagerStatic as ImageManager;
|
||||
use Intervention\Image\AbstractFont as Font;
|
||||
use Fastknife\Utils\RandomUtils;
|
||||
|
||||
/**
|
||||
* 文字码图片处理
|
||||
* Class WordCaptchaEntity
|
||||
* @package Fastknife\Domain\Entity
|
||||
*/
|
||||
class WordImage extends BaseImage
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $wordList;
|
||||
|
||||
|
||||
/**
|
||||
* @return self
|
||||
*/
|
||||
public function setWordList(array $wordList)
|
||||
{
|
||||
$this->wordList = $wordList;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWordList()
|
||||
{
|
||||
return $this->wordList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->inputWords();
|
||||
$this->makeWatermark($this->backgroundVo->image);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入文字
|
||||
*/
|
||||
protected function inputWords(){
|
||||
foreach ($this->wordList as $key => $word) {
|
||||
$point = $this->point[$key];
|
||||
$this->backgroundVo->image->text($word, $point->x, $point->y, function (Font $font) {
|
||||
$font->file($this->fontFile);
|
||||
$font->size(BaseData::FONTSIZE);
|
||||
$font->color(RandomUtils::getRandomColor());
|
||||
$font->angle(RandomUtils::getRandomAngle());
|
||||
$font->align('center');
|
||||
$font->valign('center');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回前端需要的格式
|
||||
* @return false|string[]
|
||||
*/
|
||||
public function response()
|
||||
{
|
||||
$result = $this->getBackgroundVo()->image->encode('data-url')->getEncoded();
|
||||
//返回图片base64的第二部分
|
||||
return explode(',', $result)[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 用来调试
|
||||
*/
|
||||
public function echo()
|
||||
{
|
||||
die($this->getBackgroundVo()->image->response());
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Fastknife\Domain\Vo;
|
||||
|
||||
class BackgroundVo extends ImageVo
|
||||
{
|
||||
|
||||
}
|
@ -1,179 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Fastknife\Domain\Vo;
|
||||
|
||||
use Fastknife\Utils\MathUtils;
|
||||
use Intervention\Image\Image;
|
||||
use Intervention\Image\ImageManagerStatic;
|
||||
|
||||
abstract class ImageVo
|
||||
{
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
public $image;
|
||||
|
||||
public $src;
|
||||
|
||||
private $pickMaps = [];
|
||||
|
||||
private $finishCallback;
|
||||
|
||||
public function __construct($src)
|
||||
{
|
||||
$this->src = $src;
|
||||
$this->initImage($src);
|
||||
}
|
||||
|
||||
public function initImage($src)
|
||||
{
|
||||
$this->image = ImageManagerStatic::make($src);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片中某一个位置的rgba值
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @return array
|
||||
*/
|
||||
public function getPickColor($x, $y): array
|
||||
{
|
||||
if (!isset($this->pickMaps[$x][$y])) {
|
||||
$this->pickMaps[$x][$y] = $this->image->pickColor($x, $y);
|
||||
}
|
||||
return $this->pickMaps[$x][$y];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置图片指定位置的颜色值
|
||||
*/
|
||||
public function setPixel($color, $x, $y)
|
||||
{
|
||||
$this->image->pixel($color, $x, $y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
* @return array
|
||||
*/
|
||||
public function getBlurValue(int $x, int $y): array
|
||||
{
|
||||
$image = $this->image;
|
||||
$red = [];
|
||||
$green = [];
|
||||
$blue = [];
|
||||
$alpha = [];
|
||||
foreach ([
|
||||
[0, 1], [0, -1],
|
||||
[1, 0], [-1, 0],
|
||||
[1, 1], [1, -1],
|
||||
[-1, 1], [-1, -1],
|
||||
] as $distance) //边框取5个点,4个角取3个点,其余取8个点
|
||||
{
|
||||
$pointX = $x + $distance[0];
|
||||
$pointY = $y + $distance[1];
|
||||
if ($pointX < 0 || $pointX >= $image->getWidth() || $pointY < 0 || $pointY >= $image->height()) {
|
||||
continue;
|
||||
}
|
||||
[$r, $g, $b, $a] = $this->getPickColor($pointX, $pointY);
|
||||
$red[] = $r;
|
||||
$green[] = $g;
|
||||
$blue[] = $b;
|
||||
$alpha[] = $a;
|
||||
}
|
||||
return [MathUtils::avg($red), MathUtils::avg($green), MathUtils::avg($blue), MathUtils::avg($alpha)];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 是否不透明
|
||||
* @param $x
|
||||
* @param $y
|
||||
* @return bool
|
||||
*/
|
||||
public function isOpacity($x, $y): bool
|
||||
{
|
||||
return $this->getPickColor($x, $y)[3] > 0.5;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为边框
|
||||
* @param bool $isOpacity
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
* @return bool
|
||||
*/
|
||||
public function isBoundary(bool $isOpacity, int $x, int $y): bool
|
||||
{
|
||||
$image = $this->image;
|
||||
if ($x >= $image->width() - 1 || $y >= $image->height() - 1) {
|
||||
return false;
|
||||
}
|
||||
$right = [$x + 1, $y];
|
||||
$down = [$x, $y + 1];
|
||||
if (
|
||||
$isOpacity && !$this->isOpacity(...$right)
|
||||
|| !$isOpacity && $this->isOpacity(...$right)
|
||||
|| $isOpacity && !$this->isOpacity(...$down)
|
||||
|| !$isOpacity && $this->isOpacity(...$down)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模糊图片
|
||||
* @param $targetX
|
||||
* @param $targetY
|
||||
*/
|
||||
public function vagueImage($targetX, $targetY)
|
||||
{
|
||||
$blur = $this->getBlurValue($targetX, $targetY);
|
||||
$this->setPixel($blur, $targetX, $targetY);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getPickMaps(): array
|
||||
{
|
||||
return $this->pickMaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $pickMaps
|
||||
*/
|
||||
public function setPickMaps(array $pickMaps): void
|
||||
{
|
||||
$this->pickMaps = $pickMaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提前初始化像素
|
||||
*/
|
||||
public function preparePickMaps()
|
||||
{
|
||||
$width = $this->image->getWidth();
|
||||
$height = $this->image->getHeight();
|
||||
for ($x = 0; $x < $width; $x++) {
|
||||
for ($y = 0; $y < $height; $y++) {
|
||||
$this->getPickColor($x, $y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setFinishCallback($finishCallback){
|
||||
$this->finishCallback = $finishCallback;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if(!empty($this->finishCallback) && $this->finishCallback instanceof \Closure){
|
||||
($this->finishCallback)($this);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Vo;
|
||||
|
||||
|
||||
class OffsetVo
|
||||
{
|
||||
public $x;
|
||||
public $y;
|
||||
|
||||
public function __construct($x, $y)
|
||||
{
|
||||
$this->x = $x;
|
||||
$this->y = $y;
|
||||
}
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Vo;
|
||||
|
||||
|
||||
class PointVo
|
||||
{
|
||||
public $x;
|
||||
public $y;
|
||||
|
||||
/**
|
||||
* PointDto constructor.
|
||||
* @param $x
|
||||
* @param $y
|
||||
*/
|
||||
public function __construct($x, $y)
|
||||
{
|
||||
$this->x = $x;
|
||||
$this->y = $y;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Domain\Vo;
|
||||
use Intervention\Image\Image;
|
||||
class TemplateVo extends ImageVo
|
||||
{
|
||||
/**
|
||||
* @var OffsetVo
|
||||
*/
|
||||
public $offset;
|
||||
|
||||
|
||||
/**
|
||||
* @return OffsetVo
|
||||
*/
|
||||
public function getOffset(): OffsetVo
|
||||
{
|
||||
return $this->offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OffsetVo $offset
|
||||
*/
|
||||
public function setOffset(OffsetVo $offset): void
|
||||
{
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Exception;
|
||||
|
||||
|
||||
class BlockException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Exception;
|
||||
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
class ParamException extends RuntimeException
|
||||
{
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Exception;
|
||||
|
||||
|
||||
class WordException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Service;
|
||||
|
||||
use Fastknife\Domain\Vo\PointVo;
|
||||
use Fastknife\Utils\RandomUtils;
|
||||
|
||||
class BlockPuzzleCaptchaService extends Service
|
||||
{
|
||||
/**
|
||||
* 获取验证码图片信息
|
||||
* @return array
|
||||
*/
|
||||
public function get(): array
|
||||
{
|
||||
$cacheEntity = $this->factory->getCacheInstance();
|
||||
$blockImage = $this->factory->makeBlockImage();
|
||||
$blockImage->run();
|
||||
$data = [
|
||||
'originalImageBase64' => $blockImage->response(),
|
||||
'jigsawImageBase64' => $blockImage->response('template'),
|
||||
'secretKey' => RandomUtils::getRandomCode(16, 3),
|
||||
'token' => RandomUtils::getUUID(),
|
||||
];
|
||||
//缓存
|
||||
$cacheEntity->set($data['token'], [
|
||||
'secretKey' => $data['secretKey'],
|
||||
'point' => $blockImage->getPoint()
|
||||
], 7200);
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 验证
|
||||
* @param string $token
|
||||
* @param string $pointJson
|
||||
* @param null $callback
|
||||
*/
|
||||
public function validate( $token, $pointJson, $callback = null)
|
||||
{
|
||||
//获取并设置 $this->originData
|
||||
$this->setOriginData($token);
|
||||
|
||||
//数据处理类
|
||||
$blockData = $this->factory->makeBlockData();
|
||||
|
||||
//解码出来的前端坐标
|
||||
$targetPoint = $this->encodePoint($this->originData['secretKey'], $pointJson);;
|
||||
$targetPoint = new PointVo($targetPoint['x'], $targetPoint['y']);
|
||||
|
||||
//检查
|
||||
$blockData->check($this->originData['point'], $targetPoint);
|
||||
if($callback instanceof \Closure){
|
||||
$callback();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Service;
|
||||
|
||||
use Fastknife\Exception\ParamException;
|
||||
use Fastknife\Utils\AesUtils;
|
||||
use Fastknife\Utils\RandomUtils;
|
||||
|
||||
class ClickWordCaptchaService extends Service
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 获取文字验证码
|
||||
*/
|
||||
public function get(): array
|
||||
{
|
||||
$cacheEntity = $this->factory->getCacheInstance();
|
||||
$wordImage = $this->factory->makeWordImage();
|
||||
//执行创建
|
||||
$wordImage->run();
|
||||
$data = [
|
||||
'originalImageBase64' => $wordImage->response(),
|
||||
'secretKey' => RandomUtils::getRandomCode(16, 3),
|
||||
'token' => RandomUtils::getUUID(),
|
||||
'wordList' => $wordImage->getWordList()
|
||||
];
|
||||
//缓存
|
||||
$cacheEntity->set($data['token'], [
|
||||
'secretKey' => $data['secretKey'],
|
||||
'point' => $wordImage->getPoint()
|
||||
],7200);
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证
|
||||
* @param $token
|
||||
* @param $pointJson
|
||||
* @param null $callback
|
||||
*/
|
||||
public function validate($token, $pointJson, $callback = null)
|
||||
{
|
||||
//获取并设置 $this->originData
|
||||
$this->setOriginData($token);
|
||||
|
||||
//数据实例
|
||||
$wordData = $this->factory->makeWordData();
|
||||
//解码出来的前端坐标
|
||||
$pointJson = $this->encodePoint($this->originData['secretKey'], $pointJson);
|
||||
$targetPointList = $wordData->array2Point($pointJson);
|
||||
|
||||
//检查
|
||||
$wordData->check($this->originData['point'], $targetPointList);
|
||||
if ($callback instanceof \Closure) {
|
||||
$callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Service;
|
||||
|
||||
|
||||
use Fastknife\Domain\Factory;
|
||||
use Fastknife\Exception\ParamException;
|
||||
use Fastknife\Utils\AesUtils;
|
||||
|
||||
abstract class Service
|
||||
{
|
||||
/**
|
||||
* @var Factory 工厂
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
protected $originData;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$defaultConfig = require dirname(__DIR__) . '/config.php';
|
||||
$config = array_merge($defaultConfig, $config);
|
||||
$this->factory = new Factory($config);
|
||||
}
|
||||
|
||||
abstract public function get();
|
||||
|
||||
|
||||
abstract public function validate( $token, $pointJson, $callback = null);
|
||||
|
||||
/**
|
||||
* 一次验证
|
||||
* @param $token
|
||||
* @param $pointJson
|
||||
*/
|
||||
public function check($token, $pointJson)
|
||||
{
|
||||
$this->validate($token, $pointJson, function () use ($token, $pointJson) {
|
||||
$this->setEncryptCache($token, $pointJson);
|
||||
});
|
||||
}
|
||||
|
||||
private function setEncryptCache($token, $pointJson){
|
||||
$cacheEntity = $this->factory->getCacheInstance();
|
||||
$pointStr = AesUtils::decrypt($pointJson, $this->originData['secretKey']);
|
||||
$key = AesUtils::encrypt($token. '---'. $pointStr, $this->originData['secretKey']);
|
||||
$cacheEntity->set($key,
|
||||
[
|
||||
'token' => $token,
|
||||
'point' => $pointJson
|
||||
],
|
||||
600
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 二次验证
|
||||
* @param $token
|
||||
* @param $pointJson
|
||||
*/
|
||||
public function verification($token, $pointJson)
|
||||
{
|
||||
$this->validate($token, $pointJson, function () use ($token) {
|
||||
$cacheEntity = $this->factory->getCacheInstance();
|
||||
$cacheEntity->delete($token);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 二次验证,必须要先执行一次验证才能调用二次验证
|
||||
* 兼容前端 captchaVerification 传值
|
||||
* @param string $encryptCode
|
||||
*/
|
||||
public function verificationByEncryptCode(string $encryptCode)
|
||||
{
|
||||
$result = $this->factory->getCacheInstance()->get($encryptCode);
|
||||
if(empty($result)){
|
||||
throw new ParamException('参数错误!');
|
||||
}
|
||||
|
||||
$this->validate($result['token'], $result['point'], function () use ($result,$encryptCode) {
|
||||
$cacheEntity = $this->factory->getCacheInstance();
|
||||
$cacheEntity->delete($result['token']);
|
||||
$cacheEntity->delete($encryptCode);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码坐标点
|
||||
* @param $secretKey
|
||||
* @param $point
|
||||
* @return array
|
||||
*/
|
||||
protected function encodePoint($secretKey, $point): array
|
||||
{
|
||||
$pointJson = AesUtils::decrypt($point, $secretKey);
|
||||
if ($pointJson == false) {
|
||||
throw new ParamException('aes验签失败!');
|
||||
}
|
||||
return json_decode($pointJson, true);
|
||||
}
|
||||
|
||||
protected function setOriginData($token){
|
||||
$cacheEntity = $this->factory->getCacheInstance();
|
||||
$this->originData = $cacheEntity->get($token);
|
||||
if (empty($this->originData)) {
|
||||
throw new ParamException('参数校验失败:token');
|
||||
}
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Utils;
|
||||
|
||||
/**
|
||||
* Class AesUtils
|
||||
* @package Fastknife\Utils
|
||||
*/
|
||||
class AesUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $str
|
||||
* @param $secretKey string 只有长度等于16位才能与前端CryptoJS加密一致
|
||||
* @return string
|
||||
*/
|
||||
public static function encrypt($str, string $secretKey)
|
||||
{
|
||||
return base64_encode(openssl_encrypt($str, 'AES-128-ECB', $secretKey, OPENSSL_RAW_DATA));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param $str
|
||||
* @param $secretKey string 只有长度等于16位才能与前端CryptoJS加密一致
|
||||
* @return string
|
||||
*/
|
||||
public static function decrypt($str, string $secretKey): string
|
||||
{
|
||||
$ret = openssl_decrypt(base64_decode($str), 'AES-128-ECB', $secretKey,OPENSSL_RAW_DATA);
|
||||
if($ret === false){
|
||||
throw new \RuntimeException('请检查密钥是否正确!');
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
}
|
@ -1,331 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Utils;
|
||||
|
||||
|
||||
class CacheUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* 配置参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'expire' => 300,
|
||||
'cache_subdir' => true,
|
||||
'prefix' => '',
|
||||
'path' => '',
|
||||
'hash_type' => 'md5',
|
||||
'data_compress' => false,
|
||||
'tag_prefix' => 'tag:',
|
||||
'serialize' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* 缓存读取次数
|
||||
* @var integer
|
||||
*/
|
||||
protected $readTimes = 0;
|
||||
|
||||
/**
|
||||
* 缓存写入次数
|
||||
* @var integer
|
||||
*/
|
||||
protected $writeTimes = 0;
|
||||
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @param array $options 参数
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
|
||||
if (empty($this->options['path'])) {
|
||||
$root = isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT']) ?$_SERVER['DOCUMENT_ROOT'] : getcwd();
|
||||
$this->options['path'] = $root. '/runtime/cache';
|
||||
}
|
||||
|
||||
if (substr($this->options['path'], -1) != DIRECTORY_SEPARATOR) {
|
||||
$this->options['path'] .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得变量的存储文件名
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheKey(string $name): string
|
||||
{
|
||||
$name = hash($this->options['hash_type'], $name);
|
||||
|
||||
if ($this->options['cache_subdir']) {
|
||||
// 使用子目录
|
||||
$name = substr($name, 0, 2) . DIRECTORY_SEPARATOR . substr($name, 2);
|
||||
}
|
||||
|
||||
if ($this->options['prefix']) {
|
||||
$name = $this->options['prefix'] . DIRECTORY_SEPARATOR . $name;
|
||||
}
|
||||
|
||||
return $this->options['path'] . $name . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存数据
|
||||
* @param string $name 缓存标识名
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getRaw(string $name)
|
||||
{
|
||||
$filename = $this->getCacheKey($name);
|
||||
|
||||
if (!is_file($filename)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = @file_get_contents($filename);
|
||||
|
||||
if (false !== $content) {
|
||||
$expire = (int) substr($content, 8, 12);
|
||||
if (0 != $expire && time() - $expire > filemtime($filename)) {
|
||||
//缓存过期删除缓存文件
|
||||
$this->unlink($filename);
|
||||
return;
|
||||
}
|
||||
|
||||
$content = substr($content, 32);
|
||||
|
||||
return is_string($content) ? ['content' => $content, 'expire' => $expire] : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存是否存在
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function has($name): bool
|
||||
{
|
||||
return $this->getRaw($name) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $default = null)
|
||||
{
|
||||
$this->readTimes++;
|
||||
|
||||
$raw = $this->getRaw($name);
|
||||
|
||||
return is_null($raw) ? $default : $this->unserialize($raw['content']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param mixed $value 存储数据
|
||||
* @param int $expire 有效时间 0为永久
|
||||
* @return bool
|
||||
*/
|
||||
public function set($name, $value, $expire = null): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
if (is_null($expire)) {
|
||||
$expire = $this->options['expire'];
|
||||
}
|
||||
|
||||
$filename = $this->getCacheKey($name);
|
||||
|
||||
$dir = dirname($filename);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
try {
|
||||
mkdir($dir, 0755, true);
|
||||
} catch (\Exception $e) {
|
||||
// 创建失败
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->serialize($value);
|
||||
|
||||
if ($this->options['data_compress'] && function_exists('gzcompress')) {
|
||||
//数据压缩
|
||||
$data = gzcompress($data, 3);
|
||||
}
|
||||
|
||||
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data;
|
||||
$result = file_put_contents($filename, $data);
|
||||
|
||||
if ($result) {
|
||||
clearstatcache();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function inc(string $name, int $step = 1)
|
||||
{
|
||||
if ($raw = $this->getRaw($name)) {
|
||||
$value = $this->unserialize($raw['content']) + $step;
|
||||
$expire = $raw['expire'];
|
||||
} else {
|
||||
$value = $step;
|
||||
$expire = 0;
|
||||
}
|
||||
|
||||
return $this->set($name, $value, $expire) ? $value : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减缓存(针对数值缓存)
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @param int $step 步长
|
||||
* @return false|int
|
||||
*/
|
||||
public function dec(string $name, int $step = 1)
|
||||
{
|
||||
return $this->inc($name, -$step);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @access public
|
||||
* @param string $name 缓存变量名
|
||||
* @return bool
|
||||
*/
|
||||
public function delete($name): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
return $this->unlink($this->getCacheKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function clear(): bool
|
||||
{
|
||||
$this->writeTimes++;
|
||||
|
||||
$dirname = $this->options['path'] . $this->options['prefix'];
|
||||
|
||||
$this->rmdir($dirname);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存标签
|
||||
* @access public
|
||||
* @param array $keys 缓存标识列表
|
||||
* @return void
|
||||
*/
|
||||
public function clearTag(array $keys): void
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->unlink($key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否存在后,删除
|
||||
* @access private
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
private function unlink(string $path): bool
|
||||
{
|
||||
try {
|
||||
return is_file($path) && unlink($path);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹
|
||||
* @param $dirname
|
||||
* @return bool
|
||||
*/
|
||||
private function rmdir($dirname)
|
||||
{
|
||||
if (!is_dir($dirname)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$items = new \FilesystemIterator($dirname);
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDir() && !$item->isLink()) {
|
||||
$this->rmdir($item->getPathname());
|
||||
} else {
|
||||
$this->unlink($item->getPathname());
|
||||
}
|
||||
}
|
||||
|
||||
@rmdir($dirname);
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 序列化数据
|
||||
* @access protected
|
||||
* @param mixed $data 缓存数据
|
||||
* @return string
|
||||
*/
|
||||
protected function serialize($data): string
|
||||
{
|
||||
if (is_numeric($data)) {
|
||||
return (string) $data;
|
||||
}
|
||||
|
||||
$serialize = $this->options['serialize'][0] ?? "serialize";
|
||||
|
||||
return $serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化数据
|
||||
* @access protected
|
||||
* @param string $data 缓存数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function unserialize(string $data)
|
||||
{
|
||||
if (is_numeric($data)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$unserialize = $this->options['serialize'][1] ?? "unserialize";
|
||||
|
||||
return $unserialize($data);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Fastknife\Utils;
|
||||
|
||||
class MathUtils
|
||||
{
|
||||
/**
|
||||
* 获取平均值
|
||||
* @param array $array
|
||||
* @return int
|
||||
*/
|
||||
public static function avg(array $array): int
|
||||
{
|
||||
return intval(array_sum($array) / count($array));
|
||||
}
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Fastknife\Utils;
|
||||
|
||||
|
||||
class RandomUtils
|
||||
{
|
||||
/**
|
||||
* 获取随机数
|
||||
* @param $min
|
||||
* @param $max
|
||||
* @return int
|
||||
*/
|
||||
public static function getRandomInt($min, $max): int
|
||||
{
|
||||
try {
|
||||
return random_int(intval($min), intval($max));
|
||||
}catch (\Exception $e){
|
||||
return mt_rand($min, $max);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机获取眼色值
|
||||
* @return array
|
||||
*/
|
||||
public static function getRandomColor(): array
|
||||
{
|
||||
return [self::getRandomInt(1, 255), self::getRandomInt(1, 255), self::getRandomInt(1, 255)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机获取角度
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
* @return int
|
||||
*/
|
||||
public static function getRandomAngle(int $start = -45, int $end = 45): int
|
||||
{
|
||||
return self::getRandomInt($start, $end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机获取汉字
|
||||
* @param $num int 生成汉字的数量
|
||||
* @return array
|
||||
*/
|
||||
public static function getRandomChar(int $num): array
|
||||
{
|
||||
$b = [];
|
||||
for ($i=0; $i<$num; $i++) {
|
||||
// 使用chr()函数拼接双字节汉字,前一个chr()为高位字节,后一个为低位字节
|
||||
$a = chr(self::getRandomInt(0xB0,0xD0)).chr(self::getRandomInt(0xA1, 0xF0));
|
||||
// 转码
|
||||
$h = iconv('GB2312', 'UTF-8', $a);
|
||||
if(!in_array($h, $b)){
|
||||
$b[] = $h;
|
||||
}else{
|
||||
$i--; //去重
|
||||
}
|
||||
}
|
||||
return $b;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 类似java一样的uuid
|
||||
* @param string $prefix
|
||||
* @return string
|
||||
*/
|
||||
public static function getUUID(string $prefix = ''): string
|
||||
{
|
||||
$chars = md5(uniqid((string) self::getRandomInt(1, 100), true));
|
||||
$uuid = substr($chars,0,8) . '-';
|
||||
$uuid .= substr($chars,8,4) . '-';
|
||||
$uuid .= substr($chars,12,4) . '-';
|
||||
$uuid .= substr($chars,16,4) . '-';
|
||||
$uuid .= substr($chars,20,12);
|
||||
return $prefix . $uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机字符串编码
|
||||
* @param integer $length 字符串长度
|
||||
* @param integer $type 字符串类型(1纯数字,2纯字母,3数字字母)
|
||||
* @return string
|
||||
*/
|
||||
public static function getRandomCode(int $length = 10, int $type = 1): string
|
||||
{
|
||||
$numbs = '0123456789';
|
||||
$chars = "abcdefghilkmnopqrstuvwxyz";
|
||||
$maps = '';
|
||||
if ($type === 1){
|
||||
$maps = $numbs;
|
||||
}
|
||||
if ($type === 2){
|
||||
$maps = $chars;
|
||||
}
|
||||
if ($type === 3){
|
||||
$maps = "{$numbs}{$chars}";
|
||||
}
|
||||
$string = $maps[self::getRandomInt(1, strlen($maps) - 1)];
|
||||
while (strlen($string) < $length) {
|
||||
$string .= $maps[self::getRandomInt(0, strlen($maps) - 1)];
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
return [
|
||||
'font_file' => '', //自定义字体包路径, 不填使用默认值
|
||||
//文字验证码
|
||||
'click_world' => [
|
||||
'backgrounds' => []
|
||||
],
|
||||
//滑动验证码
|
||||
'block_puzzle' => [
|
||||
/*背景图片路径, 不填使用默认值, 支持string与array两种数据结构。string为默认图片的目录,array索引数组则为具体图片的地址*/
|
||||
'backgrounds' => [],
|
||||
|
||||
/*模板图,格式同上支持string与array*/
|
||||
'templates' => [],
|
||||
|
||||
'offset' => 10, //容错偏移量
|
||||
|
||||
'is_cache_pixel' => true, //是否开启缓存图片像素值,开启后能提升服务端响应性能(但要注意更换图片时,需要清除缓存)
|
||||
|
||||
'is_interfere' => true, //开启干扰图
|
||||
],
|
||||
//水印
|
||||
'watermark' => [
|
||||
'fontsize' => 12,
|
||||
'color' => '#000000',
|
||||
'text' => '我的水印'
|
||||
],
|
||||
'cache' => [
|
||||
//若您使用了框架,并且想使用类似于redis这样的缓存驱动,则应换成框架的中的缓存驱动
|
||||
'constructor' => \Fastknife\Utils\CacheUtils::class,
|
||||
'method' => [
|
||||
//遵守PSR-16规范不需要设置此项(tp6, laravel,hyperf)。如tp5就不支持(tp5缓存方法是rm,所以要配置为"delete" => "rm")
|
||||
/**
|
||||
'get' => 'get', //获取
|
||||
'set' => 'set', //设置
|
||||
'delete' => 'delete',//删除
|
||||
'has' => 'has' //key是否存在
|
||||
*/
|
||||
],
|
||||
'options' => [
|
||||
//如果您依然使用\Fastknife\Utils\CacheUtils做为您的缓存驱动,那么您可以自定义缓存配置。
|
||||
'expire' => 300,//缓存有效期 (默认为0 表示永久缓存)
|
||||
'prefix' => '', //缓存前缀
|
||||
'path' => '', //缓存目录
|
||||
'serialize' => [], //缓存序列化和反序列化方法
|
||||
]
|
||||
]
|
||||
];
|
Before Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 82 KiB |
Before Width: | Height: | Size: 77 KiB |
Before Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 7.7 KiB |
Before Width: | Height: | Size: 7.8 KiB |
Before Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 70 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 60 KiB |
Before Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 66 KiB |
Before Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 59 KiB |
Before Width: | Height: | Size: 70 KiB |
Before Width: | Height: | Size: 82 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 8.1 KiB |
Before Width: | Height: | Size: 58 KiB |
@ -1,55 +0,0 @@
|
||||
文泉驿是一个开源汉字字体项目
|
||||
|
||||
由旅美学者房骞骞(FangQ)
|
||||
|
||||
于2004年10月创建
|
||||
|
||||
集中力量解决GNU/Linux
|
||||
|
||||
高质量中文字体匮乏的状况
|
||||
|
||||
目前,文泉驿已经开发并发布了
|
||||
|
||||
第一个完整覆盖GB18030汉字
|
||||
|
||||
(包含27000多个汉字)
|
||||
|
||||
的多规格点阵汉字字型文件
|
||||
|
||||
第一个覆盖GBK字符集的
|
||||
|
||||
开源矢量字型文件(文泉驿正黑)
|
||||
|
||||
并提供了目前包含字符数目最多的
|
||||
|
||||
开源字体——GNU Unifont——中
|
||||
|
||||
绝大多数中日韩文相关的符号
|
||||
|
||||
这些字型文件已经逐渐成为
|
||||
|
||||
主流Linux/Unix发行版
|
||||
|
||||
中文桌面的首选中文字体
|
||||
|
||||
目前Ubuntu、Fedora、Slackware
|
||||
|
||||
Magic Linux、CDLinux
|
||||
|
||||
使用文泉驿作为默认中文字体
|
||||
|
||||
Debian、Gentoo、Mandriva
|
||||
|
||||
ArchLinux、Frugalware
|
||||
|
||||
则提供了官方源支持
|
||||
|
||||
而FreeBSD则在其ports中有提供
|
||||
|
||||
所以,今天我们所要分享的就是
|
||||
|
||||
文泉驿正黑体
|
||||
|
||||
可在Linux/UNIX,Windows
|
||||
|
||||
Mac OS和嵌入式操作系统中使用
|