diff options
author | Art4 <[email protected]> | 2015-02-19 23:17:43 +0100 |
---|---|---|
committer | Art4 <[email protected]> | 2015-02-19 23:17:43 +0100 |
commit | d9c042c4c536f1afbf206aeea09b0b127b7b6418 (patch) | |
tree | 9d7ef18c6d9d4c8f8f4d1c9a5dbfceeb9fd5acf4 /plugins/af_zz_imgsetsizes | |
parent | cf42b79120290ee5866136a0c4656e6999f06045 (diff) | |
parent | 4ca621a36016de1fbb5447e1c1de0b607ba94a7c (diff) |
Merge branch 'master' into patch-1
Conflicts:
locale/de_DE/LC_MESSAGES/messages.po
Diffstat (limited to 'plugins/af_zz_imgsetsizes')
-rw-r--r-- | plugins/af_zz_imgsetsizes/init.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/plugins/af_zz_imgsetsizes/init.php b/plugins/af_zz_imgsetsizes/init.php new file mode 100644 index 000000000..d71ec096e --- /dev/null +++ b/plugins/af_zz_imgsetsizes/init.php @@ -0,0 +1,88 @@ +<?php +class Af_Zz_ImgSetSizes extends Plugin { + private $host; + + function about() { + return array(1.0, + "Set width/height attributes for images in articles (requires CURL and GD)", + "fox"); + } + + function init($host) { + $this->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 = '<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> + </head>'; + + $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; + } + +} +?> |