{"id":6588,"date":"2025-09-29T11:24:29","date_gmt":"2025-09-29T03:24:29","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=6588"},"modified":"2025-09-29T11:24:29","modified_gmt":"2025-09-29T03:24:29","slug":"springbootthumbnailator%e7%ad%89%e4%ba%8ejava%e7%95%8c%e7%9a%84%e7%be%8e%e5%9b%be%e7%a7%80%e7%a7%80%ef%bc%8c%e7%bc%a9%e5%9b%be%ef%bc%8c%e6%b0%b4%e5%8d%b0%ef%bc%8cgif%e6%8b%86%e5%b8%a7%e5%8e%8b","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/spring-boot\/springbootthumbnailator%e7%ad%89%e4%ba%8ejava%e7%95%8c%e7%9a%84%e7%be%8e%e5%9b%be%e7%a7%80%e7%a7%80%ef%bc%8c%e7%bc%a9%e5%9b%be%ef%bc%8c%e6%b0%b4%e5%8d%b0%ef%bc%8cgif%e6%8b%86%e5%b8%a7%e5%8e%8b\/","title":{"rendered":"SpringBoot+Thumbnailator\u7b49\u4e8eJava\u754c\u7684\u7f8e\u56fe\u79c0\u79c0\uff0c\u7f29\u56fe\uff0c\u6c34\u5370\uff0cgif\u62c6\u5e27\u538b\u7f29"},"content":{"rendered":"\n<p>\u524d\u6bb5\u65f6\u95f4\u5199\u4e86\u4e00\u7bc7\u6587\u7ae0\u300a<a target=\"_blank\" href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIyODE5NjUwNQ==&amp;mid=2653383435&amp;idx=1&amp;sn=9f70fcb57f87e1bbc931b9015d939224&amp;scene=21#wechat_redirect\" rel=\"noreferrer noopener\" rel=\"nofollow\" >Java \u56fe\u7247\u7b49\u6bd4\u4f8b\u7f29\u653e\u4ee5\u53ca\u6309\u50cf\u7d20\u538b\u7f29<\/a>\u300b\uff0c\u4eca\u5929\u6709\u7f51\u53cb\u770b\u5230\u4e86\uff0c\u7ed9\u6211\u56de\u590d\u8bf4\uff0c\u5e94\u8be5\u662f\u4f7f\u7528 Thumbnailator\u3002\u6211\u4e00\u641c\uff0c\u53d1\u73b0\u786e\u5b9e\u662f\u4e00\u4e2a\u5b9d\u85cf\u3002\u611f\u8c22\u8fd9\u4f4d\u70ed\u5fc3\u7f51\u53cb\u7684\u63d0\u9192\uff0c\u8ba9\u6211\u591a\u5b66\u5230\u4e86\u4e00\u4e2a\u6846\u67b6\uff0c\u8fd9\u5c31\u662f Java \u56fe\u50cf\u5904\u7406\u7684\u745e\u58eb\u519b\u5200\uff1aThumbnailator\uff0c\u672c\u6587\u4ecb\u7ecd\u8be5\u6846\u67b6\u7684\u5168\u89e3\u6790\u3002<\/p>\n\n\n\n<p>\u8bdd\u8bf4\u5728 Java \u540e\u7aef\u5f00\u53d1\u4e2d\uff0c\u56fe\u7247\u5904\u7406\u51e0\u4e4e\u65e0\u5904\u4e0d\u5728\uff1a\u5934\u50cf\u4e0a\u4f20\u3001\u5546\u54c1\u7f29\u7565\u56fe\u3001\u6d3b\u52a8\u6d77\u62a5\u3001\u8bc1\u4e66\u751f\u6210\u3001\u6c34\u5370\u6253\u7801\u2026\u2026\u5982\u679c\u6bcf\u6b21\u90fd\u624b\u5199&nbsp;<code>ImageIO<\/code>&nbsp;+&nbsp;<code>Graphics2D<\/code>\uff0c\u4e0d\u4ec5\u8981\u5904\u7406\u5927\u91cf\u7ec6\u8282\uff0c\u8fd8\u5bb9\u6613\u8e29\u5751\u3002\u6211\u4e4b\u524d\u5c31\u662f\uff0c\u5728\u8e29\u5751\u4e0e\u586b\u5751\u4e4b\u95f4\u5f98\u5f8a\uff0c\u597d\u5728\u5b8c\u7f8e\u89e3\u51b3\u4e86\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u5462\uff0c\u5c31\u82b1\u70b9\u65f6\u95f4\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u6b3e\u7f51\u53cb\u63a8\u8350\u7684<strong>\u8f7b\u91cf\u7ea7\u3001\u96f6\u4f9d\u8d56\u3001\u94fe\u5f0f API \u7684\u5b9d\u85cf\u7c7b\u5e93<\/strong>\uff1a<code>Thumbnailator<\/code>\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u5927\u5bb6\u90fd\u80fd\u641e\u61c2\u5b83\u662f\u4ec0\u4e48\u3001\u80fd\u505a\u4ec0\u4e48\u3001\u600e\u4e48\u73a9\uff0c\u4ee5\u53ca\u5982\u4f55\u628a GIF \u4e5f\u5b89\u6392\u5f97\u660e\u660e\u767d\u767d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" class=\"lazyload\"data-src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\u56fe\u7247\"\/><noscript><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0\" alt=\"\u56fe\u7247\"\/><\/noscript><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Thumbnailator \u662f\u8c01\uff1f<\/strong><\/h2>\n\n\n\n<p>Thumbnailator \u662f\u8c37\u6b4c\u5f00\u6e90\u7684 Java \u5904\u7406\u56fe\u7247\u7f29\u7565\u56fe\u751f\u6210\u5e93\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7ef4\u5ea6<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u5b9a\u4f4d<\/td><td>\u4e13\u4e3a Java \u8bbe\u8ba1\u7684<strong>\u7f29\u7565\u56fe\u4e0e\u901a\u7528\u56fe\u7247\u5904\u7406<\/strong>\u5e93<\/td><\/tr><tr><td>\u4f5c\u8005<\/td><td>Chris Kwon\uff08coobird\uff09<\/td><\/tr><tr><td>\u5f00\u6e90<\/td><td>MIT \u534f\u8bae\uff0c<code>https:\/\/github.com\/coobird\/thumbnailator<\/code>&nbsp;5.3k+ star<\/td><\/tr><tr><td>\u4f53\u79ef<\/td><td>\u4ec5\u4e00\u4e2a JAR\uff08\u2248 200 KB\uff09\uff0c<strong>\u96f6\u5916\u90e8\u4f9d\u8d56<\/strong><\/td><\/tr><tr><td>\u6700\u65b0\u7248<\/td><td>0.4.20\uff082023-06 \u53d1\u5e03\uff0c\u517c\u5bb9 Java 17+ &amp; Spring Boot 3\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff1a\u5c31\u662f\u628a\u539f\u672c\u9700\u8981\u5199 100 \u884c\u7684\u56fe\u7247\u64cd\u4f5c\uff0c\u538b\u7f29\u6210 1 \u884c\u94fe\u5f0f\u4ee3\u7801\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5b83\u89e3\u51b3\u4e86\u4ec0\u4e48\u75db\u70b9\uff1f<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u75db\u70b9<\/th><th>Thumbnailator \u89e3\u6cd5<\/th><\/tr><\/thead><tbody><tr><td>\u7f29\u7565\u56fe\u5931\u771f<\/td><td>\u5185\u7f6e\u9ad8\u8d28\u91cf\u53cc\u7ebf\u6027\/\u53cc\u4e09\u6b21\u63d2\u503c\u7b97\u6cd5<\/td><\/tr><tr><td>\u4ee3\u7801\u81c3\u80bf<\/td><td>\u94fe\u5f0f\u8c03\u7528\uff0c\u4e00\u884c\u5b8c\u6210\u7f29\u653e\u3001\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u6c34\u5370<\/td><\/tr><tr><td>\u683c\u5f0f\u652f\u6301\u5c11<\/td><td>\u652f\u6301 JPEG\u3001PNG\u3001GIF\u3001BMP\u3001WEBP\uff08\u4f9d\u8d56\u63d2\u4ef6\uff09<\/td><\/tr><tr><td>\u6279\u91cf\u5904\u7406\u96be<\/td><td>\u76f4\u63a5\u4f20&nbsp;<code>File[]<\/code>&nbsp;\u6216&nbsp;<code>Directory<\/code>\uff0c\u4e00\u952e\u6279\u91cf<\/td><\/tr><tr><td>\u53ea\u7f29\u5c0f\u4e0d\u653e\u5927<\/td><td>\u5b98\u65b9\u672a\u63d0\u4f9b\uff0c\u793e\u533a\u5df2\u7ed9\u51fa&nbsp;<code>ImageFilter<\/code>&nbsp;\u65b9\u6848<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5305\u62ec github \u4e0a\u7684\u4ecb\u7ecd\uff0c\u65e0\u9700\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u56fe\u50cf I\/O API\u3001Java 2D API\u3001\u56fe\u50cf\u5904\u7406\u3001\u56fe\u50cf\u7f29\u653e\u7b49\u6280\u672f\u5373\u53ef\u5b9e\u73b0\u56fe\u7247\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5 \u5206\u949f\u5feb\u901f\u4e0a\u624b<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u770b\u4e00\u4e0b\u5982\u4f55\u4e0a\u624b\u3002\u6211\u4eca\u5929\u82b1\u4e86\u53ef\u4e0d\u6b62 5 \u5206\u949f\u5462\ud83e\udd23\uff0c\u9605\u8bfb\u5b98\u6587\u52a0 demo \u9879\u76ee\uff0c\u518d\u52a0\u5199\u6587\u7ae0\u524d\u540e\u6709\u4e00\u4e2a\u5c0f\u65f6\u4e86\ud83e\udd23\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u5f15\u5165\u4f9d\u8d56<\/strong><\/h3>\n\n\n\n<p>\u4e0a\u624b\u5f88\u7b80\u5355\uff0c\u5148\u5f15\u5165\u4f9d\u8d56\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>&lt;!-- Maven --&gt;<\/em><br>&lt;dependency&gt;<br>&nbsp; &nbsp;&nbsp;&lt;groupId&gt;net.coobird&lt;\/groupId&gt;<br>&nbsp; &nbsp;&nbsp;&lt;artifactId&gt;thumbnailator&lt;\/artifactId&gt;<br>&nbsp; &nbsp;&nbsp;&lt;version&gt;0.4.20&lt;\/version&gt;<br>&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/ Gradle<\/em><br>implementation&nbsp;'net.coobird:thumbnailator:0.4.20'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u4e00\u884c\u4ee3\u7801\u751f\u6210\u7f29\u7565\u56fe<\/strong><\/h3>\n\n\n\n<p>\u5199\u4e0b\u548c\u5b98\u7f51\u4e00\u6837\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Thumbnails.of(\"input.jpg\")<br>&nbsp; &nbsp; .size(300,&nbsp;300) &nbsp;&nbsp;<em>\/\/ \u6700\u5927\u8fb9 300\uff0c\u4fdd\u6301\u5bbd\u9ad8\u6bd4<\/em><br>&nbsp; &nbsp; .toFile(\"thumb.jpg\");<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u540e\uff0c\u5373\u53ef\u770b\u5230\u7f29\u7565\u56fe\u6548\u679c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6838\u5fc3\u73a9\u6cd5\u5927\u5168<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u5c3a\u5bf8\u8c03\u6574\u548c\u6bd4\u4f8b\u7f29\u653e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Thumbnails.of(\"input.jpg\").size(200,200).keepAspectRatio(false).toFile(\"thumb.jpg\");<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5982\u679c\u4e0d\u662f\u7b49\u6bd4\u4f8b\u7684\u8bdd\uff0c\u53ef\u80fd\u4f1a\u53d8\u5f62\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u65cb\u8f6c<\/strong><\/h3>\n\n\n\n<p>\u65cb\u8f6c\u5ea6\u6570\u6839\u636e\u9700\u8981\u8c03\u6574\uff0c\u9ed8\u8ba4\u662f\u987a\u65f6\u9488\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Thumbnails.of(\"input.jpg\")<br>&nbsp; &nbsp; .rotate(90) &nbsp; &nbsp; &nbsp;&nbsp;<em>\/\/ \u987a\u65f6\u9488 90\u00b0<\/em><br>&nbsp; &nbsp; .toFile(\"rot90.jpg\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u6dfb\u52a0\u6c34\u5370<\/strong><\/h3>\n\n\n\n<p>\u6c34\u5370\u53ef\u4ee5\u662f\u56fe\u7247\uff0c\u4e5f\u53ef\u4ee5\u662f\u6587\u5b57\uff0c\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u6c34\u5370\u7684\u900f\u660e\u5ea6 50%\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BufferedImage watermark = ImageIO.read(new&nbsp;File(\"wm.png\"));<br>Thumbnails.of(\"input.jpg\")<br>&nbsp; &nbsp; .size(800,&nbsp;600)<br>&nbsp; &nbsp; .watermark(Positions.BOTTOM_RIGHT, watermark,&nbsp;0.5f)&nbsp;<em>\/\/ \u900f\u660e\u5ea6 50%<\/em><br>&nbsp; &nbsp; .outputQuality(0.8f)<br>&nbsp; &nbsp; .toFile(\"wm.jpg\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u88c1\u526a<\/strong><\/h3>\n\n\n\n<p>\u56fe\u7247\u88c1\u526a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Thumbnails.of(\"input.jpg\")<br>&nbsp; &nbsp; .sourceRegion(Positions.CENTER,&nbsp;400,&nbsp;400)&nbsp;<em>\/\/ \u4e2d\u5fc3 400\u00d7400<\/em><br>&nbsp; &nbsp; .size(200,&nbsp;200)<br>&nbsp; &nbsp; .toFile(\"cut.jpg\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u683c\u5f0f\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p>\u56fe\u7247\u683c\u5f0f\u8f6c\u6362\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Thumbnails.of(\"input.jpg\")<br>&nbsp; &nbsp; .outputFormat(\"png\")<br>&nbsp; &nbsp; .toFile(\"output.png\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u8f93\u51fa\u5230\u6d41 \/ BufferedImage \/ Base64<\/strong><\/h3>\n\n\n\n<p>\u8f6c\u6362\u8f93\u51fa\u5230\u4e0d\u540c\u7684\u6d41\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/\/ \u8f93\u51fa\u5230 OutputStream\uff08\u9002\u7528\u4e8e Web \u4e0b\u8f7d\uff09<\/em><br>try&nbsp;(OutputStream os = response.getOutputStream()) {<br>&nbsp; &nbsp; Thumbnails.of(\"input.jpg\").scale(0.5).toOutputStream(os);<br>}<br><br><em>\/\/ \u8f93\u51fa\u5230 BufferedImage\uff08\u7ee7\u7eed\u4e8c\u6b21\u5904\u7406\uff09<\/em><br>BufferedImage bi = Thumbnails.of(\"input.jpg\").size(200,&nbsp;200).asBufferedImage();<br><br><em>\/\/ Base64 \u5b57\u7b26\u4e32<\/em><br>ByteArrayOutputStream bos =&nbsp;new&nbsp;ByteArrayOutputStream();<br>ImageIO.write(bi,&nbsp;\"jpg\", bos);<br>String base64 = Base64.getEncoder().encodeToString(bos.toByteArray());<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<\/strong><\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u4e0d\u9700\u8981\u4f7f\u7528\u7c7b\u5e93\uff0cJava \u4e2d\u7684 File \u5bf9\u8c61\u5c31\u6709\u957f\u5ea6\u5927\u5c0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File file =&nbsp;new&nbsp;File(\"thumb.jpg\");<br>long&nbsp;size = file.length();&nbsp;<em>\/\/ \u5355\u4f4d\uff1a\u5b57\u8282<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u53ea\u7f29\u5c0f\u4e0d\u653e\u5927<\/strong><\/h2>\n\n\n\n<p>\u5b98\u65b9&nbsp;<code>.size()<\/code>&nbsp;\u4f1a<strong>\u5f3a\u5236<\/strong>\u8d34\u5408\u6846\uff0c\u5c0f\u56fe\u4f1a\u88ab\u653e\u5927\u3002\u793e\u533a\u65b9\u6848\uff1a\u81ea\u5b9a\u4e49&nbsp;<code>ImageFilter<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u5173\u952e\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public&nbsp;class&nbsp;NoScaleUpFilter&nbsp;implements&nbsp;ImageFilter&nbsp;{<br>&nbsp; &nbsp;&nbsp;privatefinalint&nbsp;maxWidth;<br>&nbsp; &nbsp;&nbsp;privatefinalint&nbsp;maxHeight;<br><br>&nbsp; &nbsp;&nbsp;public&nbsp;NoScaleUpFilter(int&nbsp;maxWidth,&nbsp;int&nbsp;maxHeight)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.maxWidth = maxWidth;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;this.maxHeight = maxHeight;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp;&nbsp;@Override<br>&nbsp; &nbsp;&nbsp;public&nbsp;BufferedImage&nbsp;apply(BufferedImage img)&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(img.getWidth() &lt;= maxWidth &amp;&amp; img.getHeight() &lt;= maxHeight) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;img;&nbsp;<em>\/\/ \u76f4\u63a5\u8fd4\u56de\u539f\u56fe<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;try&nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;Thumbnails.of(img).size(maxWidth, maxHeight).asBufferedImage();<br>&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;catch&nbsp;(IOException e) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;thrownew&nbsp;RuntimeException(e);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }<br>}<br><br><em>\/\/ \u4f7f\u7528<\/em><br>Thumbnails.of(\"small.jpg\")<br>&nbsp; &nbsp; .scale(1f)<br>&nbsp; &nbsp; .addFilter(new&nbsp;NoScaleUpFilter(300,&nbsp;300))<br>&nbsp; &nbsp; .toFile(\"small_thumb.jpg\");<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>GIF \u538b\u7f29\u4e13\u9898<\/strong><\/h2>\n\n\n\n<p>GIF \u662f\u591a\u5e27\u52a8\u753b\uff0cThumbnailator&nbsp;<strong>\u53ea\u80fd\u5904\u7406\u5355\u5e27<\/strong>\u3002\u65b9\u6848 =&nbsp;<strong>\u62c6\u5e27 \u2192 \u9010\u5e27\u538b\u7f29 \u2192 \u91cd\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u7a0d\u5fae\u6709\u4e9b\u9ebb\u70e6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u5f15\u5165 GIF \u7f16\u89e3\u7801\u5e93<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;<br>&nbsp; &nbsp;&nbsp;&lt;groupId&gt;com.madgag&lt;\/groupId&gt;<br>&nbsp; &nbsp;&nbsp;&lt;artifactId&gt;animated-gif-lib&lt;\/artifactId&gt;<br>&nbsp; &nbsp;&nbsp;&lt;version&gt;1.4&lt;\/version&gt;<br>&lt;\/dependency&gt;<\/code><\/pre>\n\n\n\n<p>Gradle \u81ea\u5df1\u6539\u4e00\u4e0b\u54c8\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><\/strong><strong>\u5b8c\u6574\u5de5\u5177\u7c7b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public&nbsp;staticbyte&#91;] compressGif(byte&#91;] src,&nbsp;int&nbsp;maxSide)&nbsp;throws&nbsp;IOException {<br>&nbsp; &nbsp; GifDecoder decoder =&nbsp;new&nbsp;GifDecoder();<br>&nbsp; &nbsp; decoder.read(new&nbsp;ByteArrayInputStream(src));<br>&nbsp; &nbsp;&nbsp;int&nbsp;count = decoder.getFrameCount();<br>&nbsp; &nbsp;&nbsp;if&nbsp;(count ==&nbsp;1) {&nbsp;<em>\/\/ \u5355\u5e27\u5f53\u9759\u6001\u56fe<\/em><br>&nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;Thumbnails.of(ImageIO.read(new&nbsp;ByteArrayInputStream(src)))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.size(maxSide, maxSide)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.outputQuality(0.8)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.asBufferedImage();<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; Dimension dim = decoder.getFrameSize();<br>&nbsp; &nbsp;&nbsp;float&nbsp;scale = Math.min(1f, (float) maxSide \/ Math.max(dim.width, dim.height));<br>&nbsp; &nbsp;&nbsp;int&nbsp;w = (int) (dim.width * scale);<br>&nbsp; &nbsp;&nbsp;int&nbsp;h = (int) (dim.height * scale);<br><br>&nbsp; &nbsp; ByteArrayOutputStream out =&nbsp;new&nbsp;ByteArrayOutputStream();<br>&nbsp; &nbsp; AnimatedGifEncoder encoder =&nbsp;new&nbsp;AnimatedGifEncoder();<br>&nbsp; &nbsp; encoder.start(out);<br>&nbsp; &nbsp; encoder.setRepeat(0);<br>&nbsp; &nbsp; encoder.setSize(w, h);<br><br>&nbsp; &nbsp;&nbsp;for&nbsp;(int&nbsp;i =&nbsp;0; i &lt; count; i++) {<br>&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage frame = decoder.getFrame(i);<br>&nbsp; &nbsp; &nbsp; &nbsp; BufferedImage compressed = Thumbnails.of(frame).size(w, h).asBufferedImage();<br>&nbsp; &nbsp; &nbsp; &nbsp; encoder.setDelay(decoder.getDelay(i));<br>&nbsp; &nbsp; &nbsp; &nbsp; encoder.addFrame(compressed);<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; encoder.finish();<br>&nbsp; &nbsp;&nbsp;return&nbsp;out.toByteArray();<br>}<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec8\u6548\u679c\uff1a\u5c06 5 MB\u30011280\u00d7720 \u7684 GIF \u538b\u6210 480\u00d7270\u3001800 KB\uff0c\u5e27\u7387\u4e0e\u64ad\u653e\u901f\u5ea6\u4e0d\u53d8\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u8e29\u5751 &amp; \u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u6211\u5728\u4f7f\u7528\u4e2d\uff0c\u4ee5\u53ca github \u4e0a\u6709\u4eba\u63d0\u5230\u7684\u5751\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5751<\/th><th>\u8bf4\u660e<\/th><th>\u89e3\u51b3<\/th><\/tr><\/thead><tbody><tr><td>PNG \u8f6c JPG \u80cc\u666f\u53d8\u9ed1<\/td><td>JPG \u4e0d\u652f\u6301\u900f\u660e<\/td><td>\u5148&nbsp;<code>.outputFormat(\"jpg\").addFilter(new CanvasFilter(Color.WHITE))<\/code><\/td><\/tr><tr><td>\u538b\u7f29\u540e\u4f53\u79ef\u53cd\u800c\u53d8\u5927<\/td><td>\u7f29\u653e+\u683c\u5f0f\u8f6c\u6362\u5bfc\u81f4<\/td><td><strong>\u540c\u683c\u5f0f\u538b\u7f29\u6700\u7a33<\/strong>\uff1aJPG\u2192JPG\uff0cPNG\u2192PNG<\/td><\/tr><tr><td>\u6279\u91cf\u547d\u540d\u51b2\u7a81<\/td><td>\u9ed8\u8ba4\u8986\u76d6<\/td><td>\u4f7f\u7528&nbsp;<code>.toFiles(Rename.PREFIX_DOT_THUMBNAIL)<\/code>&nbsp;\u81ea\u52a8\u52a0\u524d\u7f00<\/td><\/tr><tr><td>\u9ad8\u5e76\u53d1 OOM<\/td><td>\u5927\u56fe\u7247\u76f4\u63a5\u52a0\u8f7d<\/td><td>\u5148&nbsp;<code>.size(max, max)<\/code>&nbsp;\u518d\u8bfb\uff0c\u6216\u5f00\u542f&nbsp;<code>-Xmx<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u4f7f\u7528\u4e0b\u6765\uff0c\u786e\u5b9e\u65b9\u4fbf\u3002\u76f8\u6bd4\u6211\u4e4b\u524d\u6587\u7ae0\u4e2d\u7684\u4ee3\u7801\uff0c\u7c7b\u5e93\u7b80\u5316\u4e86\u6211\u4eec\u76f4\u63a5\u64cd\u4f5c ImageIO \u7684\u4e00\u4e9b api\u3002\u67d0\u4e9b\u6548\u679c\u4e5f\u6bd4\u6211\u81ea\u8eab\u5b9e\u73b0\u7684\u597d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350<\/th><\/tr><\/thead><tbody><tr><td>\u5feb\u901f\u751f\u6210\u7f29\u7565\u56fe\/\u6c34\u5370<\/td><td>Thumbnailator \u4e00\u884c\u4ee3\u7801<\/td><\/tr><tr><td>\u590d\u6742\u5408\u6210\u3001\u6ee4\u955c\u3001\u6587\u5b57<\/td><td>\u53ef\u7ed3\u5408 Java 2D \u6216\u5207\u6362\u5230 Marvin\/FilthyRichClients<\/td><\/tr><tr><td>\u6d77\u91cf\u5206\u5e03\u5f0f\u5904\u7406<\/td><td>Thumbnailator \u5355\u673a + \u6d88\u606f\u961f\u5217\uff0c\u6216\u8f6c\u5b58 OSS \u89e6\u53d1\u4e91\u7aef\u5904\u7406<\/td><\/tr><tr><td>Spring Boot 3<\/td><td>\u76f4\u63a5\u4f7f\u7528 0.4.20\uff0c\u5df2\u9a8c\u8bc1\u517c\u5bb9 Java 17<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4ee5\u4e0a\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u5927\u5bb6\u6709\u7528\uff01<\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u5199\u4e86\u4e00\u7bc7\u6587\u7ae0\u300aJava \u56fe\u7247\u7b49\u6bd4\u4f8b\u7f29\u653e\u4ee5\u53ca\u6309\u50cf\u7d20\u538b\u7f29\u300b\uff0c\u4eca\u5929\u6709\u7f51\u53cb\u770b\u5230\u4e86\uff0c\u7ed9\u6211\u56de\u590d\u8bf4\uff0c\u5e94\u8be5\u662f\u4f7f\u7528 Thumbnailato &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-6588","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"post_thumbnail_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","content_first_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_medium_image_300":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_thumbnail_image_624":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_frist_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_medium_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_large_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_full_image":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","post_all_images":[{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","id":"image0"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/TLH3CicPVibrcR44Hc0pbCbT9SBfDuYLAvlBZBxTssVqC7iagcyuZzeDSiasxfq6vd6uYF7DlBTCJW6tATCgfs9fCA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=0","id":"image1"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Spring Boot","post_date":"2025-09-29","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":303,"next_post_id":6565,"next_post_title":"SpringBoot\u6574\u5408\u5fae\u4fe1\u767b\u5f55\uff1aOpenID\u4e0eUnionID\u83b7\u53d6\u5168\u6d41\u7a0b\u89e3\u6790","previous_post_id":6496,"previous_post_title":"Spring Boot\u63a5\u53e3\u6ce8\u89e3\u5b9e\u73b0\uff1a\u5206\u5e03\u5f0f\u9501\u3001\u9650\u6d41\u3001\u8bb0\u5f55\u3001\u65e5\u5fd7\u3001\u9632\u6296\u3001\u9632\u91cd","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6588"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6588\/revisions"}],"predecessor-version":[{"id":6589,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6588\/revisions\/6589"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}