113 lines
3.5 KiB
PHP
113 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace addons\webman\form;
|
|
|
|
use addons\webman\filesystem\Filesystem;
|
|
use ExAdmin\ui\contract\UploaderAbstract;
|
|
use ExAdmin\ui\response\Response;
|
|
use Intervention\Image\ImageManagerStatic as Image;
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
class Uploader extends UploaderAbstract
|
|
{
|
|
/**
|
|
* 写入文件
|
|
* @param string $filename 文件名
|
|
* @param $content 文件内容
|
|
* @return bool
|
|
*/
|
|
protected function put(string $filename, $content): bool
|
|
{
|
|
return Filesystem::disk($this->disk)->put($filename, $content);
|
|
}
|
|
|
|
/**
|
|
* 是否存在
|
|
* @param string $path 路径
|
|
* @return bool
|
|
*/
|
|
public function exists(string $path): bool
|
|
{
|
|
return Filesystem::disk($this->disk)->exists($path);
|
|
}
|
|
|
|
/**
|
|
* 返回访问url
|
|
* @param string $path 路径
|
|
* @return string
|
|
*/
|
|
public function url(string $path): string
|
|
{
|
|
|
|
return Filesystem::disk($this->disk)->url($path);
|
|
}
|
|
|
|
/**
|
|
* 临时目录
|
|
* @return string
|
|
*/
|
|
protected function tempDirectory(): string
|
|
{
|
|
return runtime_path('tmp');
|
|
}
|
|
|
|
/**
|
|
* 写入文件
|
|
* @param $content 文件内容
|
|
* @param $filename 文件名
|
|
* @return bool|string
|
|
*/
|
|
public function putContent($content, $filename)
|
|
{
|
|
$path = $this->directory . $filename . '.' . $this->extension;
|
|
return Filesystem::disk($this->disk)->put($path, $content);
|
|
}
|
|
|
|
/**
|
|
* 上传
|
|
* @param \Closure $complete
|
|
* @param bool $exists 判断秒传
|
|
* @return Response
|
|
*/
|
|
public function upload(\Closure $complete = null, bool $exists = false)
|
|
{
|
|
return parent::upload(function (UploadedFile $file) {
|
|
if ($this->form) {
|
|
$component = $this->form->getImageComponent();
|
|
if ($component) {
|
|
$thumbnail = $component->getThumbnail();
|
|
//图片处理
|
|
$interventionCall = $component->getInterventionCall();
|
|
if (count($interventionCall) > 0) {
|
|
$image = Image::make($file->getRealPath());
|
|
foreach ($interventionCall as $call) {
|
|
call_user_func_array([$image, $call['method']], $call['arguments']);
|
|
}
|
|
$file = $image->encode(null, null)->getEncoded();
|
|
}
|
|
//生成缩略图
|
|
if (count($thumbnail) > 0) {
|
|
if ($file instanceof UploadedFile) {
|
|
$data = $file->getRealPath();
|
|
$filename = request()->input('identifier');
|
|
} else {
|
|
$data = $file;
|
|
$filename = md5($data);
|
|
}
|
|
foreach ($thumbnail as $name => $size) {
|
|
$image = Image::make($data);
|
|
list($width, $height) = $size;
|
|
$content = $image->resize($width, $height)
|
|
->encode(null, null)
|
|
->getEncoded();
|
|
$this->putContent($content, $filename . '-' . $name);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
return $file;
|
|
}, request()->input('type') == 'file'); // TODO: Change the autogenerated stub
|
|
}
|
|
}
|