app首页视频封面,上传监控视频封面

This commit is contained in:
chenbo 2024-01-08 16:17:59 +08:00
parent 3eb365c461
commit b04c038bd3
1 changed files with 32 additions and 3 deletions

View File

@ -15,6 +15,7 @@
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;
@ -26,7 +27,7 @@ use think\response\Json;
*/
class UploadController extends BaseApiController
{
public array $notNeedLogin=['uploadVideoCover'];
/**
* @notes 上传图片
* @return Json
@ -48,8 +49,36 @@ class UploadController extends BaseApiController
{
try {
$deviceId = $this->request->param('device_id', '');
$result = UploadService::image($deviceId, $this->userId,FileEnum::SOURCE_USER);
return $this->success('上传成功', ['url'=>env('project.project_url').'/'.$result['url']]);
// 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());
}