/*
* Copyright (C) 2008-2020, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see BufferedImage
, same as ImageIO.read()
does.
*
*
* JPEG images with EXIF metadata are rotated according to Orientation tag.
*
* @param imageFile a File
to read from.
*/
private static BufferedImage readImageWithOrientation(File imageFile) throws IOException {
BufferedImage image = ImageIO.read(imageFile);
if (!FilenameUtils.getExtension(imageFile.getName()).equals("jpg")) {
return image;
}
try {
ImageMetadata metadata = Imaging.getMetadata(imageFile);
if (metadata instanceof JpegImageMetadata) {
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
TiffField orientationField = jpegMetadata.findEXIFValue(TiffTagConstants.TIFF_TAG_ORIENTATION);
if (orientationField != null) {
int orientation = orientationField.getIntValue();
switch (orientation) {
case TiffTagConstants.ORIENTATION_VALUE_ROTATE_90_CW:
image = Scalr.rotate(image, Rotation.CW_90);
break;
case TiffTagConstants.ORIENTATION_VALUE_ROTATE_180:
image = Scalr.rotate(image, Rotation.CW_180);
break;
case TiffTagConstants.ORIENTATION_VALUE_ROTATE_270_CW:
image = Scalr.rotate(image, Rotation.CW_270);
break;
case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL:
image = Scalr.rotate(image, Rotation.FLIP_HORZ);
break;
case TiffTagConstants.ORIENTATION_VALUE_MIRROR_VERTICAL:
image = Scalr.rotate(image, Rotation.FLIP_VERT);
break;
case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_90_CW:
image = Scalr.rotate(Scalr.rotate(image, Rotation.FLIP_HORZ), Rotation.CW_90);
break;
case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_270_CW:
image = Scalr.rotate(Scalr.rotate(image, Rotation.FLIP_HORZ), Rotation.CW_270);
break;
case TiffTagConstants.ORIENTATION_VALUE_HORIZONTAL_NORMAL:
default:
// do nothing
break;
}
}
}
} catch (ImageReadException | IOException e) {
// failed to read metadata.
// nothing to do here, return image as is.
}
return image;
}
@Override
public void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException {
String ext = FilenameUtils.getExtension(outputFilename);
Path outputImagePath = Paths.get(fullImageDir, outputFilename);
// this throws strange exceptions
// Files.move(Paths.get(tmpDir, tempFilename), outputImagePath);
FileUtils.moveFile(Paths.get(tmpDir, tempFilename).toFile(), outputImagePath.toFile());
BufferedImage originalImage = readImageWithOrientation(outputImagePath.toFile());
int width = originalImage.getWidth();
int height = originalImage.getHeight();
int maxDimension = Math.max(width, height);
BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage;
BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage;
BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage;
ImageIO.write(image1024, ext, Paths.get(mediumImageDir, outputFilename).toFile());
ImageIO.write(image0512, ext, Paths.get(smallImageDir, outputFilename).toFile());
ImageIO.write(image0160, ext, Paths.get(thumbnailImageDir, outputFilename).toFile());
}
private String getAvatarFileName(User user, String targetExtension) {
return String.format("%s.%s", user.getUid(), targetExtension);
}
private Path getAvatarPath(User user) {
return Paths.get(avatarDir, getAvatarFileName(user, "png"));
}
public void saveAvatar(String tempFilename, User user) throws IOException {
String ext = FilenameUtils.getExtension(tempFilename);
String originalName = getAvatarFileName(user, ext);
Path originalPath = Paths.get(avatarOriginalDir, originalName);
Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING);
BufferedImage originalImage = ImageIO.read(originalPath.toFile());
String targetExt = "png";
ImageIO.write(Scalr.resize(originalImage, 96), targetExt, getAvatarPath(user).toFile());
ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(avatarSmallDir, getAvatarFileName(user, targetExt)).toFile());
}
public Attachment getAttachment(File imgFile) throws IOException {
Attachment attachment = new Attachment();
if (imgFile.exists()) {
try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) {
Iterator