본문 바로가기
프로그램 (PHP,Python)

텍스트를 이미지로 만들기

by 날으는물고기 2009. 5. 15.

텍스트를 이미지로 만들기

  1. <?php
  2. /*
  3.     Dynamic Heading Generator
  4.     By Stewart Rosenberger
  5.     http://www.stewartspeak.com/headings/    
  6.     This script generates PNG images of text, written in
  7.     the font/size that you specify. These PNG images are passed
  8.     back to the browser. Optionally, they can be cached for later use.
  9.     If a cached image is found, a new image will not be generated,
  10.     and the existing copy will be sent to the browser.
  11.     Additional documentation on PHP's image handling capabilities can
  12.     be found at http://www.php.net/image/    
  13. */
  14. $font_file = 'fontname.ttf';
  15. $font_size = 50 ; // font size in pts
  16. $font_color = '#000000' ;
  17. $background_color = '#ffffff' ;
  18. $transparent_background = true ;
  19. /*
  20. --------------------------------------------------------------
  21. Optionally, images can be cached for later use.
  22. If a cached image is found, a new image will not be generated,
  23. and the existing copy will be sent to the browser. However,
  24. that won't happen unless a WRITABLE "cache" directory is there
  25. for the images to be stored.
  26. --------------------------------------------------------------
  27. */
  28. $cache_images = true ;
  29. $cache_folder = 'cache' ;
  30. /*
  31. -----------------
  32. Added for P+C DTR
  33. -----------------
  34. */
  35. session_start();
  36. $text_info = $_SESSION['pcdtr'][$_GET['tag']];
  37. if ($text_info['font-size']) {
  38.     $font_size = $text_info['font-size'];
  39. }
  40. if ($text_info['background-color']) {
  41.     $background_color = $text_info['background-color'];
  42. }
  43. if ($text_info['color']) {
  44.     $font_color = $text_info['color'];
  45. }
  46. /*
  47. -----------------------
  48. End of P+C DTR Addition
  49. -----------------------
  50. */
  51. /*
  52. ---------------------------------------------------------------------------
  53. For basic usage, you should not need to edit anything below this comment.
  54. If you need to further customize this script's abilities, make sure you
  55. are familiar with PHP and its image handling capabilities.
  56. ---------------------------------------------------------------------------
  57. */
  58. $mime_type = 'image/png' ;
  59. $extension = '.png' ;
  60. $send_buffer_size = 4096 ;
  61. // check for GD support
  62. if(!function_exists('ImageCreate'))
  63.     fatal_error('Error: Server does not support PHP image generation') ;
  64. // clean up text
  65. if(empty($_GET['text']))
  66.     fatal_error('Error: No text specified.') ;
  67.     
  68. $text = html_entity_decode($_GET['text']).' ' ;
  69. if(get_magic_quotes_gpc())
  70.     $text = stripslashes($text) ;
  71. $text = javascript_to_html($text) ;
  72. // look for cached copy, send if it exists
  73. $hash = md5(basename($font_file) . $font_size . $font_color .
  74.             $background_color . $transparent_background . $text) ;
  75. $cache_filename = $cache_folder . '/' . $hash . $extension ;
  76. if($cache_images && ($file = @fopen($cache_filename,'rb')))
  77. {
  78.     header('Content-type: ' . $mime_type) ;
  79.     while(!feof($file))
  80.         print(($buffer = fread($file,$send_buffer_size))) ;
  81.     fclose($file) ;
  82.     exit ;
  83. }
  84. // check font availability
  85. $font_found = is_readable($font_file) ;
  86. if(!$font_found)
  87. {
  88.     fatal_error('Error: The server is missing the specified font.') ;
  89. }
  90. // create image
  91. $background_rgb = hex_to_rgb($background_color) ;
  92. $font_rgb = hex_to_rgb($font_color) ;
  93. $dip = get_dip($font_file,$font_size) ;
  94. $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
  95. $image = @ImageCreate(abs($box[2]-$box[0]),abs($box[5]-$dip)) ;
  96. if(!$image || !$box)
  97. {
  98.     fatal_error('Error: The server could not create this heading image.') ;
  99. }
  100. // allocate colors and draw text
  101. $background_color = @ImageColorAllocate($image,$background_rgb['red'],
  102.     $background_rgb['green'],$background_rgb['blue']) ;
  103. $font_color = ImageColorAllocate($image,$font_rgb['red'],
  104.     $font_rgb['green'],$font_rgb['blue']) ;
  105. ImageTTFText($image,$font_size,0,-$box[0],abs($box[5]-$box[3])-$box[1],
  106.     $font_color,$font_file,$text) ;
  107. // set transparency
  108. if($transparent_background)
  109.     ImageColorTransparent($image,$background_color) ;
  110. header('Content-type: ' . $mime_type) ;
  111. ImagePNG($image) ;
  112. // save copy of image for cache
  113. if($cache_images)
  114. {
  115.     @ImagePNG($image,$cache_filename) ;
  116. }
  117. ImageDestroy($image) ;
  118. exit ;
  119. /*
  120.     try to determine the "dip" (pixels dropped below baseline) of this
  121.     font for this size.
  122. */
  123. function get_dip($font,$size)
  124. {
  125.     $test_chars = 'abcdefghijklmnopqrstuvwxyz' .
  126.                  'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
  127.                  '1234567890' .
  128.                  '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
  129.     $box = @ImageTTFBBox($size,0,$font,$test_chars) ;
  130.     return $box[3] ;
  131. }
  132. /*
  133.     attempt to create an image containing the error message given.
  134.     if this works, the image is sent to the browser. if not, an error
  135.     is logged, and passed back to the browser as a 500 code instead.
  136. */
  137. function fatal_error($message)
  138. {
  139.     // send an image
  140.     if(function_exists('ImageCreate'))
  141.     {
  142.         $width = ImageFontWidth(5) * strlen($message) + 10 ;
  143.         $height = ImageFontHeight(5) + 10 ;
  144.         if($image = ImageCreate($width,$height))
  145.         {
  146.             $background = ImageColorAllocate($image,255,255,255) ;
  147.             $text_color = ImageColorAllocate($image,0,0,0) ;
  148.             ImageString($image,5,5,5,$message,$text_color) ;    
  149.             header('Content-type: image/png') ;
  150.             ImagePNG($image) ;
  151.             ImageDestroy($image) ;
  152.             exit ;
  153.         }
  154.     }
  155.     // send 500 code
  156.     header("HTTP/1.0 500 Internal Server Error") ;
  157.     print($message) ;
  158.     exit ;
  159. }
  160. /*
  161.     decode an HTML hex-code into an array of R,G, and B values.
  162.     accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
  163. */    
  164. function hex_to_rgb($hex)
  165. {
  166.     // remove '#'
  167.     if(substr($hex,0,1) == '#')
  168.         $hex = substr($hex,1) ;
  169.     // expand short form ('fff') color
  170.     if(strlen($hex) == 3)
  171.     {
  172.         $hex = substr($hex,0,1) . substr($hex,0,1) .
  173.              substr($hex,1,1) . substr($hex,1,1) .
  174.              substr($hex,2,1) . substr($hex,2,1) ;
  175.     }
  176.     if(strlen($hex) != 6)
  177.         fatal_error('Error: Invalid color "'.$hex.'"') ;
  178.     // convert
  179.     $rgb['red'] = hexdec(substr($hex,0,2)) ;
  180.     $rgb['green'] = hexdec(substr($hex,2,2)) ;
  181.     $rgb['blue'] = hexdec(substr($hex,4,2)) ;
  182.     return $rgb ;
  183. }
  184. /*
  185.     convert embedded, javascript unicode characters into embedded HTML
  186.     entities. (e.g. '%u2018' => '&#8216;'). returns the converted string.
  187. */
  188. function javascript_to_html($text)
  189. {
  190.     $matches = null ;
  191.     preg_match_all('/%u([0-9A-F]{4})/i',$text,$matches) ;
  192.     if(!empty($matches)) for($i=0;$i<sizeof($matches[0]);$i++)
  193.         $text = str_replace($matches[0][$i],
  194.                             '&#'.hexdec($matches[1][$i]).';',$text) ;
  195.     return $text ;
  196. }
  197. ?>
728x90

댓글