]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_zz_imgsetsizes/init.php
add imgsetsizes plugin
[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 init($host) {
12                 $this->host = $host;
13
14                 if (function_exists("curl_init") && function_exists("getimagesize")) {
15                         $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
16                 }
17         }
18
19         function hook_article_filter($article) {
20
21                 $owner_uid = $article["owner_uid"];
22
23                 $charset_hack = '<head>
24                         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
25                 </head>';
26
27                 $doc = new DOMDocument();
28                 $doc->loadHTML($charset_hack . $article["content"]);
29
30                 $found = false;
31
32                 if ($doc) {
33                         $xpath = new DOMXpath($doc);
34
35                         $images = $xpath->query('(//img[@src])');
36
37                         foreach ($images as $img) {
38                                 $src = $img->getAttribute("src");
39
40                                 $ch = curl_init($src);
41                                 curl_setopt($ch, CURLOPT_HEADER, 0);
42                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
43                                 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
44                                 curl_setopt($ch, CURLOPT_RANGE, "0-32768");
45
46                                 @$result = curl_exec($ch);
47                                 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
48
49                                 if ($result && ($http_code == 200 || $http_code == 206)) {
50                                         $filename = tempnam(sys_get_temp_dir(), "ttsizecheck");
51
52                                         if ($filename) {
53                                                 $fh = fopen($filename, "w");
54                                                 if ($fh) {
55                                                         fwrite($fh, $result);
56                                                         fclose($fh);
57
58                                                         @$info = getimagesize($filename);
59
60                                                         if ($info && $info[0] > 0 && $info[1] > 0) {
61                                                                 $img->setAttribute("width", $info[0]);
62                                                                 $img->setAttribute("height", $info[1]);
63                                                                 $found = true;
64                                                         }
65
66                                                         unlink($filename);
67                                                 }
68                                         }
69                                 }
70                         }
71
72                         if ($found) {
73                                 $doc->removeChild($doc->firstChild); //remove doctype
74                                 $article["content"] = $doc->saveHTML();
75                         }
76                 }
77
78                 return $article;
79
80         }
81
82
83         function api_version() {
84                 return 2;
85         }
86
87 }
88 ?>