|
|
@@ -16,72 +16,72 @@ use yii\imagine\Image;
|
|
|
class UploadController extends BaseController
|
|
|
{
|
|
|
|
|
|
- public $guestAccess = ['save-img'];
|
|
|
+ public $guestAccess = ['save-img'];
|
|
|
|
|
|
- //上传图片接口
|
|
|
- public function actionSaveImg()
|
|
|
- {
|
|
|
- header('Content-type:text/html;charset=utf-8');
|
|
|
- $base64 = file_get_contents('php://input');
|
|
|
- $base64_image_content = str_replace("content=", "", urldecode($base64));
|
|
|
+ //上传图片接口
|
|
|
+ public function actionSaveImg()
|
|
|
+ {
|
|
|
+ header('Content-type:text/html;charset=utf-8');
|
|
|
+ $base64 = file_get_contents('php://input');
|
|
|
+ $base64_image_content = str_replace("content=", "", urldecode($base64));
|
|
|
|
|
|
- $categoryIds = Yii::$app->request->post('categoryId', []);
|
|
|
- if (!empty($categoryIds)) {
|
|
|
- $categoryList = CategoryService::getByIds($categoryIds);
|
|
|
- foreach ($categoryList as $cat) {
|
|
|
- if ($cat['sjId'] != $this->sjId) {
|
|
|
- util::fail('请选择自己的分类');
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ $categoryIds = Yii::$app->request->post('categoryId', []);
|
|
|
+ if (!empty($categoryIds)) {
|
|
|
+ $categoryList = CategoryService::getByIds($categoryIds);
|
|
|
+ foreach ($categoryList as $cat) {
|
|
|
+ if ($cat['sjId'] != $this->sjId) {
|
|
|
+ util::fail('请选择自己的分类');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- //匹配出图片的格式
|
|
|
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
|
|
|
- $type = $result[2];
|
|
|
- $sjId = $this->sjId;
|
|
|
- $month = date("Ym");
|
|
|
- $date = date("d");
|
|
|
- $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
|
|
|
- if (!file_exists($path)) {
|
|
|
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
|
|
|
- mkdir($path, 0700, true);
|
|
|
- }
|
|
|
- $preFileName = stringUtil::uniqueFileName();
|
|
|
- $newFile = $preFileName . ".{$type}";
|
|
|
- $fullPathFile = $path . $newFile;
|
|
|
- if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
|
|
|
- util::fail('上传失败!!');
|
|
|
- }
|
|
|
- $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
|
|
|
- $imgInfo = getimagesize($fullPathFile);
|
|
|
- $width = $imgInfo[0];
|
|
|
- $height = $imgInfo[1];
|
|
|
+ //匹配出图片的格式
|
|
|
+ if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
|
|
|
+ $type = $result[2];
|
|
|
+ $sjId = $this->sjId;
|
|
|
+ $month = date("Ym");
|
|
|
+ $date = date("d");
|
|
|
+ $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
|
|
|
+ if (!file_exists($path)) {
|
|
|
+ //检查是否有该文件夹,如果没有就创建,并给予最高权限
|
|
|
+ mkdir($path, 0700, true);
|
|
|
+ }
|
|
|
+ $preFileName = stringUtil::uniqueFileName();
|
|
|
+ $newFile = $preFileName . ".{$type}";
|
|
|
+ $fullPathFile = $path . $newFile;
|
|
|
+ if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
|
|
|
+ util::fail('上传失败!!');
|
|
|
+ }
|
|
|
+ $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
|
|
|
+ $imgInfo = getimagesize($fullPathFile);
|
|
|
+ $width = $imgInfo[0];
|
|
|
+ $height = $imgInfo[1];
|
|
|
|
|
|
- //如果图片太大,进行压缩,并删除原图
|
|
|
- if ($width > 800) {
|
|
|
- $newWidth = 800;
|
|
|
- $newHeight = ceil((800 * $height) / $width);
|
|
|
- $preFileName = stringUtil::uniqueFileName();
|
|
|
- $newFile = $preFileName . ".{$type}";
|
|
|
- $newFullPathFile = $path . $newFile;
|
|
|
- Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
|
|
|
- $width = $newWidth;
|
|
|
- $height = $newHeight;
|
|
|
- $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
|
|
|
- unlink($fullPathFile);
|
|
|
- $fullPathFile = $newFullPathFile;
|
|
|
- }
|
|
|
+ //如果图片太大,进行压缩,并删除原图
|
|
|
+ if ($width > 800) {
|
|
|
+ $newWidth = 800;
|
|
|
+ $newHeight = ceil((800 * $height) / $width);
|
|
|
+ $preFileName = stringUtil::uniqueFileName();
|
|
|
+ $newFile = $preFileName . ".{$type}";
|
|
|
+ $newFullPathFile = $path . $newFile;
|
|
|
+ Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
|
|
|
+ $width = $newWidth;
|
|
|
+ $height = $newHeight;
|
|
|
+ $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
|
|
|
+ unlink($fullPathFile);
|
|
|
+ $fullPathFile = $newFullPathFile;
|
|
|
+ }
|
|
|
|
|
|
- //上传oss 2021.3.18 linqh
|
|
|
- oss::uploadImage($img,$fullPathFile);
|
|
|
+ //上传oss 2021.3.18 linqh
|
|
|
+ oss::uploadImage($img, $fullPathFile);
|
|
|
|
|
|
- //生成小图 200px
|
|
|
- //$smallWidth = 200;
|
|
|
- //$smallHeight = ceil((200 * $height) / $width);
|
|
|
- //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
|
|
|
- //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
|
|
|
+ //生成小图 200px
|
|
|
+ //$smallWidth = 200;
|
|
|
+ //$smallHeight = ceil((200 * $height) / $width);
|
|
|
+ //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
|
|
|
+ //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
|
|
|
|
|
|
- $data = business::formatUploadImg($img);
|
|
|
+ $data = business::formatUploadImg($img);
|
|
|
|
|
|
// if (!empty($categoryIds)) {
|
|
|
// $time = time();
|
|
|
@@ -95,39 +95,41 @@ class UploadController extends BaseController
|
|
|
// PicCategoryService::batchAdd($add);
|
|
|
// }
|
|
|
|
|
|
- $data['shortUrl'] = $img;
|
|
|
- util::success($data);
|
|
|
- }
|
|
|
- util::fail('上传失败!');
|
|
|
- }
|
|
|
+ $data['shortUrl'] = $img;
|
|
|
+ $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
|
|
|
+ util::success($data);
|
|
|
+ }
|
|
|
+ util::fail('上传失败!');
|
|
|
+ }
|
|
|
|
|
|
- //保存图片 ssh 2019.12.20
|
|
|
- public function actionSave()
|
|
|
- {
|
|
|
- header('Content-type:text/html;charset=utf-8');
|
|
|
- $base64_image_content = '';
|
|
|
- //匹配出图片的格式
|
|
|
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
|
|
|
- $type = $result[2];
|
|
|
- $sjId = 12358;
|
|
|
- $month = date("Ym");
|
|
|
- $date = date("d");
|
|
|
- $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
|
|
|
- if (!file_exists($path)) {
|
|
|
- //检查是否有该文件夹,如果没有就创建,并给予最高权限
|
|
|
- mkdir($path, 0700, true);
|
|
|
- }
|
|
|
- $preFileName = stringUtil::uniqueFileName();
|
|
|
- $newFile = $preFileName . ".{$type}";
|
|
|
- $fullPath = $path . $newFile;
|
|
|
- if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
|
|
|
- util::fail('上传失败');
|
|
|
- }
|
|
|
- $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
|
|
|
- $data = business::formatUploadImg($img);
|
|
|
- $data['shortUrl'] = $img;
|
|
|
- util::success($data);
|
|
|
- }
|
|
|
- }
|
|
|
+ //保存图片 ssh 2019.12.20
|
|
|
+ public function actionSave()
|
|
|
+ {
|
|
|
+ header('Content-type:text/html;charset=utf-8');
|
|
|
+ $base64_image_content = '';
|
|
|
+ //匹配出图片的格式
|
|
|
+ if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
|
|
|
+ $type = $result[2];
|
|
|
+ $sjId = 12358;
|
|
|
+ $month = date("Ym");
|
|
|
+ $date = date("d");
|
|
|
+ $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
|
|
|
+ if (!file_exists($path)) {
|
|
|
+ //检查是否有该文件夹,如果没有就创建,并给予最高权限
|
|
|
+ mkdir($path, 0700, true);
|
|
|
+ }
|
|
|
+ $preFileName = stringUtil::uniqueFileName();
|
|
|
+ $newFile = $preFileName . ".{$type}";
|
|
|
+ $fullPath = $path . $newFile;
|
|
|
+ if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
|
|
|
+ util::fail('上传失败');
|
|
|
+ }
|
|
|
+ $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
|
|
|
+ $data = business::formatUploadImg($img);
|
|
|
+ $data['shortUrl'] = $img;
|
|
|
+ $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
|
|
|
+ util::success($data);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|