Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加图片压缩功能 #256

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/b3log/solo/bolo/pic/PicUploadProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.b3log.solo.bolo.pic.util.UploadUtil;
import org.b3log.solo.model.Option;
import org.b3log.solo.repository.OptionRepository;
import org.b3log.solo.util.Images;
import org.b3log.solo.util.Solos;

import javax.servlet.ServletContext;
Expand Down Expand Up @@ -97,6 +98,12 @@ public void uploadPicture(final RequestContext context) {
} catch (Exception e) {
config = "hacpai";
}
String value;
try {
value = optionRepository.get(Option.ID_C_IMAGE_UPLOAD_COMPRESS).optString(Option.OPTION_VALUE);
} catch (Exception e) {
value = "10";
}
final ServletContext servletContext = SoloServletListener.getServletContext();
final String assets = "/";
String path = servletContext.getResource(assets).getPath();
Expand All @@ -106,7 +113,8 @@ public void uploadPicture(final RequestContext context) {
item.write(file);
item.delete();
try {
String url = UploadUtil.upload(config, file);

String url = UploadUtil.upload(config, Images.compressImage(file, Float.parseFloat(value)));
if (url.isEmpty()) {
url = "接口调用错误,请检查偏好设置-自定义图床配置,清除浏览器缓存并重启服务端。";
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/b3log/solo/bolo/prop/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public static List<String> loadOptions() {
Option.ID_C_ENABLE_AUTO_FLUSH_GITHUB,
Option.ID_C_MY_GITHUB_ID,
Option.ID_C_SEND_KEY,
Option.ID_C_HELP_IMPROVE_PLAN
Option.ID_C_HELP_IMPROVE_PLAN,
Option.ID_C_IMAGE_UPLOAD_COMPRESS,
Option.ID_C_THUMB_COMPRESS
);
return optionList;
}
Expand Down Expand Up @@ -161,7 +163,9 @@ public static List<Object[]> loadOptList(JSONObject requestJSONObject) {
new Object[]{Option.ID_C_ENABLE_AUTO_FLUSH_GITHUB, Option.CATEGORY_C_PREFERENCE, "false"},
new Object[]{Option.ID_C_MY_GITHUB_ID, Option.CATEGORY_C_PREFERENCE, ""},
new Object[]{Option.ID_C_SEND_KEY, Option.CATEGORY_C_PREFERENCE, ""},
new Object[]{Option.ID_C_HELP_IMPROVE_PLAN, Option.CATEGORY_C_PREFERENCE, ""}
new Object[]{Option.ID_C_HELP_IMPROVE_PLAN, Option.CATEGORY_C_PREFERENCE, ""},
new Object[]{Option.ID_C_IMAGE_UPLOAD_COMPRESS, Option.CATEGORY_C_PREFERENCE, "1.0"},
new Object[]{Option.ID_C_THUMB_COMPRESS, Option.CATEGORY_C_PREFERENCE, "1.0"}
);
return optList;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/b3log/solo/model/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ public final class Option {
*/
public static final String ID_C_ARTICLE_LIST_STYLE = "articleListStyle";

/**
* Article Image Upload Compress.
*/
public static final String ID_C_IMAGE_UPLOAD_COMPRESS = "imageUploadCompress";

/**
* Article Thumb Compress.
*/
public static final String ID_C_THUMB_COMPRESS = "thumbCompress";


/**
* Key of article/page comment-able.
*/
Expand Down Expand Up @@ -612,6 +623,12 @@ public static final class DefaultPreference {
*/
public static final String DEFAULT_MAX_ARCHIVE = "-1";


public static final String DEFAULT_IMAGE_UPLOAD_COMPRESS = "10";


public static final String DEFAULT_THUMB_COMPRESS = "10";

/**
* Default B3log username.
*/
Expand Down
55 changes: 52 additions & 3 deletions src/main/java/org/b3log/solo/util/Images.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@
*/
package org.b3log.solo.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;

import org.b3log.solo.bolo.pic.util.UploadUtil;
import org.b3log.solo.bolo.prop.Options;
import org.b3log.solo.model.Option;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -92,24 +103,62 @@ public static String imageSize(final String imageURL, final int width, final int
return imageURL + "?imageView2/1/w/" + width + "/h/" + height + "/interlace/1/q/100";
}

public static File compressImage(File inputFile, float quality) throws IOException {
BufferedImage image = ImageIO.read(inputFile);
File compressedFile = new File(inputFile.getParent(), "compressed_" + inputFile.getName());

ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(compressedFile);
jpgWriter.setOutput(ios);

ImageWriteParam param = jpgWriter.getDefaultWriteParam();
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
}
jpgWriter.write(null, new javax.imageio.IIOImage(image, null, null), param);
ios.close();
jpgWriter.dispose();

LOGGER.log(Level.INFO, "Temp Image " + inputFile.getName() + " Delete [" + inputFile.delete() + "]");
return compressedFile;
}


/**
* Gets an image URL randomly. Sees https://github.com/b3log/bing for more details.
*
* @return an image URL
*/
public static String randImage() {
// 修改
try {
final long min = DateUtils.parseDate("20171104", new String[]{"yyyyMMdd"}).getTime();
final long max = System.currentTimeMillis();
final long delta = max - min;
final long time = ThreadLocalRandom.current().nextLong(0, delta) + min;

return COMMUNITY_FILE_URL + "/bing/" + DateFormatUtils.format(time, "yyyyMMdd") + ".jpg";
String imageName = DateFormatUtils.format(time, "yyyyMMdd") + ".jpg";
String B3logImageURL = COMMUNITY_FILE_URL + "/bing/" + imageName;
String config = Options.get(Option.ID_C_TUCHUANG_CONFIG);
String value = Options.get(Option.ID_C_THUMB_COMPRESS);

if (!config.equals("hacpai") && !config.isEmpty()) {
File file = new File("temp/tmp_" + imageName);
// FileNotFoundException
FileUtils.copyURLToFile(new URL(B3logImageURL), file);
return UploadUtil.upload(config, compressImage(file, Float.parseFloat(value)));
}
return B3logImageURL;

} catch (final FileNotFoundException e) {
LOGGER.log(Level.ERROR, "Remote image resource lost", e);
return COMMUNITY_FILE_URL + "/bing/20171104.jpg";
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Generates random image URL failed", e);

return COMMUNITY_FILE_URL + "/bing/20171104.jpg";
}

}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/lang_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ allowCommentLabel=\u5141\u8BB8\u8BC4\u8BBA
feedOutputModel1Label=\u8BA2\u9605\u8F93\u51FA\u6A21\u5F0F:
feedOutputCntLabel=\u8BA2\u9605\u8F93\u51FA\u6587\u7AE0\u6570
feedOutputCnt1Label=\u8BA2\u9605\u8F93\u51FA\u6587\u7AE0\u6570:
imageUploadCompressLabel=\u56fe\u7247\u4e0a\u4f20\u538b\u7f29\uff08\u0030\u002d\u0031\uff09:
thumbCompressLabel=\u6587\u7ae0\u5c01\u9762\u538b\u7f29\uff08\u0030\u002d\u0031\uff09:
customVars1Label=<a href="https://github.com/b3log/solo/issues/12535" target="_blank">\u81EA\u5B9A\u4E49\u6A21\u677F\u53D8\u91CF</a>:
abstractLabel=\u6458\u8981
fullContentLabel=\u5168\u6587
Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/admin/admin-preference.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
</select>
<label for="feedOutputCnt">${feedOutputCnt1Label}</label>
<input id="feedOutputCnt" class="normalInput" type="text"/>
<label for="imageUploadCompress">${imageUploadCompressLabel}</label>
<input id="imageUploadCompress" class="normalInput" type="text"/>
<label for="thumbCompress">${thumbCompressLabel}</label>
<input id="thumbCompress" class="normalInput" type="text"/>
<label for="customVars">${customVars1Label}</label>
<input id="customVars" class="normalInput" type="text"/>
</details>
Expand Down
Loading
Loading