diff options
author | Andrew Dolgov <[email protected]> | 2020-09-14 20:59:01 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2020-09-14 20:59:01 +0300 |
commit | 47a8820e212eb0661c83b238f50ff4929aaed087 (patch) | |
tree | c5ea98ac0f8c393ffbc3e737a80884c35fbcd7b1 | |
parent | 92fba0b75f4b1292f99773b820ca0c5dd1e68009 (diff) | |
parent | 883dfa98031e96dd6c0460d807d0b9859917ae64 (diff) |
Merge branch 'master' of git.tt-rss.org:fox/ttrss-api-resize
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | init.php | 130 |
2 files changed, 119 insertions, 13 deletions
@@ -3,6 +3,8 @@ Downsamples images if an API client requests the backend to do so. Useful for slow or unreliable network connections, i.e. mobile internet. +May also be enabled through the web UI (Preferences -> Feeds). + This plugin handles an additional parameter passed to ``getHeadlines`` or ``getArticle`` API calls: @@ -11,7 +11,7 @@ class Api_Resize_Media extends Plugin { function about() { return array(1.0, - "Resizes images as requested by API clients", + "Resizes media on the fly (for API clients or web UI)", "fox"); } @@ -25,6 +25,11 @@ class Api_Resize_Media extends Plugin { $host->add_hook($host::HOOK_RENDER_ARTICLE_API, $this, 999); $host->add_hook($host::HOOK_ARTICLE_IMAGE, $this, 999); + + $host->add_hook($host::HOOK_PREFS_TAB, $this); + $host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this, 999); + $host->add_hook($host::HOOK_RENDER_ARTICLE, $this, 999); + $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this, 999); } private function make_thumbnail($input_filename, $output_filename, $dim_max_x = 600, $dim_max_y = 600, @@ -100,6 +105,7 @@ class Api_Resize_Media extends Plugin { } } + /** @noinspection PhpUnused */ public function api_resize() { $url = validate_url($_REQUEST["url"]); @@ -227,15 +233,8 @@ class Api_Resize_Media extends Plugin { return $url; } - function hook_article_image($enclosures, $content, $site_url) { - $article = $this->hook_render_article_api(["headline" => ["content" => $content]]); - - return ["", "", $article["content"]]; - } - - function hook_render_article_api($row) { + private function process_article($row, $width) { $need_saving = false; - $width = (int) clean($_REQUEST["resize_width"]); $article = isset($row['headline']) ? $row['headline'] : $row['article']; @@ -248,13 +247,26 @@ class Api_Resize_Media extends Plugin { $imgs = $xpath->query("//img[@src]"); foreach ($imgs as $img) { - $new_src = $this->rewrite_url_if_needed($img->getAttribute("src"), $width); + $orig_src = $img->getAttribute("src"); + $new_src = $this->rewrite_url_if_needed($orig_src, $width); + + if ($new_src != $orig_src) { + $need_saving = true; + + $parent = $img->parentNode; + + if ($parent && $parent->tagName != "a") { + $img_link = $doc->createElement("a"); + $img_link->setAttribute("href", $orig_src); + $img_link->setAttribute("target", "_blank"); + + $parent->replaceChild($img_link, $img); + + $img_link->appendChild($img); + } - if ($new_src != $img->getAttribute("src")) { $img->setAttribute("src", $new_src); $img->removeAttribute("srcset"); - - $need_saving = true; } } @@ -299,6 +311,45 @@ class Api_Resize_Media extends Plugin { return $article; } + /** @noinspection PhpUnused */ + function hook_article_image($enclosures, $content, $site_url) { + $width = (int) clean($_REQUEST["resize_width"]); + + $article = $this->process_article(["headline" => ["content" => $content]], $width); + + return ["", "", $article["content"]]; + } + + /** @noinspection PhpUnused */ + function hook_enclosure_entry($enc) { + $force_width = (int) $this->host->get($this, "force_width", 0); + + $enc["content_url"] = $this->rewrite_url_if_needed($enc["content_url"], $force_width); + + return $enc; + } + + /** @noinspection PhpUnused */ + function hook_render_article_cdm($row) { + $force_width = (int) $this->host->get($this, "force_width", 0); + + return $this->process_article(["article" => $row], $force_width); + } + + /** @noinspection PhpUnused */ + function hook_render_article($row) { + $force_width = (int) $this->host->get($this, "force_width", 0); + + return $this->process_article(["article" => $row], $force_width); + } + + /** @noinspection PhpUnused */ + function hook_render_article_api($row) { + $width = (int) clean($_REQUEST["resize_width"]); + + return $this->process_article($row, $width); + } + // https://stackoverflow.com/questions/280658/can-i-detect-animated-gifs-using-php-and-gd function is_animated_gif ($filename) { $raw = file_get_contents($filename); @@ -330,6 +381,59 @@ class Api_Resize_Media extends Plugin { return false; } + function hook_prefs_tab($args) { + if ($args != "prefFeeds") return; + + $force_width = (int) $this->host->get($this, "force_width", 0); + + print "<div dojoType='dijit.layout.AccordionPane' + title=\"<i class='material-icons'>photo</i> ".$this->__( 'Resize media on the fly (api_resize_media)')."\">"; + + print "<form dojoType='dijit.form.Form'>"; + + print "<script type='dojo/method' event='onSubmit' args='evt'> + evt.preventDefault(); + if (this.validate()) { + console.log(dojo.objectToQuery(this.getValues())); + new Ajax.Request('backend.php', { + parameters: dojo.objectToQuery(this.getValues()), + onComplete: function(transport) { + Notify.info(transport.responseText); + } + }); + //this.reset(); + } + </script>"; + + print_hidden("op", "pluginhandler"); + print_hidden("method", "save"); + print_hidden("plugin", "api_resize_media"); + + print "<h2>" . $this->__( "Global settings") . "</h2>"; + + print "<fieldset>"; + + print "<label>".$this->__( "Also resize in web UI (width, 0 - disables)")."</label>"; + print "<input dojoType='dijit.form.NumberSpinner' + required='1' name='force_width' id='api_resize_force_width' value='$force_width'> " . $this->__('pixels'); + + print "</fieldset>"; + + print_button("submit", $this->__( "Save"), "class='alt-primary'"); + + print "</form>"; + + print "</div>"; + } + + function save() { + $force_width = (int) $_POST["force_width"]; + + $this->host->set($this, "force_width", $force_width); + + echo $this->T_sprintf("Data saved (%d)", $force_width); + } + function api_version() { return 2; } |