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