]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgsetsizes/init.php
cdfd06338d95b6689bb0aefb030325d3a7dace14
2 class Af_Zz_ImgSetSizes
extends Plugin
{
7 "Set width/height attributes for images in articles (requires CURL and GD)",
12 return array("needs_curl" => true);
15 function init($host) {
18 if (function_exists("curl_init") && function_exists("getimagesize")) {
19 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
23 function hook_article_filter($article) {
25 if (defined('NO_CURL') ||
!function_exists("curl_init"))
28 $charset_hack = '<head>
29 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
32 $doc = new DOMDocument();
33 $doc->loadHTML($charset_hack . $article["content"]);
38 $xpath = new DOMXpath($doc);
40 $images = $xpath->query('(//img[@src])');
42 foreach ($images as $img) {
43 $src = $img->getAttribute("src");
45 $ch = curl_init($src);
46 curl_setopt($ch, CURLOPT_HEADER
, 0);
47 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
48 curl_setopt($ch, CURLOPT_BINARYTRANSFER
,1);
49 curl_setopt($ch, CURLOPT_RANGE
, "0-32768");
51 @$result = curl_exec($ch);
52 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE
);
54 if ($result && ($http_code == 200 ||
$http_code == 206)) {
55 $filename = tempnam(sys_get_temp_dir(), "ttsizecheck");
58 $fh = fopen($filename, "w");
63 @$info = getimagesize($filename);
65 if ($info && $info[0] > 0 && $info[1] > 0) {
66 $img->setAttribute("width", $info[0]);
67 $img->setAttribute("height", $info[1]);
78 $doc->removeChild($doc->firstChild
); //remove doctype
79 $article["content"] = $doc->saveHTML();
88 function api_version() {