init.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class Af_Zz_ImgSetSizes extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Set width/height attributes for images in articles (requires CURL and GD)",
  7. "fox");
  8. }
  9. function flags() {
  10. return array("needs_curl" => true);
  11. }
  12. function init($host) {
  13. $this->host = $host;
  14. if (function_exists("curl_init") && function_exists("getimagesize")) {
  15. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  16. }
  17. }
  18. function hook_article_filter($article) {
  19. if (defined('NO_CURL') || !function_exists("curl_init"))
  20. return $article;
  21. $charset_hack = '<head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  23. </head>';
  24. $doc = new DOMDocument();
  25. $doc->loadHTML($charset_hack . $article["content"]);
  26. $found = false;
  27. if ($doc) {
  28. $xpath = new DOMXpath($doc);
  29. $images = $xpath->query('(//img[@src])');
  30. foreach ($images as $img) {
  31. $src = $img->getAttribute("src");
  32. $ch = curl_init($src);
  33. curl_setopt($ch, CURLOPT_HEADER, 0);
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  36. curl_setopt($ch, CURLOPT_RANGE, "0-32768");
  37. @$result = curl_exec($ch);
  38. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  39. if ($result && ($http_code == 200 || $http_code == 206)) {
  40. $filename = tempnam(sys_get_temp_dir(), "ttsizecheck");
  41. if ($filename) {
  42. $fh = fopen($filename, "w");
  43. if ($fh) {
  44. fwrite($fh, $result);
  45. fclose($fh);
  46. @$info = getimagesize($filename);
  47. if ($info && $info[0] > 0 && $info[1] > 0) {
  48. $img->setAttribute("width", $info[0]);
  49. $img->setAttribute("height", $info[1]);
  50. $found = true;
  51. }
  52. unlink($filename);
  53. }
  54. }
  55. }
  56. }
  57. if ($found) {
  58. $doc->removeChild($doc->firstChild); //remove doctype
  59. $article["content"] = $doc->saveHTML();
  60. }
  61. }
  62. return $article;
  63. }
  64. function api_version() {
  65. return 2;
  66. }
  67. }
  68. ?>