diff --git a/docs/changelog.md b/docs/changelog.md index ebf4c868..b68d5205 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,6 +1,10 @@ Changelog ========= +#### 4.0.38 + +* Added the system property "com.sksamuel.scrimage.webp.binary.dir" to configure the directory of the webp binaries. + #### 4.0.37 * Better error message when image loading fails, to indicate the file type. diff --git a/docs/webp.md b/docs/webp.md index ddd637f3..b8d4fb19 100644 --- a/docs/webp.md +++ b/docs/webp.md @@ -7,8 +7,14 @@ Scrimage provides support for webp through the `scrimage-webp` module. To use we This module uses the `dwebp`, `cwebp` and `gif2webp` binaries, created by Google. The `scrimage-webp` module comes with the linux_x64, window_x64, mac-10.15 binaries already included (see required [copyright notice](https://github.com/sksamuel/scrimage/blob/master/scrimage-webp/src/main/resources/dist_webp_binaries/LICENSE)). -If you don't wish to use the embedded binaries, then you can [download other versions](https://developers.google.com/speed/webp) and place them -on your classpath at `/webp_binaries/{osName}/dwebp` or `/webp_binaries/{osName}/cwebp` or `/webp_binaries/{osName}/gif2webp`. +If you don't wish to use the embedded binaries, then you can [download other versions](https://developers.google.com/speed/webp) +and use the system property "com.sksamuel.scrimage.webp.binary.dir" to configure the directory you placed the binaries: +```shell +java -Dcom.sksamuel.scrimage.webp.binary.dir="/tmp/webp" -jar myprogram.jar +``` +Or you can place them on your classpath at `/webp_binaries/{osName}/dwebp` or `/webp_binaries/{osName}/cwebp` or +`/webp_binaries/{osName}/gif2webp`. +The directory set by the system property is always searched first. `{osName}` must be one of `window`, `linux`, `mac`. ie `/webp_binaries/window/cwebp`. diff --git a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java index 0871312f..cebf7b39 100644 --- a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java +++ b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/CWebpHandler.java @@ -14,9 +14,15 @@ public class CWebpHandler extends WebpHandler { static { try { - // write out binary to a location we can execute it from - binary = createPlaceholder("cwebp"); - installCWebp(); + // try to get the binary path from the system property + Path pathFromProperty = getPathFromProperty("cwebp"); + if (pathFromProperty != null) { + binary = pathFromProperty; + } else { + // write out binary to a location we can execute it from + binary = createPlaceholder("cwebp"); + installCWebp(); + } } catch (IOException e) { throw new RuntimeException(e); } diff --git a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/DWebpHandler.java b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/DWebpHandler.java index 770c8b67..34c5ad7b 100644 --- a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/DWebpHandler.java +++ b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/DWebpHandler.java @@ -13,9 +13,15 @@ public class DWebpHandler extends WebpHandler { static { try { - // write out binary to a location we can execute it from - binary = createPlaceholder("dwebp"); - installDWebp(); + // try to get the binary path from the system property + Path pathFromProperty = getPathFromProperty("dwebp"); + if (pathFromProperty != null) { + binary = pathFromProperty; + } else { + // write out binary to a location we can execute it from + binary = createPlaceholder("dwebp"); + installDWebp(); + } } catch (IOException e) { throw new RuntimeException(e); } diff --git a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/Gif2WebpHandler.java b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/Gif2WebpHandler.java index 420f12d5..64556696 100644 --- a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/Gif2WebpHandler.java +++ b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/Gif2WebpHandler.java @@ -14,9 +14,15 @@ public class Gif2WebpHandler extends WebpHandler { static { try { - // write out binary to a location we can execute it from - binary = createPlaceholder("gif2webp"); - installGif2Webp(); + // try to get the binary path from the system property + Path pathFromProperty = getPathFromProperty("gif2webp"); + if (pathFromProperty != null) { + binary = pathFromProperty; + } else { + // write out binary to a location we can execute it from + binary = createPlaceholder("gif2webp"); + installGif2Webp(); + } } catch (IOException e) { throw new RuntimeException(e); } diff --git a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpHandler.java b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpHandler.java index 473ca0c8..0ffc929c 100644 --- a/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpHandler.java +++ b/scrimage-webp/src/main/java/com/sksamuel/scrimage/webp/WebpHandler.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.concurrent.TimeUnit; @@ -16,6 +17,21 @@ abstract class WebpHandler { private static final Logger logger = LoggerFactory.getLogger(WebpHandler.class); + protected static Path getPathFromProperty(String name) { + try { + String binaryDir = System.getProperty("com.sksamuel.scrimage.webp.binary.dir"); + if (binaryDir != null && !binaryDir.isEmpty()) { + Path path = Paths.get(binaryDir, name); + if (Files.isExecutable(path)) { + return path; + } + } + return null; + } catch (Exception ignored) { + return null; + } + } + protected static Path createPlaceholder(String name) throws IOException { return Files.createTempFile(name, "binary"); }