Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐少华
midjourney-proxy
Commits
9c92af97
Commit
9c92af97
authored
May 10, 2019
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
OSS增加图片裁剪方法同时持久化
parent
0a626b44
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
11 deletions
+126
-11
FileUtils.java
...mmon/src/main/java/com/pcloud/common/utils/FileUtils.java
+0
-0
ImageUtils.java
...mon/src/main/java/com/pcloud/common/utils/ImageUtils.java
+126
-11
OssUtils.java
...rc/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
+0
-0
No files found.
pcloud-common/src/main/java/com/pcloud/common/utils/FileUtils.java
View file @
9c92af97
This diff is collapsed.
Click to expand it.
pcloud-common/src/main/java/com/pcloud/common/utils/ImageUtils.java
View file @
9c92af97
...
...
@@ -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
[]
getWidthHeight
Size
(
String
fileUrl
)
{
public
static
float
[]
getWidthHeight
NoOrient
(
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
...
...
pcloud-common/src/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
View file @
9c92af97
This source diff could not be displayed because it is too large. You can
view the blob
instead.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment