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

refactor: preserve original image if smaller than requested thumbnail size #6582

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.halo.app.core.attachment;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -74,10 +76,14 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
throw new UnsupportedOperationException(
"Unsupported image format for: " + formatNameOpt.orElse("unknown"));
}
var thumbnail = Scalr.resize(img, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH,
size.getWidth());
var formatName = formatNameOpt.orElse("jpg");
var thumbnailFile = getThumbnailFile(formatName);
if (img.getWidth() <= size.getWidth()) {
Files.copy(tempImagePath, thumbnailFile.toPath(), REPLACE_EXISTING);
return;
}
var thumbnail = Scalr.resize(img, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH,
size.getWidth());
ImageIO.write(thumbnail, formatName, thumbnailFile);
}

Expand Down
Loading