summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2019-08-12 18:58:23 +0300
committerAndrew Dolgov <[email protected]>2019-08-12 18:58:23 +0300
commitb9e0c4ade026c1b1a791090698c6f7abbd8ca62c (patch)
tree1475ed009eb891baab5412fcff0a9c0c452a100f
parent81ce2ff5268812feebc51a6b8fa2c56998f783de (diff)
save output as webp
update README
-rw-r--r--README.md25
-rw-r--r--init.php34
2 files changed, 48 insertions, 11 deletions
diff --git a/README.md b/README.md
index 33d58b2..8359a75 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,24 @@
-git clone to ``plugins.local/af_zz_api_resize``.
+## Resizes images as requested by API clients
+
+Downsamples images if an API client requests the backend to do so. Useful for
+slow or unreliable network connections, i.e. mobile internet.
+
+This plugin handles two additional parameters passed to ``getHeadlines`` or ``getArticle``
+API call:
+
+* ``resize_enabled`` (bool) - enable or disable resizing
+* ``resize_width`` (int) - target width (or height, plugin resizes based on a larger dimension)
+
+Images are saved in a WEBP format.
+
+## Installation
+
+Plugin assumes your tt-rss image cache (``cache/images``) is writable
+and GD library is available to PHP.
+
+1. git clone to ``plugins.local/af_zz_api_resize``.
+2. Enable "downsample images" option in the API client (for example, tt-rss-android)
+
+## License
+
+GPLv3
diff --git a/init.php b/init.php
index af7b77c..bfe310e 100644
--- a/init.php
+++ b/init.php
@@ -89,10 +89,12 @@ class Af_Zz_Api_Resize extends Plugin {
}
}
- if ($need_stamp || !$need_alpha)
+ /*if ($need_stamp || !$need_alpha)
@imageJpeg($t_im, $output_filename, 75);
else
- @imagePng($t_im, $output_filename, 5);
+ @imagePng($t_im, $output_filename, 5);*/
+
+ imagewebp($t_im, $output_filename, 80);
imageDestroy($o_im);
imageDestroy($t_im);
@@ -204,6 +206,10 @@ class Af_Zz_Api_Resize extends Plugin {
function hook_render_article_api($params) {
$need_saving = false;
$width = (int) clean($_REQUEST["resize_width"]);
+ $enabled = sql_bool_to_bool(clean($_REQUEST["resize_enabled"]));
+
+ if (!$enabled || !$width)
+ return $params["headline"];
$doc = new DOMDocument();
if (@$doc->loadHTML('<?xml encoding="UTF-8">' . $params["headline"]["content"])) {
@@ -221,19 +227,27 @@ class Af_Zz_Api_Resize extends Plugin {
}
}
- $vids = $xpath->query("(//picture)");
+ $vids = $xpath->query("//video[@poster]");
foreach ($vids as $vid) {
- $vsrcs = $xpath->query("source", $vid);
+ $new_poster = $this->rewrite_url_if_needed($vid->getAttribute("poster"), $width);
+
+ if ($new_poster != $vid->getAttribute("src")) {
+ $vid->setAttribute("poster", $new_poster);
+
+ $need_saving = true;
+ }
+ }
- foreach ($vsrcs as $vsrc) {
- $new_src = $this->rewrite_url_if_needed($vsrc->getAttribute("src"), $width);
+ $psrcs = $xpath->query("//picture/source");
- if ($new_src != $vsrc->getAttribute("src")) {
- $vid->setAttribute("src", $new_src);
+ foreach ($psrcs as $src) {
+ $new_src = $this->rewrite_url_if_needed($src->getAttribute("src"), $width);
- $need_saving = true;
- }
+ if ($new_src != $src->getAttribute("src")) {
+ $src->setAttribute("src", $new_src);
+
+ $need_saving = true;
}
}
}