PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/wkp_ImageExt.php

https://github.com/pastak/jichikai_web
PHP | 138 lines | 126 code | 9 blank | 3 comment | 0 complexity | af0b2dcf19d9c2d2bf76249556c83cb7 MD5 | raw file
  1. <?php
  2. /**
  3. * Extended image functions plugin for LionWiki, (c) 2009, 2010 Adam Zivner, GPL'd
  4. */
  5. class ImageExt
  6. {
  7. var $desc = array(
  8. array("ImageExt plugin", "provides advanced image functions.")
  9. );
  10. var $dir;
  11. var $quality = 85;
  12. var $imgs = array();
  13. function ImageExt()
  14. {
  15. $this->dir = $GLOBALS["VAR_DIR"] . "images/";
  16. }
  17. function formatBegin()
  18. {
  19. global $CON;
  20. preg_match_all("#\[((https?://|\./)[^\]\|]+\.(jpeg|jpg|gif|png))(\|[^\]]+)?\]#", $CON, $this->imgs, PREG_SET_ORDER);
  21. foreach($this->imgs as $img)
  22. $CON = str_replace($img[0], "{IMAGE}", $CON);
  23. }
  24. function formatEnd()
  25. {
  26. global $CON;
  27. foreach($this->imgs as $img) {
  28. $link = $i_attr = $a_attr = $alt = $center = $tag = $style = "";
  29. $width = $height = 0;
  30. preg_match_all("/\|([^\]\|=]+)(=([^\]\|\"]+))?(?=[\]\|])/", $img[0], $options, PREG_SET_ORDER);
  31. foreach($options as $o)
  32. if($o[1] == "center") $center = true;
  33. else if($o[1] == "right" || $o[1] == "left") $style .= "float:$o[1];";
  34. else if($o[1] == "link") $link = (substr($o[3], 0, 4) == "http" || substr($o[3], 0, 2) == "./") ? $o[3] : "$self?page=" . u($o[3]);
  35. else if($o[1] == "alt") $alt = h($o[3]);
  36. else if($o[1] == "title") $a_attr .= ' title="'.h($o[3]).'"';
  37. else if($o[1] == "width") $width = $o[3];
  38. else if($o[1] == "height") $height = $o[3];
  39. else if($o[1] == "class") $i_attr .= " class=\"$o[3]\"";
  40. else if($o[1] == "id") $i_attr .= " id=\"$o[3]\"";
  41. else if($o[1] == "style") $style .= "$o[3];";
  42. else if($o[1] == "noborder") $style .= "border:0;outline:0;";
  43. else if($o[1] == "thumb") $link = $img[1];
  44. if($width || $height)
  45. $img[1] = $this->scaleImage($img[1], $width, $height);
  46. $tag = "<img src=\"$img[1]\" alt=\"$alt\" style=\"$style\"$i_attr/>";
  47. if($link) $tag = "<a href=\"$link\"$a_attr>$tag</a>";
  48. if($center) $tag = "<div style=\"text-align:center\">$tag</div>";
  49. $CON = preg_replace("/\{IMAGE\}/", $tag, $CON, 1);
  50. }
  51. }
  52. function rstrtrim($str, $remove)
  53. {
  54. $len = strlen($remove);
  55. $offset = strlen($str) - $len;
  56. while($offset > 0 && $offset == strpos($str, $remove, $offset))
  57. {
  58. $str = substr($str, 0, $offset);
  59. $offset = strlen($str) - $len;
  60. }
  61. return rtrim($str);
  62. }
  63. function pathToFilename($path, $x, $y)
  64. {
  65. $path = clear_path($path);
  66. if(substr($path, 0, 7) == "http://")
  67. $path = substr($path, 7);
  68. $ext = substr($path, -4);
  69. if(!strcasecmp($ext, ".png") || !strcasecmp($ext, ".gif") || !strcasecmp($ext, ".jpe"))
  70. $path = substr($path, 0, -4);
  71. if(!strcasecmp($ext, ".jpeg") || !strcasecmp($ext, ".jfif"))
  72. $path = substr($path, 0, -5);
  73. $path = str_replace("/", "_", $path);
  74. $path = str_replace(".", "_", $path);
  75. return $path . "-{$x}x{$y}.jpg";
  76. }
  77. function scaleImage($path, $nx, $ny)
  78. {
  79. if(!file_exists($this->dir))
  80. mkdir(rtrim($this->dir, "/"), 0777);
  81. if(substr($path, 0, 2) == "./")
  82. $path = "http://" . $_SERVER["HTTP_HOST"] . dirname($_SERVER["REQUEST_URI"]) . '/' . substr($path, 2);
  83. $filename = $this->pathToFilename($path, $nx, $ny);
  84. if(!file_exists($this->dir) . $filename) {
  85. if(!strcasecmp(substr($path, -4), ".png"))
  86. $img = imagecreatefrompng($path);
  87. else if(!strcasecmp(substr($path, -4), ".gif"))
  88. $img = imagecreatefromgif($path);
  89. else if(!strcasecmp(substr($path, -4), ".jpg") || !strcasecmp(substr($path, -5), ".jpeg") || !strcasecmp(substr($path, -4), ".jpe"))
  90. $img = imagecreatefromjpeg($path);
  91. $ox = imagesx($img);
  92. $oy = imagesy($img);
  93. if($nx && !$ny)
  94. $ny = (int) ($oy * $nx / $ox);
  95. else if(!$nx && $ny)
  96. $nx = (int) ($ox * $ny / $oy);
  97. $thumb = imagecreatetruecolor($nx, $ny);
  98. imagecopyresampled($thumb, $img, 0, 0, 0, 0, $nx, $ny, $ox, $oy);
  99. imagejpeg($thumb, $this->dir . $filename, $this->quality);
  100. }
  101. return "./" . $this->dir . $filename;
  102. }
  103. }