From: Andrew Dolgov Date: Sun, 9 Nov 2014 17:31:46 +0000 (+0300) Subject: add imgsetsizes plugin X-Git-Tag: 1.15~14 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=346297d4d216e58566e6f35b7b6fbd2235f46258;p=tt-rss.git add imgsetsizes plugin --- diff --git a/plugins/af_zz_imgsetsizes/init.php b/plugins/af_zz_imgsetsizes/init.php new file mode 100644 index 00000000..d71ec096 --- /dev/null +++ b/plugins/af_zz_imgsetsizes/init.php @@ -0,0 +1,88 @@ +host = $host; + + if (function_exists("curl_init") && function_exists("getimagesize")) { + $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); + } + } + + function hook_article_filter($article) { + + $owner_uid = $article["owner_uid"]; + + $charset_hack = ' + + '; + + $doc = new DOMDocument(); + $doc->loadHTML($charset_hack . $article["content"]); + + $found = false; + + if ($doc) { + $xpath = new DOMXpath($doc); + + $images = $xpath->query('(//img[@src])'); + + foreach ($images as $img) { + $src = $img->getAttribute("src"); + + $ch = curl_init($src); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); + curl_setopt($ch, CURLOPT_RANGE, "0-32768"); + + @$result = curl_exec($ch); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + if ($result && ($http_code == 200 || $http_code == 206)) { + $filename = tempnam(sys_get_temp_dir(), "ttsizecheck"); + + if ($filename) { + $fh = fopen($filename, "w"); + if ($fh) { + fwrite($fh, $result); + fclose($fh); + + @$info = getimagesize($filename); + + if ($info && $info[0] > 0 && $info[1] > 0) { + $img->setAttribute("width", $info[0]); + $img->setAttribute("height", $info[1]); + $found = true; + } + + unlink($filename); + } + } + } + } + + if ($found) { + $doc->removeChild($doc->firstChild); //remove doctype + $article["content"] = $doc->saveHTML(); + } + } + + return $article; + + } + + + function api_version() { + return 2; + } + +} +?>