Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: ImageTTFText + polskie znaki
Forum PHP.pl > Forum > PHP
jakub_klamca
Witam,

mam prośbę o pomoc w rozwiązaniu problemu z (chyba) kodowaniem polskich znaków w niektórych fontach, których używam do tworzenia plików png.

skrypt wygląda tak (jes to tylko część kodu php, służąca do wglądu, całość można znależźć tutaj

http://www.stewartspeak.com/projects/dtr/ ):

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



czcionki, z którymi mam problem można pobrać w celach testowych stąd :
http://www.domtar.pl/fonty.zip


w dokumentacji do skryptu można znaleźć taką uwagę :

"Some fonts do not contain glyphs for every character you may need. When this happens, such as with certain non-English characters, you will see blocks instead of words. The only fix for this problem is to use a different font."

Używane przezemnie czcionki są polskojęzyczne, W windowsie czy Photoshopie nie ma problemu z polskimi znakami, więc teoretycznie nie powinno być ich również w php.

Moje przypuszczenia : istnieją różne sposoby kodowania znaków w plikach ttf (tak samo jak istnieje ttf type 1 i ttf type 2, o czym wiele osób nie ma zielonego pojęcia), tylko jak zmienić kodowanie w takiej czcionce ?

Jeżeli ktoś wie o jakimś programie do konwersji czcionek (nawet komercyjnym) lub o jakimś innym skrypcie który nie będzie mieć problemów z polskimi znakami, niech da znać.


Dziękuję za wszytskie odpowiedzi.





Sprawa rozwiązana, problem tkwi w tzw mapowaniu fontów (czyli zdaje się coś jak kodowanie).

Na krasnalu propblemy ze znakami znikają po zapisaniu w czciokach takich dwóch mapowań, wygenerownych z PostScriptu :

Macitosh Roman
Microsoft Unicode BMP only.

Można to zrobić wersją testową programu FontCreator 5.5

Pradowpodbnie pod unixami również problemy znikną.
Hazel
Mógłby ktoś opisać, jak to NAPRAWDĘ zrobić?
Bo ja się nie łapię w tym mapowaniu fontów, a mam dokładnie taki sam problem :/
Ściągnąłem Font Creatora 5.5 i niewiele z tego rozumiem blinksmiley.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.