]> git.wh0rd.org - tt-rss.git/blob - plugins/af_zz_imgsetsizes/init.php
cdfd06338d95b6689bb0aefb030325d3a7dace14
[tt-rss.git] / plugins / af_zz_imgsetsizes / init.php
1 <?php
2 class Af_Zz_ImgSetSizes extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Set width/height attributes for images in articles (requires CURL and GD)",
8 "fox");
9 }
10
11 function flags() {
12 return array("needs_curl" => true);
13 }
14
15 function init($host) {
16 $this->host = $host;
17
18 if (function_exists("curl_init") && function_exists("getimagesize")) {
19 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
20 }
21 }
22
23 function hook_article_filter($article) {
24
25 if (defined('NO_CURL') || !function_exists("curl_init"))
26 return $article;
27
28 $charset_hack = '<head>
29 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
30 </head>';
31
32 $doc = new DOMDocument();
33 $doc->loadHTML($charset_hack . $article["content"]);
34
35 $found = false;
36
37 if ($doc) {
38 $xpath = new DOMXpath($doc);
39
40 $images = $xpath->query('(//img[@src])');
41
42 foreach ($images as $img) {
43 $src = $img->getAttribute("src");
44
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");
50
51 @$result = curl_exec($ch);
52 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
53
54 if ($result && ($http_code == 200 || $http_code == 206)) {
55 $filename = tempnam(sys_get_temp_dir(), "ttsizecheck");
56
57 if ($filename) {
58 $fh = fopen($filename, "w");
59 if ($fh) {
60 fwrite($fh, $result);
61 fclose($fh);
62
63 @$info = getimagesize($filename);
64
65 if ($info && $info[0] > 0 && $info[1] > 0) {
66 $img->setAttribute("width", $info[0]);
67 $img->setAttribute("height", $info[1]);
68 $found = true;
69 }
70
71 unlink($filename);
72 }
73 }
74 }
75 }
76
77 if ($found) {
78 $doc->removeChild($doc->firstChild); //remove doctype
79 $article["content"] = $doc->saveHTML();
80 }
81 }
82
83 return $article;
84
85 }
86
87
88 function api_version() {
89 return 2;
90 }
91
92 }
93 ?>