Explorar el Código

阿里云图片处理方法

徐滕 hace 3 días
padre
commit
4261064f9f

+ 151 - 0
jeeplus-modules/jeeplus-public-modules/src/main/java/com/jeeplus/pubmodules/oss/service/OssService.java

@@ -3,7 +3,9 @@ package com.jeeplus.pubmodules.oss.service;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.HttpMethod;
 import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.model.GeneratePresignedUrlRequest;
 import com.aliyun.oss.model.OSSObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -266,6 +268,155 @@ public class OssService extends ServiceImpl<OssServiceMapper, WorkAttachment> {
     }
 
     /**
+     * 缩略图
+     * 获取阿里云 OSS 上图片的缩略图临时访问地址
+     * @param file 图片完整路径(包含 http(s)://)
+     * @return 带缩略图样式的临时URL
+     */
+    public String getThumbnailTemporaryLookUrl(String file) {
+        try {
+            file = file.replace("amp;", "");
+            String cons;
+
+            if (file.contains(aliyunUrl)) {
+                cons = aliyunUrl;
+            } else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")) {
+                cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
+            } else {
+                cons = aliyunDownloadUrl;
+            }
+
+            // 获取 key(即 OSS 路径)
+            String key = file.split(cons + "/")[1];
+
+            // 设置过期时间(24小时)
+            Date expiration = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24);
+
+            // 设置图片处理参数,这里按宽度200缩放
+            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
+            request.setExpiration(expiration);
+            request.setProcess("image/resize,w_200"); // ✅ 这里是关键:缩略图处理样式
+
+            //初始化OSSClient
+            OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
+            // 生成带签名的URL
+            URL url = ossClient.generatePresignedUrl(request);
+            return url.toString();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "";
+        }
+    }
+
+    /**
+     * 带水印、虚化原图(水印旋转45度)
+     * 阿里云获取临时文件查看url(带水印)
+     * @param file OSS 文件完整 URL
+     * @return 带临时访问权限的 URL
+     */
+    public String getFileTemporaryLookUrlWithWatermark(String file) {
+        try {
+            file = file.replace("amp;", "");
+
+            String cons;
+            if (file.contains(aliyunUrl)) {
+                cons = aliyunUrl;
+            } else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")) {
+                cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
+            } else {
+                cons = aliyunDownloadUrl;
+            }
+
+            String key = file.split(cons + "/")[1];
+
+            // 过期时间 24 小时
+            Date expiration = new Date(System.currentTimeMillis() + 1000L * 60 * 60 * 24);
+
+            // 构建预签名请求(带水印)
+            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
+
+            String text = "仅用于公司人员信息备案,其他用途无效";
+            // 将字符串转为 UTF-8 字节
+            byte[] bytes = text.getBytes(java.nio.charset.StandardCharsets.UTF_8);
+
+            // 使用 URL Safe Base64 编码
+            String base64Text = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
+
+            // 设置水印参数:中间 g_center,红色 FF0000,字号 40
+            String imageProcess = "image/blur,r_50,s_5" + "/watermark,text_" + base64Text + ",color_FF0000,size_40,g_center,rotate_45";
+
+            request.setProcess(imageProcess);
+
+            request.setExpiration(expiration);
+
+            //初始化OSSClient
+            OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
+            URL url = ossClient.generatePresignedUrl(request);
+            return url.toString();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "";
+        }
+    }
+
+
+    /**
+     * 带水印、虚化、缩略图(水印旋转45度)
+     * 获取阿里云 OSS 上图片的缩略图临时访问地址(带水印、缩略图、图片虚化)
+     * @param file 图片完整路径(包含 http(s)://)
+     * @return 带缩略图样式的临时URL
+     */
+    public String getThumbnailTemporaryWithWatermarkDimLookUrl(String file) {
+        try {
+            file = file.replace("amp;", "");
+
+            String cons;
+
+            if (file.contains(aliyunUrl)) {
+                cons = aliyunUrl;
+            } else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")) {
+                cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
+            } else {
+                cons = aliyunDownloadUrl;
+            }
+
+            // 获取 key(即 OSS 路径)
+            String key = file.split(cons + "/")[1];
+
+            // 设置过期时间(24小时)
+            Date expiration = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24);
+
+            // 设置图片处理参数,这里按宽度200缩放
+            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
+            request.setExpiration(expiration);
+
+            String text = "仅用于公司人员信息备案,其他用途无效";
+            // 将字符串转为 UTF-8 字节
+            byte[] bytes = text.getBytes(java.nio.charset.StandardCharsets.UTF_8);
+
+            // 使用 URL Safe Base64 编码
+            String base64Text = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
+
+            String imageProcess = "image/blur,r_50,s_5" + "/watermark,text_" + base64Text + ",color_FF0000,size_40,g_center,rotate_45"+ "/resize,w_200";
+
+            request.setProcess(imageProcess);
+
+            //初始化OSSClient
+            OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
+            // 生成带签名的URL
+            URL url = ossClient.generatePresignedUrl(request);
+            return url.toString();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "";
+        }
+    }
+
+
+    /**
      * 附件下载
      * @param key
      * @param fileName