Advertisement
Guest User

Untitled

a guest
Feb 7th, 2012
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. // customizable variables
  5.  
  6. $font_file = 'ambient.ttf';
  7.  
  8. $font_size = 23 ; // font size in pts
  9.  
  10. $font_color = '#FF0000';
  11.  
  12. $image_file = 'xxx.png';
  13.  
  14.  
  15.  
  16. // x and y for the bottom right of the text
  17.  
  18. // so it expands like right aligned text
  19.  
  20. $x_finalpos = 273;
  21.  
  22. $y_finalpos = 103;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. // trust me for now...in PNG out PNG
  29.  
  30. $mime_type = 'image/png' ;
  31.  
  32. $extension = '.png' ;
  33.  
  34. $s_end_buffer_size = 4096 ;
  35.  
  36.  
  37.  
  38.  
  39.  
  40. // check for GD support
  41.  
  42. if(!function_exists('ImageCreate'))
  43.  
  44. fatal_error('Error: Server does not support PHP image generation') ;
  45.  
  46.  
  47.  
  48. // check font availability;
  49.  
  50. if(!is_readable($font_file)) {
  51.  
  52. fatal_error('Error: The server is missing the specified font.') ;
  53.  
  54. }
  55.  
  56.  
  57.  
  58. // create and measure the text
  59.  
  60. $font_rgb = hex_to_rgb($font_color) ;
  61.  
  62. $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
  63.  
  64.  
  65.  
  66. $text_width = abs($box[2]-$box[0]);
  67.  
  68. $text_height = abs($box[5]-$box[3]);
  69.  
  70.  
  71.  
  72. $image = imagecreatefrompng($image_file);
  73.  
  74.  
  75.  
  76. if(!$image || !$box)
  77.  
  78. {
  79.  
  80. fatal_error('Error: The server could not create this image.') ;
  81.  
  82. }
  83.  
  84.  
  85.  
  86. // allocate colors and measure final text position
  87.  
  88. $font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;
  89.  
  90.  
  91.  
  92. $image_width = imagesx($image);
  93.  
  94.  
  95.  
  96. $put_text_x = $image_width - $text_width - ($image_width - $x_finalpos);
  97.  
  98. $put_text_y = $y_finalpos;
  99.  
  100.  
  101.  
  102. // Write the text
  103.  
  104. imagettftext($image, $font_size, 0, $put_text_x, $put_text_y, $font_color, $font_file, $text);
  105.  
  106.  
  107.  
  108.  
  109.  
  110. header('Content-type: ' . $mime_type) ;
  111.  
  112. ImagePNG($image) ;
  113.  
  114.  
  115.  
  116. ImageDestroy($image) ;
  117.  
  118. exit ;
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /*
  125.  
  126. attempt to create an image containing the error message given.
  127.  
  128. if this works, the image is sent to the browser. if not, an error
  129.  
  130. is logged, and passed back to the browser as a 500 code instead.
  131.  
  132. */
  133.  
  134. function fatal_error($message)
  135.  
  136. {
  137.  
  138. // send an image
  139.  
  140. if(function_exists('ImageCreate'))
  141.  
  142. {
  143.  
  144. $width = ImageFontWidth(5) * strlen($message) + 10 ;
  145.  
  146. $height = ImageFontHeight(5) + 10 ;
  147.  
  148. if($image = ImageCreate($width,$height))
  149.  
  150. {
  151.  
  152. $background = ImageColorAllocate($image,255,255,255) ;
  153.  
  154. $text_color = ImageColorAllocate($image,0,0,0) ;
  155.  
  156. ImageString($image,5,5,5,$message,$text_color) ;
  157.  
  158. header('Content-type: image/png') ;
  159.  
  160. ImagePNG($image) ;
  161.  
  162. ImageDestroy($image) ;
  163.  
  164. exit ;
  165.  
  166. }
  167.  
  168. }
  169.  
  170.  
  171.  
  172. // send 500 code
  173.  
  174. header("HTTP/1.0 500 Internal Server Error") ;
  175.  
  176. print($message) ;
  177.  
  178. exit ;
  179.  
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. /*
  187.  
  188. decode an HTML hex-code into an array of R,G, and B values.
  189.  
  190. accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
  191.  
  192. */
  193.  
  194. function hex_to_rgb($hex) {
  195.  
  196. // remove '#'
  197.  
  198. if(substr($hex,0,1) == '#')
  199.  
  200. $hex = substr($hex,1) ;
  201.  
  202.  
  203.  
  204. // expand short form ('fff') color to long form ('ffffff')
  205.  
  206. if(strlen($hex) == 3) {
  207.  
  208. $hex = substr($hex,0,1) . substr($hex,0,1) .
  209.  
  210. substr($hex,1,1) . substr($hex,1,1) .
  211.  
  212. substr($hex,2,1) . substr($hex,2,1) ;
  213.  
  214. }
  215.  
  216.  
  217.  
  218. if(strlen($hex) != 6)
  219.  
  220. fatal_error('Error: Invalid color "'.$hex.'"') ;
  221.  
  222.  
  223.  
  224. // convert from hexidecimal number systems
  225.  
  226. $rgb['red'] = hexdec(substr($hex,0,2)) ;
  227.  
  228. $rgb['green'] = hexdec(substr($hex,2,2)) ;
  229.  
  230. $rgb['blue'] = hexdec(substr($hex,4,2)) ;
  231.  
  232.  
  233.  
  234. return $rgb ;
  235.  
  236. }
  237.  
  238. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement