Files
orico-official-website-old/app/admin/controller/Webuploader.php
2024-10-29 17:38:47 +08:00

786 lines
33 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\Lang;
use think\Loader;
use think\Config;
use image\Image;
use pagination\Pagination;
class Webuploader extends BaseController
{
private $docDir = '';
private $rootDir = '';
private $basePath = '/';
private $saveDirectory = 'default';
private $num = '10';
public function __construct()
{
parent::__construct();
date_default_timezone_set("Asia/Shanghai");
$this->docDir = $this->request->server('DOCUMENT_ROOT');
$this->rootDir = $this->request->root();
$this->basePath = $this->docDir . $this->rootDir . '/uploads';
$this->saveDirectory = '';
error_reporting(E_ERROR | E_WARNING);
Config::set('url_common_param', true);
header("Content-Type: text/html; charset=utf-8");
}
public function show()
{
$filter_name = $this->request->get('filter_name', '', 'urldecode');
if (!empty($filter_name)) {
$filter_name = trim(str_replace(['/../', '../', '*'], '', $filter_name), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
}
$relativepath = $this->request->get('directory', '', 'urldecode');
if (!empty($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$directory = $this->basePath . '/' . $relativepath;
} else {
$directory = $this->basePath;
}
$type = $this->request->get('type', '');
$target = $this->request->get('target', '');
$thumb = $this->request->get('thumb', '');
$func = $this->request->get('func', 'undefined');
$num = $this->request->get('num/d', 1);
$url = array();
$url['target'] = $target;
$data['target'] = $target;
$url['thumb'] = $thumb;
$data['thumb'] = $thumb;
$url['type'] = $type;
$data['type'] = $type;
$url['func'] = $func;
$data['func'] = $func;
$url['num'] = $num;
$data['num'] = $num;
//Config::set('url_common_param', true);
//$config = Config::get('paginate');
$page = $this->request->request('page/d', 1);
$page = $page < 1 ? 1 : $page;
$directories = array();
$files = array();
$data['images'] = array();
if (mb_substr($directory . '/' . $filter_name, 0, mb_strlen($this->basePath)) == $this->basePath) {
// Get directories
$directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);
if (!$directories) {
$directories = array();
}
switch ($type) {
case 'image':
// Get files
$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
break;
case 'video':
// Get files
$files = glob($directory . '/' . $filter_name . '*.{avi,mp4,rmvb,mkv,wmv,ogg,webm,AVI,MP4,RMVB,MKV,WMV,OGG,WEBM}', GLOB_BRACE);
break;
case 'file':
// Get files
$files = glob($directory . '/' . $filter_name . '*.*', GLOB_BRACE);
break;
default:
break;
}
if (!$files) {
$files = array();
}
}
// Merge directories and files
$images = array_merge(array_diff($directories, [$this->basePath . '/smallimg', $this->basePath . '/allimg']), $files);
// Get total number of files and directories
$image_total = count($images);
// Split the array based on current page number and max number of items per page of 10
$images = array_splice($images, ($page - 1) * 16, 16);
foreach ($images as $image) {
$name = str_split(basename($image), 24);
if (is_dir($image)) {
$path = mb_substr($image, mb_strlen($this->docDir . $this->rootDir));
$url['directory'] = urlencode(mb_substr($image, mb_strlen($this->basePath)));
$data['images'][] = array(
'thumb' => '',
'name' => implode(' ', $name),
'type' => 'directory',
'path' => $path,
'href' => url('/admin/webuploader/show', array_filter($url), true)
);
} elseif (is_file($image)) {
$path = mb_substr($image, mb_strlen($this->docDir . $this->rootDir));
$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
switch ($ext) {
case 'jpg':
case 'png':
case 'gif':
case 'bmp':
case 'jpeg':
$data['images'][] = array(
'thumb' => $path,
'name' => implode(' ', $name),
'type' => 'image',
'path' => $path,
'href' => $this->request->domain() . $path
);
break;
case 'html':
case 'htm':
case 'js':
case 'php':
case 'txt':
$data['images'][] = array(
'thumb' => $path,
'name' => implode(' ', $name),
'type' => 'text',
'path' => $path,
'href' => 'javascript:void(0);'
);
break;
case 'avi':
case 'mp4':
case 'rmvb':
case 'mkv':
case 'wmv':
case 'ogg':
case 'webm':
$data['images'][] = array(
'thumb' => $path,
'name' => implode(' ', $name),
'type' => 'video',
'path' => $path,
'href' => 'javascript:void(0);'
);
break;
case 'zip':
case 'rar':
case 'gz':
case 'tar':
case 'tgz':
case 'gz':
case 'iso':
$data['images'][] = array(
'thumb' => $path,
'name' => implode(' ', $name),
'type' => 'archive',
'path' => $path,
'href' => 'javascript:void(0);'
);
break;
default:
$data['images'][] = array(
'thumb' => $path,
'name' => implode(' ', $name),
'type' => 'other',
'path' => $path,
'href' => 'javascript:void(0);'
);
break;
}
}
}
$data['filter_name'] = $filter_name;
$url['directory'] = urlencode($relativepath);
$data['directory'] = urlencode($relativepath);
//Upload
$data['webuploader'] = url('/admin/webuploader/modal', array_filter($url), true);
// Refresh
$data['refresh'] = url('/admin/webuploader/show', array_filter($url), true);
if (!empty($relativepath)) {
$pos = strrpos($relativepath, '/');
if ($pos) {
$url['directory'] = urlencode(mb_substr($relativepath, 0, $pos));
} else {
$url['directory'] = '';
}
}
// Parent
if (!empty($relativepath)) {
$pos = strrpos($relativepath, '/');
if ($pos) {
$url['directory'] = urlencode(mb_substr($relativepath, 0, $pos));
} else {
$url['directory'] = '';
}
}
$data['parent'] = url('/admin/webuploader/show', array_filter($url), true);
//Pagination
if (!empty($relativepath)) {
$url['directory'] = urlencode($relativepath);
}
if (!empty($filter_name)) {
$url['filter_name'] = urlencode($filter_name);
}
$url['page'] = '{page}';
$pagination = new Pagination();
$pagination->total = $image_total;
$pagination->page = $page;
$pagination->limit = 16;
$pagination->url = url('/admin/webuploader/show', array_filter($url), true);
$data['pagination'] = $pagination->render();
$this->assign($data);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html', 'tpl_replace_string' => [],]);
return $this->fetch();
}
public function modal()
{
$relativepath = $this->request->get('directory', '', 'urldecode');
if (!empty($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
}
$func = $this->request->get('func', 'undefined');
$num = $this->request->get('num', $this->num);
$type = $this->request->get('type', '');
$target = $this->request->get('target', '');
$thumb = $this->request->get('thumb', '');
$url = array();
$url['target'] = $target;
$data['target'] = $target;
$url['thumb'] = $thumb;
$data['thumb'] = $thumb;
$url['type'] = $type;
$data['type'] = $type;
$url['func'] = $func;
$data['func'] = $func;
$url['num'] = $num;
$data['num'] = $num;
//$url['directory'] = urlencode($relativepath);
//$data['directory'] = urlencode($relativepath);
$url['directory'] = $relativepath;
$data['directory'] = $relativepath;
//Uploadiframe
$data['uploadiframe'] = url('/admin/webuploader/uploadiframe', array_filter($url), true);
//Webuploader
$data['webuploader'] = url('/admin/webuploader/modal', array_filter($url), true);
// Refresh
$data['refresh'] = url('/admin/webuploader/show', array_filter($url), true);
// Parent
if (!empty($relativepath)) {
$pos = strrpos($relativepath, '/');
if ($pos) {
$url['directory'] = urlencode(mb_substr($relativepath, 0, $pos));
} else {
$url['directory'] = '';
}
}
$data['parent'] = url('/admin/webuploader/show', array_filter($url), true);
//url
$data['filter_name'] = '';
$this->assign($data);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html', 'tpl_replace_string' => [],]);
return $this->fetch();
}
public function uploadiframe()
{
$relativepath = $this->request->get('directory', '', 'urldecode');
if (!empty($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
}
$func = $this->request->get('func', 'undefined');
//$num = $this->request->get('num/d', 1);
$num = $this->request->get('num', $this->num);
$type = $this->request->get('type', '');
$target = $this->request->get('target', '');
$thumb = $this->request->get('thumb', '');
$url = array();
//$url['target'] = $target;
$data['target'] = $target;
//$url['thumb'] = $thumb;
$data['thumb'] = $thumb;
//$url['type'] = $type;
$data['type'] = $type;
//$url['func'] = empty($func) ? 'undefined' : $func;
$data['func'] = empty($func) ? 'undefined' : $func;
//$url['num'] = $num;
$data['num'] = $num;
$url['directory'] = urlencode($relativepath);
$data['directory'] = urlencode($relativepath);
if ($type == 'image') {
$data['uploadurl'] = url('/admin/webuploader/imageupload', array_filter($url), true);
$data['deleteurl'] = url('/admin/webuploader/deluploadimage', array_filter($url), true);
$data['ext'] = 'jpg,png,gif,jpeg';
//$data['mimetype']='jpg,png,gif,jpeg';
} else {
$data['uploadurl'] = url('/admin/webuploader/fileupload', array_filter($url), true);
$data['deleteurl'] = url('/admin/webuploader/deluploadfile', array_filter($url), true);
$data['ext'] = '*';
//$data['mimetype']='*/*';
}
$this->assign($data);
Config::set('default_ajax_return', 'html');
$this->view->engine(['type' => 'php', 'view_suffix' => 'html']);
$content = $this->fetch();
$tpl_replace_string = $this->view->engine->config('tpl_replace_string');
return str_replace(array_keys($tpl_replace_string), array_values($tpl_replace_string), $content);
}
public function imageupload()
{
// 上传图片框中的描述表单名称,
$title = $this->request->get('pictitle');
$relativepath = $this->request->get('directory', '', 'urldecode');
if (!empty($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
}
$this->saveDirectory = $relativepath ?: 'default';
// 获取表单上传文件 例如上传了001.jpg
$file = request()->file('file');
// if (empty($file))
// $file = request()->file('upfile');
$image_upload_limit_size = Config::get('image_upload_limit_size');
$result = $this->validate(
['file' => $file],
['file' => 'image|fileSize:' . $image_upload_limit_size . '|fileExt:jpg,jpeg,gif,png'],
['file.image' => '上传文件必须为图片', 'file.fileSize' => '上传文件过大', 'file.fileExt' => '上传文件后缀名必须为jpg,jpeg,gif,png']
);
if (true !== $result || !$file) {
$state = "ERROR " . $result;
} else {
$saveDirectory = $this->saveDirectory . '/' . date('Y/md') . '/';
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move($this->basePath . '/' . $saveDirectory, false);
if ($info) {
$state = "SUCCESS";
} else {
$state = "ERROR " . $file->getError();
}
$return_url = '/uploads/' . $saveDirectory . $info->getFilename();
$return_data['url'] = str_replace('\\', '/', $return_url);
}
if ($state == 'SUCCESS') {
$this->watermark($return_url);
}
$return_data['title'] = $title;
$return_data['original'] = ''; // 这里好像没啥用 暂时注释起来
$return_data['state'] = $state;
$return_data['directory'] = $this->saveDirectory;
return json_encode($return_data);
}
public function fileupload()
{
// 上传图片框中的描述表单名称,
$title = $this->request->get('pictitle');
$relativepath = $this->request->get('directory', '', 'urldecode');
if (!empty($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
}
$this->saveDirectory = $relativepath ?: 'default';
// 获取表单上传文件 例如上传了001.jpg
$file = request()->file('file');
// if (empty($file))
// $file = request()->file('upfile');
$file_upload_limit_size = Config::get('file_upload_limit_size');
$result = $this->validate(
['file' => $file],
['file' => 'fileSize:' . $file_upload_limit_size],
['file.fileSize' => '上传文件过大']
);
if (true !== $result || !$file) {
$state = "ERROR " . $result;
} else {
$saveDirectory = $this->saveDirectory . '/' . date('Y/md') . '/';
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move($this->basePath . '/' . $saveDirectory, false);
if ($info) {
$state = "SUCCESS";
} else {
$state = "ERROR " . $file->getError();
}
$return_url = '/uploads/' . $saveDirectory . $info->getFilename();
$return_data['url'] = str_replace('\\', '/', $return_url);
}
$return_data['title'] = $title;
$return_data['state'] = $state;
$return_data['directory'] = $this->saveDirectory;
echo json_encode($return_data);
exit;
}
/*
删除上传的图片
*/
public function deluploadimage()
{
$action = $this->request->get('action', 'del');
$filename = $this->request->get('filename');
$filename = empty($filename) ? $this->request->get('url') : $filename;
// $filename = str_replace('../', '', $filename);
// $filename = trim($filename, "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$filename = trim(str_replace('../', '', $filename), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$filename = './' . $filename;
if ($action == 'del' && !empty($filename) && is_file($filename)) {
$size = getimagesize($filename);
$filetype = explode('/', $size['mime']);
if ($filetype[0] != 'image') {
exit(0);
}
if (unlink($filename)) {
echo 1;
} else {
echo 0;
}
}
exit;
}
/*
删除上传的文件
*/
public function deluploadfile()
{
$action = $this->request->get('action', 'del');
$filename = $this->request->get('filename');
$filename = empty($filename) ? $this->request->get('url') : $filename;
// $filename = str_replace('../', '', $filename);
// $filename = trim($filename, "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$filename = trim(str_replace('../', '', $filename), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$filename = './' . $filename;
if ($action == 'del' && !empty($filename) && is_file($filename)) {
if (unlink($filename)) {
echo 1;
} else {
echo 0;
}
}
exit;
}
public function preview()
{
// 此页面用来协助 IE6/7 预览图片,因为 IE 6/7 不支持 base64
$DIR = 'preview';
// Create target dir
if (!file_exists($DIR)) {
@mkdir($DIR);
}
$cleanupTargetDir = true; // Remove old files
$maxFileAge = 5 * 3600; // Temp file age in seconds
if ($cleanupTargetDir) {
if (!is_dir($DIR) || !$dir = opendir($DIR)) {
die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
}
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $DIR . DIRECTORY_SEPARATOR . $file;
// Remove temp file if it is older than the max age and is not the current file
if (@filemtime($tmpfilePath) < time() - $maxFileAge) {
@unlink($tmpfilePath);
}
}
closedir($dir);
}
$src = file_get_contents('php://input');
if (preg_match("#^data:image/(\w+);base64,(.*)$#", $src, $matches)) {
$previewUrl = sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
);
$previewUrl = str_replace("preview.php", "", $previewUrl);
$base64 = $matches[2];
$type = $matches[1];
if ($type === 'jpeg') {
$type = 'jpg';
}
$filename = md5($base64) . ".$type";
$filePath = $DIR . DIRECTORY_SEPARATOR . $filename;
if (file_exists($filePath)) {
die('{"jsonrpc" : "2.0", "result" : "' . $previewUrl . 'preview/' . $filename . '", "id" : "id"}');
} else {
$data = base64_decode($base64);
file_put_contents($filePath, $data);
die('{"jsonrpc" : "2.0", "result" : "' . $previewUrl . 'preview/' . $filename . '", "id" : "id"}');
}
} else {
die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "un recoginized source"}}');
}
}
public function upload()
{
$json = [];
// Make sure we have the correct directory
$relativepath = $this->request->get('directory', '', 'urldecode');
if (!empty($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$directory = $this->basePath . '/' . $relativepath;
} else {
$directory = $this->basePath;
}
// Check its a directory
if (!is_dir($directory) || mb_substr($directory, 0, mb_strlen($this->basePath)) != $this->basePath) {
if (!mkdir($directory, 0755, true)) {
$json['error'] = '路径错误';
}
}
if (!$json) {
$json = [
'success' => '文件上传成功'
];
$file_upload_limit_size = Config::get('file_upload_limit_size');
$image_upload_limit_size = Config::get('image_upload_limit_size');
// 获取表单上传文件
$errors = [];
$files = $this->request->file('file');
foreach ($files as $k => $file) {
$file_info = $file->getInfo();
$idx = mb_strripos($file_info['name'], '.', 0, 'utf-8');
$file = $file->validate(['size' => $file_upload_limit_size]);
if (false !== $idx) {
$start = $idx;
if (mb_strlen($file_info['name'], 'utf-8') > $idx) {
$start = $idx + 1;
}
$ext = mb_substr($file_info['name'], $start);
if (in_array($ext, ['gif', 'jpg', 'jpeg', 'png', 'bmp'])) {
$file->validate(['size' => $image_upload_limit_size]);
}
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move($directory, false);
if ($info) {
// 成功上传后 获取上传信息
$this->watermark('/uploads/' . $relativepath . '/' . $info->getFilename());
} else {
// 上传失败获取错误信息
$errors[] = '文件【' . $file->getInfo()['name'] . '】' . $file->getError();
}
}
if (count($errors) != 0) {
$json = [
'success' => implode('\n', $errors)
];
}
}
echo json_encode($json);
exit;
}
public function folder()
{
$json = array();
// Make sure we have the correct directory
$relativepath = $this->request->get('directory', '', 'urldecode');
if (isset($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$directory = $this->basePath . '/' . $relativepath;
} else {
$directory = $this->basePath;
}
// Check its a directory
if (!is_dir($directory) || mb_substr($directory, 0, mb_strlen($this->basePath)) != $this->basePath) {
$json['error'] = '路径错误';
echo json_encode($json);
exit;
}
if ($this->request->isPost()) {
// Sanitize the folder name
$folder = basename($this->request->post('folder', '', 'urldecode'));
// Validate the filename length
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $folder)) {
$json['error'] = $folder . '文件夹名称不是由数字字母下划线组成';
echo json_encode($json);
exit;
}
// Validate the filename length
if ((mb_strlen($folder) < 3) || (mb_strlen($folder) > 128)) {
$json['error'] = $folder . '文件夹长度错误';
echo json_encode($json);
exit;
}
// Check if directory already exists or not
if (is_dir($directory . '/' . $folder)) {
$json['error'] = $folder . '文件夹已存在';
echo json_encode($json);
exit;
}
}
if (!isset($json['error'])) {
mkdir($directory . '/' . $folder, 0777);
chmod($directory . '/' . $folder, 0777);
@touch($directory . '/' . $folder . '/' . 'index.html');
$json['success'] = '文件夹创建成功';
}
echo json_encode($json);
exit;
}
public function rename()
{
$json = array();
// Make sure we have the correct directory
$relativepath = $this->request->get('directory', '', 'urldecode');
if (isset($relativepath)) {
$relativepath = trim(str_replace(['/../', '../', '*'], '', $relativepath), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
$directory = $this->basePath . '/' . $relativepath;
} else {
$directory = $this->basePath;
}
// Check its a directory
if (!is_dir($directory) || mb_substr($directory, 0, mb_strlen($this->basePath)) != $this->basePath) {
$json['error'] = '路径错误';
echo json_encode($json);
exit;
}
if ($this->request->isPost()) {
// Sanitize the folder name
$path = $this->request->post('path', '', 'urldecode');
$pathinfo = pathinfo($path);
$filenewname = $this->request->post('filenewname', '', 'urldecode');
// Validate the filename length
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $filenewname)) {
$json['error'] = $filenewname . '文件名称不是由数字字母下划线组成';
echo json_encode($json);
exit;
}
// Validate the filename length
if ((mb_strlen($filenewname) < 3) || (mb_strlen($filenewname) > 128)) {
$json['error'] = $filenewname . '文件名长度错误,至少3个字符以上';
echo json_encode($json);
exit;
}
// Check if directory already exists or not
if (!file_exists($directory . '/' . $pathinfo['basename']) || !(file_exists('.' . $path))) {
$json['error'] = $pathinfo['filename'] . '文件不存在';
echo json_encode($json);
exit;
}
if (!isset($json['error'])) {
rename('.' . $path, $directory . '/' . $filenewname . ($pathinfo['extension'] ? '.' . $pathinfo['extension'] : ''));
$json['success'] = '文件重命名成功';
}
}
echo json_encode($json);
exit;
}
public function delete()
{
$json = array();
$paths = $this->request->post();
if (isset($paths['path'])) {
$paths = $paths['path'];
} else {
$paths = array();
}
// Loop through each path to run validations
foreach ($paths as $path) {
$path = trim(str_replace(['/../', '../', '*'], '', $path), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
// Check path exsists
if ($path == 'uploads' || mb_substr($this->docDir . $this->rootDir . '/' . $path, 0, mb_strlen($this->basePath)) != $this->basePath) {
$json['error'] = '删除错误';
break;
}
}
if (!$json) {
// Loop through each path
foreach ($paths as $path) {
$path = $this->docDir . $this->rootDir . '/' . trim(str_replace(['/../', '../', '*'], '', $path), "\x20\x09\x0A\x0D\x00\x0B\x2E\x2F");
// If path is just a file delete it
if (is_file($path)) {
unlink($path);
// If path is a directory beging deleting each file and sub folder
} elseif (is_dir($path)) {
$files = array();
// Make path into an array
$path = array($path . '*');
// While the path array is still populated keep looping through
while (count($path) != 0) {
$next = array_shift($path);
foreach (glob($next) as $file) {
// If directory add to path array
if (is_dir($file)) {
$path[] = $file . '/*';
}
// Add the file to the files to be deleted array
$files[] = $file;
}
}
// Reverse sort the file array
rsort($files);
foreach ($files as $file) {
// If file just delete
if (is_file($file)) {
unlink($file);
// If directory use the remove directory function
} elseif (is_dir($file)) {
rmdir($file);
}
}
}
}
$json['success'] = '删除成功';
}
echo json_encode($json);
exit;
}
public function watermark($return_url = '/uploads/nopic.jpg')
{
$iswatermark = Config::get('watermark');
$return_data = ['watermark' => $iswatermark];
if ($iswatermark) {
$wmconfig = [
'watermark' => $iswatermark,
'mark_type' => Config::get('mark_type'),
'mark_image' => Config::get('mark_image'),
'mark_width_height' => Config::get('mark_width_height'),
'mark_text' => Config::get('mark_text'),
'mark_text_color' => Config::get('mark_text_color'),
'mark_degree' => Config::get('mark_degree'),
'mark_quality' => Config::get('mark_quality'),
'mark_position' => Config::get('mark_position'),
];
$imgresource = '.' . $return_url;
$image = Image::open($imgresource);
//$image->open($imgresource);
$return_data['mark_type'] = $wmconfig['mark_type'];
if ($image->width() > $wmconfig['mark_width_height']['width'] && $image->height() > $wmconfig['mark_width_height']['height']) {
$save_filename = $this->basePath . '/original_image' . mb_substr($return_url, 8); //截取 /uploads 后的内容
if (!is_dir(dirname($save_filename))) {
mkdir(dirname($save_filename), 0777, true);
}
$image->save($save_filename, null, 100);
if ($wmconfig['mark_type'] == 'text') {
//$image->text($wmconfig['mark_text'],'./hgzb.ttf',20,'#000000',9)->save($imgresource);
$ttf = './hgzb.ttf';
if (file_exists($ttf)) {
$size = $wmconfig['mark_text_size'] ? $wmconfig['mark_text_size'] : 30;
$color = $wmconfig['mark_text_color'] ?: '#000000';
if (!preg_match('/^#[0-9a-fA-F]{6}$/', $color)) {
$color = '#000000';
}
$transparency = intval((100 - $wmconfig['mark_degree']) * (127 / 100));
$color .= dechex($transparency);
$image->open($imgresource)->text($wmconfig['mark_text'], $ttf, $size, $color, $wmconfig['mark_position'])->save($imgresource);
$return_data['mark_text'] = $wmconfig['mark_text'];
}
} else {
//$image->water('.'.$wmconfig['mark_img'],9,$wmconfig['mark_degree'])->save($imgresource);
$waterPath = '.' . $wmconfig['mark_image'];
$quality = $wmconfig['mark_quality'] ? $wmconfig['mark_quality'] : 80;
$waterTempPath = dirname($waterPath) . '/temp_' . basename($waterPath);
$image->open($waterPath)->save($waterTempPath, null, $quality);
$image->open($imgresource)->water($waterTempPath, $wmconfig['mark_position'], $wmconfig['mark_degree'])->save($imgresource);
@unlink($waterTempPath);
}
}
}
return $return_data;
}
}