Commit 9c92af97 by songxiang

OSS增加图片裁剪方法同时持久化

parent 0a626b44
...@@ -468,7 +468,8 @@ public class FileUtils { ...@@ -468,7 +468,8 @@ public class FileUtils {
/** /**
* 根据路径删除指定的目录或文件,无论存在与否 * 根据路径删除指定的目录或文件,无论存在与否
* *
* @param sPath 要删除的目录或文件 * @param sPath
* 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。 * @return 删除成功返回 true,否则返回 false。
*/ */
public static boolean deleteFolder(String sPath) { public static boolean deleteFolder(String sPath) {
...@@ -493,7 +494,8 @@ public class FileUtils { ...@@ -493,7 +494,8 @@ public class FileUtils {
/** /**
* 删除单个文件 * 删除单个文件
* *
* @param sPath 被删除文件的文件名 * @param sPath
* 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false * @return 单个文件删除成功返回true,否则返回false
*/ */
public static boolean deleteFile(String sPath) { public static boolean deleteFile(String sPath) {
...@@ -513,7 +515,8 @@ public class FileUtils { ...@@ -513,7 +515,8 @@ public class FileUtils {
/** /**
* 删除目录(文件夹)以及目录下的文件 * 删除目录(文件夹)以及目录下的文件
* *
* @param sPath 被删除目录的文件路径 * @param sPath
* 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false * @return 目录删除成功返回true,否则返回false
*/ */
public static boolean deleteDirectory(String sPath) { public static boolean deleteDirectory(String sPath) {
...@@ -687,8 +690,10 @@ public class FileUtils { ...@@ -687,8 +690,10 @@ public class FileUtils {
/** /**
* 合并两个文件 * 合并两个文件
* *
* @param outFile 目标文件 * @param outFile
* @param leafFile 源文件 * 目标文件
* @param leafFile
* 源文件
*/ */
public static void mergeFiles(File outFile, File leafFile) { public static void mergeFiles(File outFile, File leafFile) {
FileOutputStream fos = null; FileOutputStream fos = null;
...@@ -705,7 +710,7 @@ public class FileUtils { ...@@ -705,7 +710,7 @@ public class FileUtils {
fos = new FileOutputStream(outFile, true); fos = new FileOutputStream(outFile, true);
fis = new FileInputStream(leafFile); fis = new FileInputStream(leafFile);
int len = 0; int len = 0;
for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1; ) { for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1;) {
fos.write(buf, 0, len); fos.write(buf, 0, len);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
...@@ -724,8 +729,10 @@ public class FileUtils { ...@@ -724,8 +729,10 @@ public class FileUtils {
/** /**
* 合并多个文件 * 合并多个文件
* *
* @param outFile 输出文件, * @param outFile
* @param leafFiles 文件碎片集 * 输出文件,
* @param leafFiles
* 文件碎片集
*/ */
public static void mergeFileList(File outFile, List<File> leafFiles) { public static void mergeFileList(File outFile, List<File> leafFiles) {
FileOutputStream fos = null; FileOutputStream fos = null;
...@@ -764,9 +771,12 @@ public class FileUtils { ...@@ -764,9 +771,12 @@ public class FileUtils {
/** /**
* 音频文件转换 * 音频文件转换
* *
* @param data 音频byte数组 * @param data
* @param sourExt 原始后缀 * 音频byte数组
* @param ext 新后缀 * @param sourExt
* 原始后缀
* @param ext
* 新后缀
* @return * @return
* @author PENG * @author PENG
*/ */
...@@ -1093,7 +1103,8 @@ public class FileUtils { ...@@ -1093,7 +1103,8 @@ public class FileUtils {
/** /**
* 从输入流中获取数据 * 从输入流中获取数据
* *
* @param inStream 输入流 * @param inStream
* 输入流
* @return * @return
* @throws Exception * @throws Exception
*/ */
......
...@@ -11,10 +11,11 @@ import java.math.BigDecimal; ...@@ -11,10 +11,11 @@ import java.math.BigDecimal;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.dcg.util.StringUtils;
import com.itextpdf.text.Image; import com.itextpdf.text.Image;
import com.pcloud.common.constant.AliyunConstant; import com.pcloud.common.constant.AliyunConstant;
import com.pcloud.common.constant.FilePathConst; import com.pcloud.common.constant.FilePathConst;
...@@ -440,6 +441,43 @@ public class ImageUtils { ...@@ -440,6 +441,43 @@ public class ImageUtils {
} }
/** /**
* 获取图片的宽度和高度(考虑了图片旋转的情况。也就是说,获得的宽就是宽,高就是高)
*
* @param img
* 图片文件
* @return
*/
public static float[] getWidthHeightSize(String fileUrl) {
Image image = null;
String localFile = null;
float[] size = new float[2];
try {
if (StringTools.contains(fileUrl, AliyunConstant.OSS_CDN_URLS)) {
localFile = OssUtils.imageAutoOrient(fileUrl, 0);
image = Image.getInstance(localFile);
} else if (fileUrl.startsWith("http")) {
image = Image.getInstance(FileUtils.downloadByteFromUrl(fileUrl));
} else {
image = Image.getInstance(fileUrl);
}
} catch (Exception e) {
LOGGER.error("【IMAGE API】获取图片的宽度和高度[getWidthHeightSize]:" + e.getMessage(), e);
} finally {
if (!StringUtil.isEmpty(localFile)) {
FileUtils.deleteFile(localFile);
}
}
if (image == null) {
size[0] = 0;
size[1] = 0;
} else {
size[0] = image.getWidth();
size[1] = image.getHeight();
}
return size;
}
/**
* 获取对比比例 * 获取对比比例
* *
* @param num1 * @param num1
...@@ -655,27 +693,28 @@ public class ImageUtils { ...@@ -655,27 +693,28 @@ public class ImageUtils {
} }
/** /**
* 获取图片的宽度和高度 * 获取图片的宽度和高度(未考虑图片旋转的情况。也就是说,获得的宽可能是宽也可能是高,高可能是高也肯是宽)
* *
* @param img * @param fileUrl
* 图片文件
* @return * @return
*/ */
public static float[] getWidthHeightSize(String fileUrl) { public static float[] getWidthHeightNoOrient(String fileUrl) {
Image image = null; Image image = null;
String localFile = null; String localFile = null;
float[] size = new float[2]; float[] size = new float[2];
try { try {
if (StringTools.contains(fileUrl, AliyunConstant.OSS_CDN_URLS)) { /*
localFile = OssUtils.imageAutoOrient(fileUrl, 0); * if (StringTools.contains(fileUrl, AliyunConstant.OSS_CDN_URLS)) { localFile =
image = Image.getInstance(localFile); * OssUtils.imageAutoOrient(fileUrl); image = Image.getInstance(localFile); }
} else if (fileUrl.startsWith("http")) { * else
*/
if (fileUrl.startsWith("http")) {
image = Image.getInstance(FileUtils.downloadByteFromUrl(fileUrl)); image = Image.getInstance(FileUtils.downloadByteFromUrl(fileUrl));
} else { } else {
image = Image.getInstance(fileUrl); image = Image.getInstance(fileUrl);
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("【IMAGE API】获取图片的宽度和高度:" + e.getMessage(), e); LOGGER.error("【IMAGE API】获取图片的宽度和高度[getWidthHeightNoOrient]:" + e.getMessage(), e);
} finally { } finally {
if (!StringUtil.isEmpty(localFile)) { if (!StringUtil.isEmpty(localFile)) {
FileUtils.deleteFile(localFile); FileUtils.deleteFile(localFile);
...@@ -692,6 +731,82 @@ public class ImageUtils { ...@@ -692,6 +731,82 @@ public class ImageUtils {
} }
/** /**
* image transcode to webp
*
* 注意:使用此方法转图片格式,机器上必须安装有谷歌cwebp工具
*
* @param fileUrl
* @return
*/
// public static UploadResultInfo transcodeToWebp(String fileUrl, int quality) {
// LOGGER.info("【IMAGE API】image transcode to webp.<START>.[fileUrl]=" + fileUrl
// + ",[quality]=" + quality);
// String fileNameAll = FileUtils.getFileNameAll(fileUrl);
// String localFilePath = FilePathConst.DOWNLOAD_PATH + fileNameAll;
// FileUtils.downloadFileFromUrl(fileUrl, localFilePath);
// String outputFilePath = FilePathConst.IMAGE_PATH + "webp/" + fileNameAll +
// ".webp";
// FileUtils.creatFiles(outputFilePath);
// UploadResultInfo uploadResultInfo = null;
// try {
// String os = System.getProperty("os.name");
// if (os.toLowerCase().startsWith("win")) {
// executeCwebp4Win(localFilePath, outputFilePath, quality);
// } else {
// executeCwebp4Linux(localFilePath, outputFilePath, quality);
// }
// uploadResultInfo = OssUtils.uploadLocalFile4Child(outputFilePath, fileUrl);
// } catch (Exception e) {
// LOGGER.error("An error happend when convert to webp. Img is: " +
// e.getMessage(), e);
// throw new FileException(FileException.FILE_CONVERT_FAIL, "transcode to webp
// is fail!");
// } finally {
// FileUtils.deleteFile(localFilePath);
// FileUtils.deleteFile(outputFilePath);
// }
// LOGGER.info("【IMAGE API】image transcode to webp.<START>.[uploadResultInfo]="
// + uploadResultInfo);
// return uploadResultInfo;
// }
/**
* execute cwebp command:cwebp [options] input_file -o output_file.webp
*
* @param inputFilePath
* @param outputFilePath
* @param quality
* @throws Exception
*/
// private static void executeCwebp4Win(String inputFilePath, String
// outputFilePath, int quality) throws Exception {
// Process process = new ProcessBuilder("cwebp", "-q", (quality == 0 ? 80 :
// quality) + "", inputFilePath, "-o",
// outputFilePath).redirectErrorStream(true).start();
// if (0 != process.waitFor()) {
// throw new Exception("process wait for fail!");
// }
// }
/**
* execute cwebp command:cwebp [options] input_file -o output_file.webp
*
* @param inputFilePath
* @param outputFilePath
* @param quality
* @throws Exception
*/
// private static void executeCwebp4Linux(String inputFilePath, String
// outputFilePath, int quality) throws Exception {
// Process process = new ProcessBuilder("/usr/local/bin/cwebp", "-q", (quality
// == 0 ? 80 : quality) + "",
// inputFilePath, "-o", outputFilePath).redirectErrorStream(true).start();
// if (0 != process.waitFor()) {
// throw new Exception("process wait for fail!");
// }
// }
/**
* 上传的图片转换成webpO * 上传的图片转换成webpO
* *
* @param localFilePath * @param localFilePath
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment