💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 需要composer引入图像处理扩展 `composer require topthink/think-image` 引入类 `use think\Config as FaConfig;` ``` /** * 上传文件 * @ApiMethod (POST) * @param File $file 文件流 */ public function upload() { $file = $this->request->file('file'); if (empty($file)) { $this->error(__('No file upload or server upload limit exceeded')); } //判断是否已经存在附件 $sha1 = $file->hash(); $upload = FaConfig::get('upload'); preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches); $type = strtolower($matches[2]); $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3]; $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0); $fileInfo = $file->getInfo(); $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION)); $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'png'; //验证是否为图片文件 $imagewidth = $imageheight = 0; if (in_array($fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) { $imgInfo = getimagesize($fileInfo['tmp_name']); if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) { $this->error(__('Uploaded file is not a valid image')); } $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth; $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight; } $replaceArr = [ '{year}' => date("Y"), '{mon}' => date("m"), '{day}' => date("d"), '{hour}' => date("H"), '{min}' => date("i"), '{sec}' => date("s"), '{random}' => Random::alnum(16), '{random32}' => Random::alnum(32), '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'], '{suffix}' => $suffix, '{.suffix}' => $suffix ? '.' . $suffix : '', '{filemd5}' => md5_file($fileInfo['tmp_name']), ]; $savekey = $upload['savekey']; $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey); $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1); $fileName = substr($savekey, strripos($savekey, '/') + 1); $splInfo = $file->move(ROOT_PATH . '/public' . $uploadDir, $fileName); if ($splInfo) { /*缩略图*/ $vs = $splInfo->getSaveName(); $img = \think\Image::open($splInfo); /*解决图片旋转90问题_START*/ $str = $splInfo->getfilename(); $exif = exif_read_data($str); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 8: /*通用的是8*/ $img->rotate(90)->thumb(150, 150)->save(ROOT_PATH . '/public' . $uploadDir . $vs); break; case 3: /*IOS选择文件是3*/ $img->rotate(180)->thumb(150, 150)->save(ROOT_PATH . '/public' . $uploadDir . $vs); break; case 6: /*IOS相机是6*/ $img->rotate(90)->thumb(150, 150)->save(ROOT_PATH . '/public' . $uploadDir . $vs); break; } } else { $img->thumb(150, 150)->save(ROOT_PATH . '/public' . $uploadDir . $vs); } /*解决图片旋转90问题_END*/ $params = array( 'admin_id' => 0, 'user_id' => (int)$this->auth->id, 'filesize' => $fileInfo['size'], 'imagewidth' => $imagewidth, 'imageheight' => $imageheight, 'imagetype' => $suffix, 'imageframes' => 0, 'mimetype' => $fileInfo['type'], 'url' => $uploadDir . $splInfo->getSaveName(), 'uploadtime' => time(), 'storage' => 'local', 'sha1' => $sha1, ); $attachment = new \app\common\model\Attachment; $attachment->data(array_filter($params)); $attachment->save(); \think\Hook::listen("upload_after", $attachment); $this->success(__('Upload successful'), [ 'full_url' => request()->domain() . $uploadDir . $splInfo->getSaveName(), 'url' => $uploadDir . $splInfo->getSaveName() ]); } else { // 上传失败获取错误信息 $this->error($file->getError()); } } 补充(重中之重): $str = $splInfo->getfilename(); getfilename()方法是修改了底层旨在获取文件服务器地址:方法 文件路径:thinkphp\library\think\File.php public function getfilename($name = '') { return isset($this->filename[$name]) ? $this->filename[$name] : $this->filename; } $exif = exif_read_data($str); 这个方法需要php安装 exif扩展 ``` 返回示例: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201219113527412.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MDUwMzYw,size_16,color_FFFFFF,t_70)