88 lines
3.0 KiB
PHP
88 lines
3.0 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||
// | 访问官网:https://www.likeadmin.cn
|
||
// | likeadmin团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeadminTeam
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\common\enum\FileEnum;
|
||
use app\common\model\file\File;
|
||
use app\common\service\UploadService;
|
||
use Exception;
|
||
use think\response\Json;
|
||
|
||
|
||
/** 上传文件
|
||
* Class UploadController
|
||
* @package app\api\controller
|
||
*/
|
||
class UploadController extends BaseApiController
|
||
{
|
||
public array $notNeedLogin=['uploadVideoCover'];
|
||
/**
|
||
* @notes 上传图片
|
||
* @return Json
|
||
* @author 段誉
|
||
* @date 2022/9/20 18:11
|
||
*/
|
||
public function image()
|
||
{
|
||
try {
|
||
$result = UploadService::image(0, $this->userId,FileEnum::SOURCE_USER);
|
||
return $this->success('上传成功', ['url'=>env('project.project_url').'/'.$result['url']]);
|
||
} catch (Exception $e) {
|
||
return $this->fail($e->getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
public function uploadVideoCover()
|
||
{
|
||
try {
|
||
$deviceId = $this->request->param('device_id', '');
|
||
// Base64编码的图片字符串
|
||
$base64Image = $this->request->param('image'); // 这里只展示部分内容
|
||
|
||
// 去除前面的"data:"标识
|
||
$base64Data = substr($base64Image, strpos($base64Image, ',') + 1);
|
||
|
||
// 将Base64编码的图片字符串转换为二进制数据
|
||
$binaryData = base64_decode($base64Data);
|
||
|
||
// 创建图像对象
|
||
$imageObject = imagecreatefromstring($binaryData);
|
||
$saveDir = 'uploads/images/' . date('Ymd'). '/';
|
||
if (!is_dir($saveDir)) {
|
||
mkdir($saveDir, 0755, true);
|
||
}
|
||
$fileName = uniqid().'.png';
|
||
|
||
// 保存图片
|
||
imagepng($imageObject, $saveDir.$fileName);
|
||
|
||
$file = File::create([
|
||
'cid' => $deviceId,
|
||
'type' => FileEnum::IMAGE_TYPE,
|
||
'name' => $fileName,
|
||
'uri' => $saveDir . $fileName,
|
||
'source' => FileEnum::SOURCE_USER,
|
||
'source_id' => $this->userId,
|
||
'create_time' => time(),
|
||
]);
|
||
return $this->success('上传成功', ['url'=>env('project.project_url').'/'.$saveDir.$fileName]);
|
||
} catch (Exception $e) {
|
||
return $this->fail($e->getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
} |