PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ php-ppcms/includes/classes/lib.image.text.class.php

http://php-ppcms.googlecode.com/
PHP | 333 lines | 222 code | 48 blank | 63 comment | 43 complexity | a4eed74fa4384b9dca4d0d796a50e1f7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. class LibImageText {
  9. //text
  10. var $text = '';
  11. //
  12. var $font_path = 'images/fonts/';
  13. var $font_list = array(
  14. 'Antykwa' => array('spacing' => -3, 'minSize' => 27, 'maxSize' => 40, 'font' => 'AntykwaBold.ttf'),
  15. 'Candice' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 31, 'font' => 'Candice.ttf'),
  16. 'DingDong' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'Ding-DongDaddyO.ttf'),
  17. 'Duality' => array('spacing' => -2, 'minSize' => 30, 'maxSize' => 38, 'font' => 'Duality.ttf'),
  18. 'Heineken' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 34, 'font' => 'Heineken.ttf'),
  19. 'Jura' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 32, 'font' => 'Jura.ttf'),
  20. 'StayPuft' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 32, 'font' => 'StayPuft.ttf'),
  21. 'Times' => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 34, 'font' => 'TimesNewRomanBold.ttf'),
  22. 'VeraSans' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'VeraSansBold.ttf'),
  23. 'HANDGOTB' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'HANDGOTB.TTF'),
  24. );
  25. var $font_name = 'HANDGOTB';
  26. //Background color
  27. var $backgroundColor = array(248, 248, 248);
  28. //Foreground color
  29. var $foregroundColor = array(0, 0, 0);
  30. //Foreground colors
  31. var $colors = array(
  32. //array(27,78,181), // blue
  33. //array(22,163,35), // green
  34. //array(214,36,7), // red
  35. array(0,0,0), // black
  36. //array(255,255,255),//white
  37. );
  38. //Shadow Color
  39. var $shadowColor = null; //array(0, 0, 0);
  40. //Min word length (for non-dictionary random text generation)
  41. var $minWordLength = 5;
  42. /**
  43. * Max word length (for non-dictionary random text generation)
  44. *
  45. * Used for dictionary words indicating the word-length
  46. * for font-size modification purposes
  47. */
  48. var $maxWordLength = 30;
  49. //
  50. var $width = 480;
  51. var $height = 50;
  52. var $imageFormat = 'jpg';
  53. //
  54. var $save_path = 'images/texts/';
  55. var $save_type = '';
  56. var $save_image = false;
  57. var $save_filename = '';
  58. var $save_name = '';
  59. //Internal image size factor (for better image quality)
  60. //1: low, 2: medium, 3: high
  61. var $scale = 1;
  62. //
  63. var $im;
  64. //constructor
  65. function LibImageText() {
  66. }
  67. //methods
  68. function image() {
  69. $text = $this->text;
  70. //
  71. $this->_image_allocate();
  72. if( $this->font_name != '' && isset($this->font_list[$this->font_name]) ) {
  73. $fontcfg = $this->font_list[$this->font_name];
  74. } else {
  75. $fontcfg = $this->font_list[array_rand($this->font_list)];
  76. }
  77. $this->_image_text_wh($text, $fontcfg);
  78. if( $this->blur && function_exists('imagefilter') ) {
  79. imagefilter($this->im, IMG_FILTER_GAUSSIAN_BLUR);
  80. }
  81. $this->_image_reduce();
  82. if( $this->save_image == true ) {
  83. $this->_image_save();
  84. } else {
  85. $this->_image_write();
  86. }
  87. $this->_image_cleanup();
  88. }
  89. //
  90. function _image_allocate() {
  91. // Cleanup
  92. if (!empty($this->im)) {
  93. imagedestroy($this->im);
  94. }
  95. $this->im = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
  96. // Background color
  97. $this->GdBgColor = imagecolorallocate($this->im,
  98. $this->backgroundColor[0],
  99. $this->backgroundColor[1],
  100. $this->backgroundColor[2]
  101. );
  102. imagefilledrectangle($this->im, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $this->GdBgColor);
  103. // Foreground color
  104. $color = $this->colors[mt_rand(0, sizeof($this->colors)-1)];
  105. $this->GdFgColor = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);
  106. // Shadow color
  107. if (!empty($this->shadowColor) && is_array($this->shadowColor) && sizeof($this->shadowColor) >= 3) {
  108. $this->GdShadowColor = imagecolorallocate($this->im,
  109. $this->shadowColor[0],
  110. $this->shadowColor[1],
  111. $this->shadowColor[2]
  112. );
  113. }
  114. }
  115. //
  116. function _image_text($text, $fontcfg = array()) {
  117. if (empty($fontcfg)) {
  118. // Select the font configuration
  119. $fontcfg = $this->font_list[array_rand($this->font_list)];
  120. }
  121. // Full path of font file
  122. $fontfile = $this->font_path . $fontcfg['font'];
  123. //Increase font-size for shortest words: 9% for each glyp missing
  124. $lettersMissing = $this->maxWordLength-strlen($text);
  125. $fontSizefactor = 1+($lettersMissing*0.09);
  126. // Text generation (char by char)
  127. $x = 20*$this->scale;
  128. $y = round(($this->height*27/40)*$this->scale);
  129. $length = strlen($text);
  130. for ($i=0; $i<$length; $i++) {
  131. //$degree = rand($this->maxRotation*-1, $this->maxRotation);
  132. //$fontsize = rand($fontcfg['minSize'], $fontcfg['maxSize'])*$this->scale*$fontSizefactor;
  133. $degree = 0;
  134. $fontsize = $fontcfg['maxSize']*$this->scale*$fontSizefactor;
  135. $letter = substr($text, $i, 1);
  136. if( $this->shadowColor ) {
  137. $coords = imagettftext($this->im, $fontsize, $degree,
  138. $x+$this->scale, $y+$this->scale,
  139. $this->GdShadowColor, $fontfile, $letter);
  140. }
  141. $coords = imagettftext($this->im, $fontsize, $degree,
  142. $x, $y,
  143. $this->GdFgColor, $fontfile, $letter);
  144. $x += ($coords[2]-$x) + ($fontcfg['spacing']*$this->scale);
  145. }
  146. }
  147. //
  148. function _image_texts($text, $fontcfg = array()) {
  149. if (empty($fontcfg)) {
  150. // Select the font configuration
  151. $fontcfg = $this->font_list[array_rand($this->font_list)];
  152. }
  153. // Full path of font file
  154. $fontfile = $this->font_path . $fontcfg['font'];
  155. //Increase font-size for shortest words: 9% for each glyp missing
  156. $lettersMissing = $this->maxWordLength-strlen($text);
  157. $fontSizefactor = 1+($lettersMissing*0.09);
  158. // Text generation (char by char)
  159. $x = 20*$this->scale;
  160. $y = round(($this->height*27/40)*$this->scale);
  161. $degree = 0;
  162. $fontsize = $fontcfg['maxSize']*$this->scale*$fontSizefactor;
  163. //$fontsize = 20;
  164. //
  165. if( $this->shadowColor ) {
  166. $coords = imagettftext($this->im, $fontsize, $degree,
  167. $x+$this->scale, $y+$this->scale,
  168. $this->GdShadowColor, $fontfile, $text);
  169. }
  170. $coords = imagettftext($this->im, $fontsize, $degree,
  171. $x, $y,
  172. $this->GdFgColor, $fontfile, $text);
  173. }
  174. //
  175. function _image_text_wh($text, $fontcfg = array()) {
  176. if (empty($fontcfg)) {
  177. // Select the font configuration
  178. $fontcfg = $this->font_list[array_rand($this->font_list)];
  179. }
  180. // Full path of font file
  181. $fontfile = $this->font_path . $fontcfg['font'];
  182. $text_size = 20;
  183. $text_angle = 0;
  184. $text_box = imagettfbbox($text_size, $text_angle, $fontfile, $text);
  185. $text_w = $text_box['2'] - $text_box['0'];
  186. $text_h = $text_box['3'] - $text_box['5'];
  187. //$text_x = ceil(($this->width - $text_w)/2);
  188. //$text_y = $text_h+10;
  189. $text_x = 10;
  190. $text_y = $text_h + ceil(($this->height - $text_h)/2);
  191. imagettftext($this->im, $text_size, $text_angle, $text_x, $text_y, $this->GdFgColor, $fontfile, $text);
  192. }
  193. //
  194. function _image_reduce() {
  195. $imResampled = imagecreatetruecolor($this->width, $this->height);
  196. imagecopyresampled($imResampled, $this->im,
  197. 0, 0, 0, 0,
  198. $this->width, $this->height,
  199. $this->width*$this->scale, $this->height*$this->scale
  200. );
  201. imagedestroy($this->im);
  202. $this->im = $imResampled;
  203. }
  204. //
  205. function _image_write() {
  206. if( $this->imageFormat == 'png' && function_exists('imagepng') ) {
  207. header("Content-type: image/png");
  208. imagepng($this->im);
  209. } else {
  210. header("Content-type: image/jpeg");
  211. imagejpeg($this->im, null, 80);
  212. }
  213. }
  214. //
  215. function _image_save() {
  216. $file = $this->_image_file();
  217. if( $this->imageFormat == 'png' && function_exists('imagepng') ) {
  218. imagepng($this->im, $file);
  219. } else {
  220. imagejpeg($this->im, $file, 80);
  221. }
  222. }
  223. //
  224. function _image_cleanup() {
  225. imagedestroy($this->im);
  226. $this->im = NULL;
  227. }
  228. //
  229. function _image_file() {
  230. $filename = $this->_image_filename();
  231. $this->save_filename = $filename;
  232. return $this->save_path . $filename;
  233. }
  234. //
  235. function _image_filename() {
  236. $filename = $this->_generate_filepath() . $this->_generate_filename();
  237. if( file_exists($this->save_path . $filename) ) {
  238. return $this->_image_filename();
  239. }
  240. return $filename;
  241. }
  242. //
  243. function _random() {
  244. return util_create_random_value('8', 'mixed');
  245. }
  246. function _generate_filename() {
  247. if( $this->imageFormat == 'png' && function_exists('imagepng') ) {
  248. $extension = 'png';
  249. } else {
  250. $extension = 'jpg';
  251. }
  252. if( $this->save_name != '' ) {
  253. return $this->save_name . '.' . $extension;
  254. }
  255. return substr(md5(time()), 0, 16) . '_' . $this->_random() . '.' . $extension;
  256. }
  257. function _generate_filepath() {
  258. if( !is_dir($this->save_path) ) {
  259. die("ERROR DIR");
  260. }
  261. $save_subdir = '';
  262. if( $this->save_type == 'month' ) {
  263. $save_subdir = strftime("%Y%m", time());
  264. } elseif( $this->save_type == 'day' ) {
  265. $save_subdir = strftime("%Y%m%d", time());
  266. } elseif( $this->save_type == 'random' ) {
  267. $save_subdir = substr(md5(strftime("%Y%m", time())), 0, 6);
  268. }
  269. $save_subdir = trim($save_subdir);
  270. if( strlen($save_subdir) > 0 ) {
  271. if( !is_dir($this->save_path . $save_subdir . '/') ) {
  272. @mkdir($this->save_path . $save_subdir . '/');
  273. }
  274. $filepath = $save_subdir . '/';
  275. if( !is_dir($this->save_path . $filepath) ) {
  276. die("ERROR DIRS");
  277. }
  278. } else {
  279. $filepath = $save_subdir;
  280. }
  281. return $filepath;
  282. }
  283. }
  284. //
  285. ?>