init.php 1.8 KB

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