This is a Server based on Java, which allows you to serve WebP images on the fly.
And you can easily integrate into your project.
e.g When you visit
https://a.com/1.jpg
,it will serve asimage/webp
without changing the URL.For Safari and Opera users, the original image will be used.
Modified from n0vad3v/webp_server
-
Add a simple feature that you can config more image directory
-
And you can easily integrate into your Java project.
You need newer version than 0.3
add to build.gralde
implementation 'moe.keshane:webp-server-java:{version}'
add to pom.xml
<dependency>
<groupId>moe.keshane</groupId>
<artifactId>webp-server-java</artifactId>
<version>{version}</version>
</dependency>
To initial a server you need to create a WebpServerConfig object.
Two params of WebpServerConfig are Map<String,String> imgMap,List<String> allowedTypes
.
imaMap
is a map of request uri and image file path like
Map<String,String> imgMap = new HashMap<>();
map.put("/i","/home/ubuntu/pic");
map.put("/img","/home/ubuntu/pic2");
map.put("/","/home/ubuntu/pic1");
allowedTypes
is a list of allowed image extension name like
List<String> allowedTypes = Arrays.asList("jpg","png","jpeg","webp");
and initial the server
WebpServerConfig webpConfig = new WebpServerConfig(imgMap,allowedTypes);
WebpServer server = WebpServer.init(webpConfig);
Server would return a webp image file if is not safari or origin image file if is safari.
The param is HttpServletRequest
object.
File file = server.request(request);
Create a 'config.json' file
If you are serving images at https://example.com/pics/tsuki.jpg
and
your files are at /var/www/image/pics/tsuki.jpg
, then imgMap
shall be {"/pics":"/var/www/image/pics/tsuki.jpg"}
.
Download the webp-server
from release page.
Create a config.json
{
"host": "127.0.0.1",
"port": 3333,
"imgMap": {
"/i": "/home/ubuntu/pic",
"/img": "/home/ubuntu/pic2",
"/": "/home/pic1"
},
"allowedTypes": ["jpg","png","jpeg","webp"]
}
You must provide a config file to run this server
Run this Jar package like
java -jar webp-server-java.jar /path/to/your/config.json
Use screen
or tmux
to avoid being terminated. Let's take screen
for example
screen -S webp
java -jar webp-server-java.jar /path/to/your/config.json
(Use Ctrl-A-D to detach the screen
with webp-server
running.)
Let Nginx to proxy_pass http://localhost:3333/;
, and your webp-server is on-the-fly
location ^~ /wp-content/uploads/ {
proxy_pass http://127.0.0.1:3333;
}
Install jdk8 and clone the repo
then
./gradlew build
./gradlew bootJar