Przeglądaj źródła

Merge branch 'dev' into 'mp-server-test'

优化生成缩略图

See merge request quanshu/mp-server!389
zhong chunping 3 lat temu
rodzic
commit
5f9ab87ab1

+ 13 - 8
mp-admin/src/main/java/com/qs/mp/web/controller/common/FileUploadController.java

@@ -243,12 +243,12 @@ public class FileUploadController extends BaseApiController {
         try {
             QcloudFileUtils.putStream(file.getInputStream(), name, bucketName, mimeType );
             BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
-            Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
+//            Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
 
 
             int pWidth = bufferedImage.getWidth();
             int pHeight = bufferedImage.getHeight();
-            builder.size(pWidth, pHeight);
+//            builder.size(pWidth, pHeight);
             double accuracy = CompressUtil.getAccuracy(file.getSize()/1024);
             LogUtil.info(logger, "原始文件长:{0},大小:{1},contentType:{2}", pWidth, file.getSize()/1024, mimeType);
 
@@ -259,32 +259,37 @@ public class FileUploadController extends BaseApiController {
             }
             String thumbName = name +"_s";
             String outFilePath = filePath+"/thumb/"+thumbName+"."+mimeType.substring(mimeType.lastIndexOf("/")+1);
-            builder.toFile(outFilePath);
+//            builder.toFile(outFilePath);
 
             // 压缩图片
             if (pWidth > 500) {
                 float scale = 500f / pWidth;
                 if ("image/png".equals(file.getContentType())) {
-                    Thumbnails.of(outFilePath).scale(scale > 0.5 ? 0.5 : scale).outputQuality(0.8)
+                    Thumbnails.of(bufferedImage).scale(scale > 0.5 ? 0.5 : scale).outputQuality(0.8)
                         .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
                         .toFile(outFilePath);
                 } else {
-                    Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8).toFile(outFilePath);
+                    Thumbnails.of(bufferedImage).scale(scale).outputQuality(0.8).toFile(outFilePath);
                 }
             }else {
                 if ("image/png".equals(file.getContentType())) {
-                    Thumbnails.of(outFilePath).scale(0.5).outputQuality(0.8)
+                    Thumbnails.of(bufferedImage).scale(0.5).outputQuality(0.8)
                         .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
                         .toFile(outFilePath);
                 } else {
-                    Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8).toFile(outFilePath);
+                    Thumbnails.of(bufferedImage).scale(1f).outputQuality(0.8).toFile(outFilePath);
                 }
             }
 
 
             File outFile = new File(outFilePath);
             LogUtil.info(logger, "压缩后文件大小:{0}", outFile.length()/ 1024);
-            QcloudFileUtils.putFile(outFile, thumbName, bucketName, mimeType);
+            if (outFile.length() < file.getSize()) {
+                QcloudFileUtils.putFile(outFile, thumbName, bucketName, mimeType);
+            } else {
+                // 如果大小没被压缩,则用原图作为缩略图
+                QcloudFileUtils.putStream(file.getInputStream(), thumbName, bucketName, mimeType );
+            }
             if(outFile.exists()) {
                 LogUtil.info(log, "delete file..."+outFilePath);
                 outFile.delete();

+ 14 - 8
mp-quartz/src/main/java/com/qs/mp/quartz/task/CosTask.java

@@ -120,10 +120,10 @@ public class CosTask {
 
     // 压缩图片
     BufferedImage bufferedImage = ImageIO.read(downFile);
-    Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
+//    Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
     int pWidth = bufferedImage.getWidth();
     int pHeight = bufferedImage.getHeight();
-    builder.size(pWidth, pHeight);
+//    builder.size(pWidth, pHeight);
     double accuracy = CompressUtil.getAccuracy(downObjectMeta.getContentLength()/1024);
     LogUtil.info(logger, "原始文件长:{0},大小:{1},类型:{2},压缩比:{3}", pWidth, downObjectMeta.getContentLength()/1024, contentType, 0.8);
 
@@ -135,7 +135,7 @@ public class CosTask {
     String thumbName = key + "_s";
     String outFilePath =
         filePath + "/thumb/" + thumbName + "." + contentType.substring(contentType.lastIndexOf("/") + 1);
-    builder.toFile(outFilePath);
+//    builder.toFile(outFilePath);
 
     // 压缩图片
     // 压缩图片
@@ -143,25 +143,31 @@ public class CosTask {
     if (pWidth > 500) {
       float scale = 500f / pWidth;
       if ("image/png".equals(contentType)) {
-        Thumbnails.of(outFilePath).scale(scale > 0.5 ? 0.5 : scale).outputQuality(0.8)
+        Thumbnails.of(bufferedImage).scale(scale > 0.5 ? 0.5 : scale).outputQuality(0.8)
             .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
             .toFile(outFilePath);
       } else {
-        Thumbnails.of(outFilePath).scale(scale).outputQuality(0.8).toFile(outFilePath);
+        Thumbnails.of(bufferedImage).scale(scale).outputQuality(0.8).toFile(outFilePath);
       }
     }else {
       if ("image/png".equals(contentType)) {
-        Thumbnails.of(outFilePath).scale(0.5).outputQuality(0.8)
+        Thumbnails.of(bufferedImage).scale(0.5).outputQuality(0.8)
             .imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png")
             .toFile(outFilePath);
       } else {
-        Thumbnails.of(outFilePath).scale(1f).outputQuality(0.8).toFile(outFilePath);
+        Thumbnails.of(bufferedImage).scale(1f).outputQuality(0.8).toFile(outFilePath);
       }
     }
 
     File outFile = new File(outFilePath);
-    QcloudFileUtils.putFile(outFile, thumbName, publicBucketName, contentType);
     LogUtil.info(logger, "压缩后文件大小:{0}", outFile.length()/ 1024);
+    if (outFile.length() > downObjectMeta.getContentLength()) {
+      // 没压缩,则使用原文件作为缩略图
+      QcloudFileUtils.putFile(downFile, thumbName, publicBucketName, contentType);
+    } else {
+      QcloudFileUtils.putFile(outFile, thumbName, publicBucketName, contentType);
+    }
+
     downFile.delete();
     outFile.delete();