Java tutorial
/* * Copyright (c) 2016 OpenDesign All rights reserved. * * This software is the confidential and proprietary information of OpenDesign. * You shall not disclose such Confidential Information and shall use it * only in accordance with the terms of the license agreement you entered into * with OpenDesign. */ package com.opendesign.utils; import java.awt.image.BufferedImage; import java.io.File; import java.util.List; import javax.imageio.ImageIO; import org.apache.commons.lang3.StringUtils; import net.coobird.thumbnailator.Thumbnails; /** * <pre> * ?? * </pre> * * @author hanchanghao * @since 2016. 11. 15. */ public class ThumbnailManager { /** * (??/?) ?? ( ? ?? ? [? ? ? ] ) * ? ? (228 x 169) */ private static final int IMG_WIDTH_DESIGN_WORK_MEDIUM = 228; /** * (?? ?) (? ? ?) * ? ? (1084 x 677) */ private static final int IMG_WIDTH_DESIGN_WORK_LARGE = 1084; /** * (?) (?? width) * ? ? (192 x ) */ private static final int IMG_WIDTH_PROJECT_WORK_SMALL = 192; /** * (?) (?? ? ? ??) * ? ? (268 x 109) */ private static final int IMG_WIDTH_PROJECT_WORK_MEDIUM = 268; /** * (?) (? ? ?) * ? ? (956 x ) */ private static final int IMG_WIDTH_PROJECT_WORK_LARGE = 956; /** * ??(??/? ) ? Suffix - Medium() ? */ public static final String SUFFIX_DESIGN_WORK_MEDIUM = "_DE_M"; /** * ??(??/? ) ? Suffix - Big */ public static final String SUFFIX_DESIGN_WORK_LARGE = "_DE_L"; /** * ??(? ) ? Suffix - Small ? */ public static final String SUFFIX_PROJECT_WORK_SMALL = "_PR_S"; /** * ??(? ) ? Suffix - Medium() ? */ public static final String SUFFIX_PROJECT_WORK_MEDIUM = "_PR_M"; /** * ??(? ) ? Suffix - Big ? */ public static final String SUFFIX_PROJECT_WORK_LARGE = "_PR_L"; /** * (??/?) ( ? ?? ?) * ? ? (228 x 169) * @param filePath */ public static void saveThumbDesignWorkMedium(String filePath) { try { resizeNSaveThumbnail(IMG_WIDTH_DESIGN_WORK_MEDIUM, new File(filePath), SUFFIX_DESIGN_WORK_MEDIUM); } catch (Exception e) { e.printStackTrace(); } } /** * (?? ?) (? ? ?) * ? ? (1084 x 677) * @param filePath */ public static void saveThumbDesignWorkLarge(String filePath) { try { resizeNSaveThumbnail(IMG_WIDTH_DESIGN_WORK_LARGE, new File(filePath), SUFFIX_DESIGN_WORK_LARGE); } catch (Exception e) { e.printStackTrace(); } } /** * (?) (?? width) * ? ? (192 x ) * @param filePath */ public static void saveThumbProjectWorkSmall(String filePath) { try { resizeNSaveThumbnail(IMG_WIDTH_PROJECT_WORK_SMALL, new File(filePath), SUFFIX_PROJECT_WORK_SMALL); } catch (Exception e) { e.printStackTrace(); } } /** * (?) (?? ? ? ??) * ? ? (268 x 109) * @param filePath */ public static void saveThumbProjectWorkMedium(String filePath) { try { resizeNSaveThumbnail(IMG_WIDTH_PROJECT_WORK_MEDIUM, new File(filePath), SUFFIX_PROJECT_WORK_MEDIUM); } catch (Exception e) { e.printStackTrace(); } } /** * (?) (? ? ?) * ? ? (956 x ) * @param filePath */ public static void saveThumbProjectWorkLarge(String filePath) { try { resizeNSaveThumbnail(IMG_WIDTH_PROJECT_WORK_LARGE, new File(filePath), SUFFIX_PROJECT_WORK_LARGE); } catch (Exception e) { e.printStackTrace(); } } /** * ?? ? * ? width ? resize ?. * @param width * @param imageFile * @param suffix * @throws Exception */ private static void resizeNSaveThumbnail(double width, File imageFile, String suffix) throws Exception { BufferedImage originalImage = ImageIO.read(imageFile); double originalWidth = originalImage.getWidth(); double originalHeight = originalImage.getHeight(); double resizableWidth = originalWidth; double resizableHeight = originalHeight; String extension = imageFile.getName().substring(imageFile.getName().indexOf(".") + 1); String fileName = imageFile.getName().substring(0, imageFile.getName().indexOf(".")); String target = imageFile.getAbsolutePath(); target = target.substring(0, target.lastIndexOf(File.separator) + 1) + fileName + suffix + "." + extension; if (originalWidth > width) { resizableWidth = width; double ratio = (double) width / (double) originalImage.getWidth(); resizableHeight = originalImage.getHeight() * ratio; Thumbnails.of(originalImage).size((int) resizableWidth, (int) resizableHeight).outputFormat(extension) .toFile(target); } else { CmnUtil.fileCopy(imageFile.getAbsolutePath(), target); } } /** * ??/? ? ?? * @param thumbnail * @param productFileList */ public static void resizeNClone4DesignWork(String thumbnailPath, List<String> productFilePaths) { File thumbFile = new File(thumbnailPath); if (StringUtils.isNotEmpty(thumbnailPath) && isImageFile(thumbFile)) { saveThumbDesignWorkMedium(thumbnailPath); //? ?? saveThumbDesignWorkLarge(thumbnailPath); //? ?? } for (String filePath : productFilePaths) { File productFile = new File(filePath); if (StringUtils.isNotEmpty(filePath) && isImageFile(productFile)) { saveThumbDesignWorkLarge(filePath); //? } } } /** * ?? ?(Full path) * * @param filePath - fullPath * @param suffix - {@link ThumbnailManager#SUFFIX_DESIGN_WORK_MEDIUM} so on.... * @return */ public static String getThumbnail(String filePath, String suffix) { if (StringUtils.isEmpty(filePath)) { return filePath; } String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1); fileName = fileName.substring(0, fileName.indexOf(".")); String extension = filePath.substring(filePath.indexOf(".") + 1); String filePathWithSuffix = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1) + fileName + suffix + "." + extension; return filePathWithSuffix; } /** * <pre> * ? ? ?. * * ? ?? ?, * png ? ? ?? ? , * ? ? ? ? * </pre> * @param file * @return */ public static boolean isImageFile(File file) { /* String mimeType = new MimetypesFileTypeMap().getContentType( file ); // mimeType should now be something like "image/png" System.out.println("mimetype=" + mimeType + " file=" + file); return mimeType.substring(0,5).equalsIgnoreCase("image"); */ String[] validSuffixes = { "jpg", "jpeg", "png", "gif", "bmp" }; for (String suffix : validSuffixes) { String filePath = file.getName().toLowerCase(); if (filePath.endsWith(suffix)) { return true; } } return false; } public static void main(String[] args) { // //product ? // saveThumbDesignWorkMedium(thumbnailPath); //? ?? // saveThumbDesignWorkLarge(thumbnailPath); //? ?? // // //project ? // saveThumbDesignWorkMedium(thumbnailPath); //? ?? // saveThumbProjectWorkMedium(thumbnailPath); //? ?? // // //work ? // saveThumbProjectWorkSmall(thumbnailPath); //? ?? // saveThumbProjectWorkLarge(thumbnailPath); //? ?? File productFolder = new File( "/Users/windfall/Desktop/00_working/02_?/02_01_OpenDesign/02_01_100_temp/km_upload/product"); int updated = 0; int total = 0; for (File image : productFolder.listFiles()) { if (image != null) { System.out.println("resizing " + image.getAbsolutePath()); saveThumbDesignWorkMedium(image.getAbsolutePath()); //? ?? saveThumbDesignWorkLarge(image.getAbsolutePath()); //? ?? updated++; } else { System.out.println("Fail=" + image); } total++; } System.out.println("Product Files=[" + total + "] updated=[" + updated + "]"); updated = 0; total = 0; File projectFolder = new File( "/Users/windfall/Desktop/00_working/02_?/02_01_OpenDesign/02_01_100_temp/km_upload/project"); for (File image : projectFolder.listFiles()) { if (image != null) { System.out.println("resizing " + image.getAbsolutePath()); saveThumbDesignWorkMedium(image.getAbsolutePath()); //? ?? saveThumbProjectWorkMedium(image.getAbsolutePath()); //? ?? updated++; } else { System.out.println("Fail=" + image); } total++; } System.out.println("Project Files=[" + total + "] updated=[" + updated + "]"); updated = 0; total = 0; File workFolder = new File( "/Users/windfall/Desktop/00_working/02_?/02_01_OpenDesign/02_01_100_temp/km_upload/project_work_file"); for (File image : workFolder.listFiles()) { if (image != null) { System.out.println("resizing " + image.getAbsolutePath()); saveThumbProjectWorkSmall(image.getAbsolutePath()); //? ?? saveThumbProjectWorkLarge(image.getAbsolutePath()); //? ?? updated++; } else { System.out.println("Fail=" + image); } total++; } System.out.println("Work Files=[" + total + "] updated=[" + updated + "]"); } }