PageRenderTime 72ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/classes/class_gd.php

http://lansuite.googlecode.com/
PHP | 279 lines | 188 code | 45 blank | 46 comment | 52 complexity | 87dbffd27b34183c2dca81ac6d483929 MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. class gd {
  3. var $img;
  4. var $height;
  5. var $width;
  6. var $font;
  7. var $font_size;
  8. var $free_type = 0;
  9. var $available = 0;
  10. // Constructor
  11. function gd () {
  12. if (function_exists("gd_info")) {
  13. $GD = gd_info();
  14. $this->available = 1;
  15. if ($GD["Freetype Support"]) $this->free_type = 1;
  16. if ($GD["FreeType Support"]) $this->free_type = 1;
  17. }
  18. }
  19. function NewImage($width, $height, $interlace = NULL) {
  20. $this->width = $width;
  21. $this->height = $height;
  22. $this->SetFont();
  23. if (function_exists("imagecreatetruecolor")) $this->img = imagecreatetruecolor($this->width, $this->height);
  24. else $this->img = imagecreate($this->width, $this->height);
  25. if (!$this->img) $this->img = imagecreate($this->width, $this->height);
  26. if (!$this->img) return t('Unable to Initialize new GD image stream');
  27. ImageInterlace($this->img, $interlace);
  28. }
  29. // Output image (as file if $file is specified, or as direct output if not)
  30. function PutImage($file = NULL, $type = NULL, $destroy = TRUE) {
  31. global $config;
  32. if ($file) {
  33. $path = substr($file, 0, strrpos($file, "/"));
  34. $filename = substr($file, strrpos($file, "/") + 1, strlen($file));
  35. $type = substr($filename, strrpos($filename, ".") + 1, 4);
  36. }
  37. if ($type == "") $type = "png";
  38. if ($file) {
  39. if (!is_writable($path)) return t('Unable to write in directory /\'/%1/\'/', $path);
  40. } else Header("Content-type: image/$type");
  41. if ($file) {
  42. switch(strtolower($type)) {
  43. case "jpeg":
  44. case "jpg": if (ImageTypes() & IMG_JPG) ImageJPEG($this->img, $file); break;
  45. case "gif": if (ImageTypes() & IMG_GIF) ImageGIF($this->img, $file); break;
  46. case "wbmp": if (ImageTypes() & IMG_WBMP) ImageWBMP($this->img, $file); break;
  47. case "bmp":
  48. include_once("ext_scripts/gd/bmp.php");
  49. ImageBMP($this->img, $file);
  50. break;
  51. case "ico":
  52. include_once("ext_scripts/gd/ico.php");
  53. ImageICO($this->img, $file);
  54. break;
  55. case "cur":
  56. include_once("ext_scripts/gd/cur.php");
  57. ImageCUR($this->img, $file);
  58. break;
  59. /* case "ani":
  60. include_once("ext_scripts/gd/ani.php");
  61. ImageANI($this->img, $file);
  62. break;*/
  63. default: if (ImageTypes() & IMG_PNG) ImagePNG($this->img, $file); break;
  64. }
  65. chmod ($file, octdec($config["lansuite"]["chmod_file"]));
  66. // Check filesize. Delete if filesize = 0 (i.e. becaus of exceeded disk quota), so it is tried to be generated on next load
  67. if (filesize($file) == 0) unlink($file);
  68. } else {
  69. switch(strtolower($type)) {
  70. case "jpeg":
  71. case "jpg": if (ImageTypes() & IMG_JPG) ImageJPEG($this->img); break;
  72. case "gif": if (ImageTypes() & IMG_GIF) ImageGIF($this->img); break;
  73. case "wbmp": if (ImageTypes() & IMG_WBMP) ImageWBMP($this->img); break;
  74. case "bmp":
  75. include_once("ext_scripts/gd/bmp.php");
  76. ImageBMP($this->img);
  77. break;
  78. case "ico":
  79. include_once("ext_scripts/gd/ico.php");
  80. ImageICO($this->img);
  81. break;
  82. case "cur":
  83. include_once("ext_scripts/gd/cur.php");
  84. ImageCUR($this->img);
  85. break;
  86. /* case "ani":
  87. include_once("ext_scripts/gd/ani.php");
  88. ImageANI($this->img);
  89. break;*/
  90. default: if (ImageTypes() & IMG_PNG) ImagePNG($this->img); break;
  91. }
  92. }
  93. if ($destroy) ImageDestroy($this->img);
  94. }
  95. function SetFont($font = NULL, $font_size = NULL) {
  96. if ($font) $this->font = $font;
  97. else $this->font = $cfg["t_font_path"];
  98. if (!$this->font) $this->font = "ext_inc/fonts/verdana.ttf";
  99. if ($font_size) $this->font_size = $font_size;
  100. else $this->font_size = $cfg["t_font_size"];
  101. if (!$this->font_size) $this->font_size = 8;
  102. }
  103. #### Draws the text $text @ $xpos, $ypos, with color $color.
  104. ## The text will be cut to a maximum of $max_length chars.
  105. function Text($xpos, $ypos, $color, $text, $max_length = NULL, $angle = NULL){
  106. global $cfg;
  107. if ($max_length > 4) if (strlen($text) > $max_length) $text = substr($text, 0, $max_length-3) . "...";
  108. if ($angle == "") $angle = 0;
  109. if ($this->free_type) ImageTtfText($this->img, $this->font_size, $angle, $xpos, $ypos + $this->font_size, $color, $this->font, $text);
  110. else {
  111. $ypos = $ypos - 3;
  112. $text_parts = split ("\r\n", $text);
  113. $i = 0;
  114. while (list ($key, $val) = each ($text_parts)) {
  115. ImageString($this->img, 2, $xpos, $ypos + $i, $val, $color);
  116. $i += ($this->font_size + 2);
  117. }
  118. }
  119. }
  120. /*
  121. function CreateButton($name) {
  122. global $auth, $language, $func;
  123. if (!file_exists("ext_inc/auto_images/{$auth["design"]}/$language/button_$name.png")) {
  124. $func->CreateDir("ext_inc/auto_images/{$auth["design"]}");
  125. $func->CreateDir("ext_inc/auto_images/{$auth["design"]}/$language");
  126. $text = t($name);
  127. if (strlen($text) <= 10) {
  128. $start_x = 34 - (strlen($text) * 6) / 2;
  129. $this->img = ImageCreateFromPNG("design/{$auth["design"]}/images/button.png");
  130. } elseif (strlen($text) <= 15) {
  131. $start_x = 49 - (strlen($text) * 6) / 2;
  132. $this->img = ImageCreateFromPNG("design/{$auth["design"]}/images/button_b.png");
  133. } else {
  134. $start_x = 64 - (strlen($text) * 6) / 2;
  135. if (strlen($text) > 20) $text = substr($text, 0, 20);
  136. $this->img = ImageCreateFromPNG("design/{$auth["design"]}/images/button_c.png");
  137. }
  138. $this->SetFont("ext_inc/fonts/verdana.ttf", 7);
  139. $this->Text ($start_x, 4, imagecolorallocate($this->img, 30, 30, 30), $text, 20);
  140. $this->PutImage("ext_inc/auto_images/{$auth["design"]}/$language/button_$name.png");
  141. }
  142. }
  143. */
  144. function OpenImage($filename) {
  145. if (!file_exists($filename)) return 0;
  146. $type = strtolower(substr($filename, strrpos($filename, ".") + 1, 4));
  147. switch ($type) {
  148. default: return 0; break;
  149. case "png": if (ImageTypes() & IMG_PNG) $img_src = ImageCreateFromPNG($filename); break;
  150. case "jpeg":
  151. case "jpg": if (ImageTypes() & IMG_JPG) $img_src = ImageCreateFromJPEG($filename); break;
  152. case "gif": if (ImageTypes() & IMG_GIF) $img_src = ImageCreateFromGIF($filename); break;
  153. case "wbmp": if (ImageTypes() & IMG_WBMP) $img_src = ImageCreateFromWBMP($filename); break;
  154. case "bmp":
  155. include_once("ext_scripts/gd/bmp.php");
  156. $img_src = ImageCreateFromBMP($filename);
  157. break;
  158. case "ico":
  159. include_once("ext_scripts/gd/ico.php");
  160. $img_src = ImageCreateFromICO($filename);
  161. break;
  162. case "cur":
  163. include_once("ext_scripts/gd/cur.php");
  164. $img_src = ImageCreateFromCUR($filename);
  165. break;
  166. /* case "ani":
  167. include_once("ext_scripts/gd/ani.php");
  168. $img_src = ImageCreateFromANI($filename);
  169. break;*/
  170. }
  171. return $img_src;
  172. }
  173. function CreateThumb($old_file, $new_file, $max_width, $max_height) {
  174. global $func;
  175. if (($old_file != $new_file) and file_exists($new_file)) return;
  176. $imgsrc_old = $this->OpenImage($old_file);
  177. if (!$imgsrc_old) {
  178. $func->error("Could not open source file '$old_file'", NO_LINK);
  179. return false;
  180. } else {
  181. // Calculate new size
  182. $old_width = imagesx($imgsrc_old);
  183. $old_height = imagesy($imgsrc_old);
  184. $ratio_x = $old_width / $max_width;
  185. $ratio_y = $old_height / $max_height;
  186. if ($old_width <= $max_width and $old_height <= $max_height) {
  187. $new_width = $old_width;
  188. $new_height = $old_height;
  189. } elseif ($ratio_x > $ratio_y and $ratio_x > 0) {
  190. $new_width = $max_width;
  191. $new_height = $old_height / $ratio_x;
  192. } elseif ($ratio_y > 0) {
  193. $new_width = $old_width / $ratio_y;
  194. $new_height = $max_height;
  195. } else {
  196. $func->error("Source file '$old_file' has 0x0 pixel", NO_LINK);
  197. return false;
  198. }
  199. $this->NewImage($new_width, $new_height);
  200. ImageCopyResized($this->img, $imgsrc_old, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
  201. imagecolortransparent($this->img, imagecolorallocate($this->img, 255, 255, 255));
  202. $this->PutImage($new_file);
  203. ImageDestroy($imgsrc_old);
  204. }
  205. return true;
  206. }
  207. function Colorize($source_file, $r, $g, $b, $percentage) {
  208. if ($source_file) {
  209. $this->img = $this->OpenImage($source_file);
  210. $this->width = imagesx($this->img);
  211. $this->height = imagesy($this->img);
  212. }
  213. // Create Layover
  214. $layover = imagecreate($this->width, $this->height);
  215. $fill = imagefill($layover, 0, 0, imagecolorallocate($layover, $r, $g, $b));
  216. // Merge Layover with original image
  217. $merge = imagecopymerge($this->img, $layover, 0, 0, 0, 0, $this->width, $this->height, $percentage);
  218. imagedestroy($layover);
  219. }
  220. function MergeImages($source_file1, $source_file2, $target_file) {
  221. $this->img = $this->OpenImage($source_file1);
  222. $this->width = imagesx($this->img);
  223. $this->height = imagesy($this->img);
  224. $source_img2 = $this->OpenImage($source_file2);
  225. $source_img2_width = imagesx($source_img2);
  226. $source_img2_height = imagesy($source_img2);
  227. $merge = imagecopymerge($this->img, $source_img2, 0, 0, 0, 0, $this->width, $this->height, 25);
  228. imagedestroy($source_img2);
  229. $this->PutImage($target_file);
  230. }
  231. }
  232. ?>