PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/pngRender.class.php

https://github.com/swat/pragyan
PHP | 73 lines | 62 code | 4 blank | 7 comment | 5 complexity | b6c65aac7b0efb41294e04ab34784b27 MD5 | raw file
  1. <?php
  2. if(!defined('__PRAGYAN_CMS'))
  3. {
  4. header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
  5. echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
  6. echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
  7. exit(1);
  8. }
  9. /**
  10. * @package pragyan
  11. * @copyright (c) 2010 Pragyan Team
  12. * @license http://www.gnu.org/licenses/ GNU Public License
  13. * @author akash6190
  14. * For more details, see README
  15. */
  16. class pngrender {
  17. function render_png($string,$hash){
  18. $font = 4;
  19. $width = ImageFontWidth($font) * strlen($string);
  20. $height = ImageFontHeight($font);
  21. $im = @imagecreate ($width,$height);
  22. $background_color = imagecolorallocate ($im, 255, 255, 255); //white background
  23. $text_color = imagecolorallocate ($im, 0, 0,0);//black text
  24. imagecolortransparent($im,$background_color);
  25. imagestring ($im, $font, 0, 0, $string, $text_color);
  26. $pngdata=imagepng ($im,$this->CACHE_DIR."/$hash.png");
  27. chdir($current_dir);
  28. }
  29. function cleanup($hash) {
  30. $current_dir = getcwd();
  31. chdir($this->TMP_DIR);
  32. unlink($this->TMP_DIR . "/$hash.png");
  33. chdir($current_dir);
  34. }
  35. function transform($text) {
  36. global $sourceFolder;
  37. global $uploadFolder;
  38. global $urlRequestRoot, $cmsFolder;
  39. $uploadDir = $sourceFolder . "/" . $uploadFolder;
  40. if (!file_exists($uploadDir . "/temp"))
  41. mkdir($uploadDir . "/temp", 0755);
  42. if (!file_exists($uploadDir . "/cache"))
  43. mkdir($uploadDir . "/cache", 0755);
  44. $this->TMP_DIR = $uploadDir . "/temp";
  45. $this->CACHE_DIR = $uploadDir . "/cache";
  46. $this->URL_PATH = $urlRequestRoot . "/" . $cmsFolder ."/" .$uploadFolder . "/cache";
  47. preg_match_all("/\[tex\](.*?)\[\/tex\]/si", $text, $matches1);
  48. preg_match_all("/\[img\](.*?)\[\/img\]/si", $text, $matches2);
  49. $matches[0]=array_merge($matches2[0],$matches1[0]);
  50. $matches[1]=array_merge($matches2[1],$matches1[1]);
  51. for ($i = 0; $i < count($matches[0]); $i++) {
  52. $position = strpos($text, $matches[0][$i]);
  53. $thunk = $matches[1][$i];
  54. $hash = md5($thunk);
  55. $full_name = $this->CACHE_DIR . "/" .
  56. $hash . ".png";
  57. $url = $this->URL_PATH . "/" .
  58. $hash . ".png";
  59. if (!is_file($full_name)) {
  60. $this->render_png($thunk, $hash);
  61. $this->cleanup($hash);
  62. } else
  63. touch($full_name);
  64. $text = substr_replace($text, "<img src=\"$url\" alt=\"Formula: $i\" />", $position, strlen($matches[0][$i]));
  65. }
  66. exec("find " . $uploadDir . "/cache -type f -mtime +14 | xargs rm -f");
  67. return $text;
  68. }
  69. }