Commit 9c92af97 by songxiang

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

parent 0a626b44
......@@ -11,10 +11,11 @@ import java.math.BigDecimal;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dcg.util.StringUtils;
import com.itextpdf.text.Image;
import com.pcloud.common.constant.AliyunConstant;
import com.pcloud.common.constant.FilePathConst;
......@@ -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
......@@ -653,29 +691,30 @@ public class ImageUtils {
LOGGER.error("【图片】输出文件流关闭失败:" + e.getMessage(), e);
}
}
/**
* 获取图片的宽度和高度
* 获取图片的宽度和高度(未考虑图片旋转的情况。也就是说,获得的宽可能是宽也可能是高,高可能是高也肯是宽)
*
* @param img
* 图片文件
* @param fileUrl
* @return
*/
public static float[] getWidthHeightSize(String fileUrl) {
public static float[] getWidthHeightNoOrient(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")) {
/*
* if (StringTools.contains(fileUrl, AliyunConstant.OSS_CDN_URLS)) { localFile =
* OssUtils.imageAutoOrient(fileUrl); 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】获取图片的宽度和高度:" + e.getMessage(), e);
LOGGER.error("【IMAGE API】获取图片的宽度和高度[getWidthHeightNoOrient]:" + e.getMessage(), e);
} finally {
if (!StringUtil.isEmpty(localFile)) {
FileUtils.deleteFile(localFile);
......@@ -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
*
* @param localFilePath
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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