Tiny PNG is a tools of compressed PNG or JPEG image online , also that quality of compression is better
Official website of TinyPNG:https://tinypng.com/
Attach a screenshot of the official website:
The main reason for writing this script at that time was that many scripts of TinyPNG on the Internet were written in Python, which required Python environment and install tinify package. It was too troublesome. Then, I found that official website of TinyPNG had provide Java API , so I decided to write a jar to encapsulate these troubles to use a jar can be realize image compression.
Fisrt, apply an API_KEY
in TinyPNG. Note: Each API_KEY
only can be compress 500 images every month. Of course, you can apply more API_KEY
in multiple mailboxes. Then the size of each compressed image can't be more then 5 M.
Second, Pull the TinyImg prject and open it use IDEA,find the TinyHelper.java
.
Replace FC8dkDxbNVlJHL9JpmfCT0YzzRgVXZXT
to your API_KEY
,as follows:
static {
setKey("FC8dkDxbNVlJHL9JpmfCT0YzzRgVXZXT");
}
Third, select in menu bar : Build -> Build Artifacts -> TinyImg.jar -> Build, the jar can be use directly after generate, as follows:
Want to simple? You can try the way
- Dowload the tinyimg.jar file had build successfully. This file have a built-in
API_KEY
. I am not sure theAPI_KEY
whether effective, so you had better replace it to yourAPI_KEY
- Open it as zip file, but didn't unzip! find the file
com/ytempest/tinyimg/TinyHelper.class
inside, and unzip this file. - Open thd
class
file in noptepadd++ [Other edtior also can as long as its encoding format isANSI
].
SearchFC8dkDxbNVlJHL9JpmfCT0YzzRgVXZXT
in file and replace it to yourAPI_KEY
and save. - Replace the original
TinyHelper.class
in tinyimg.jar to a newTinyHelper.class
modify by us. Congratulation, you can use it now.
Guess: When the Java source file is compiled into a class
file, the string resource will not be modified, but the encoding format has changed. Note that the string length modified in this way must be consistent with the string length of the source file, otherwise the operation of the class
file will be damaged
Please ensure JDK Environment, setup it if didn''t had.
option | argument | explain | example |
---|---|---|---|
-h |
help document | java -jar -h |
|
compress the image under direcotry | java -jar tinyyimg.jar |
||
-k |
APK_KEY |
API_KEY apply from TinyPNG |
java -jar tinyimg.jar -k API_KYE |
-i |
input path | path of image or directory | java -jar tinyimg.jar -i inputPath |
-o |
ouput path | path of image or directory | java -jar tinyimg.jar -o outputPath |
-r |
compress all the image under diectory when input is directory | java -jar tinyimg.jar -r |
Example:java -jar tinyimg.jar inputImgPath outputImgPath
Explain:
-
inputImgPath
:The path of image need compress -
outputImgPath
:Output path of image compress finish. [Selectable, default override original file if not set ]
Example One :java -jar tinyimg.jar
Explain:
- Compress the image file under current directory and override original imagefile
- If you want to compress the image file under current directory and ouput to target directory. you can use :
java -jar tinyimg.jar . outputDirPath
Example Two:java -jar tinyimg.jar inputDirPath outputDirPath
Explain:
-
inputDirPath
:The directory that include the image file need to compress -
outputDirPath
:The path of output after compress finish. [Selectable, default override if not set]
Example:java -jar tingyimg.jar -s FC8dkDxbNVlJHL9JpmfCT0YzzRgVXZXT
示例:java -jar tinypng.jar -r
- v1.0 support compress single image file and direcotry include image
- v1.1 add the support of options,and improve the process
- support set
API_KEY
of TinyPNG - support compress all the image file under directory recursively
- support set