PDA

View Full Version : php gd and copyright symbol


FunkyRes
25th December 2008, 10:58 AM
Following string variable -


$lstring="©" . date("Y") . " My Real Name";


Draw it on an image -


imagefttext($im, $fontsize, 0, $xp, $yp, $black, $fontfile, $lstring);


produces


'2008 My Real Name


I want it to produce


©2008 My Real Name


The fonts I've tried - New Century Schoolbook (Type 1 - Roman Face) and Courier (Type 1 - Roman and Oblique faces)

Clearly gd or php doesn't like me directly inputting the © symbol into a string to be drawn on an image. What is the proper way to get gd library to fetch that character from the font?

dr death
26th December 2008, 08:30 AM
You say you are using type 1 fonts. However, you use the imagefttext() function which loads True Type fonts. For type 1 fonts, you would use imagepsloadfont(), imagepstext() and imagepsfreefont(). Are you sure that you are using type1 fonts?

On my machine (F9) it seems that PHP is not compiled with t1lib support, so I can't test those functions. I have tested with a True Type font (from the msttcore-fonts package) and that seems to work fine. So if you are actually using true type fonts, you could try doing the UTF conversion on the fly, or using the numerical character reference. One of these may work for you...

$im = imagecreatetruecolor(200, 100);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 200, 100, $white);
$text = html_entity_decode("©", ENT_COMPAT, "UTF-8"). date("Y") . " My Real Name";
// $text = "©" . date("Y") . " My Real Name";
$font="/usr/share/fonts/msttcore/cour.ttf";
imagefttext($im, 12, 0, 0, 10, $black, $font, $text);
header ("Content-type: image/png");
imagepng ($im);
imagedestroy($im);

FunkyRes
26th December 2008, 11:00 AM

imagefttext() function which loads True Type fonts

uses freetype2 (hence the ft between image and text) and works quite will with Type1 fonts. Thus by using the FreeType functions, I don't have to change my code to change between TrueType and Type 1 fonts.

Many builds of php are not compiled against t1lib and thus the freetype method is the only option to load Type1 fonts.

-=-
However, it seems your solution only works if a ttf font is specified - with a type 1 font, it does the some thing as direct input of the copyright symbol (make a ' )

So I guess I need to file a bug report.

dr death
26th December 2008, 02:31 PM
Interesting, and very useful too. I guess t1lib is not needed.

The documentation for imagefttext() at php.net http://www.php.net/manual/en/function.imagefttext.php is a bit misleading. For the font file parameter they say "The path to the TrueType font you wish to use" and then go on to say a ".ttf" will be appended if the filename does not start with a "/". Maybe it was just a copy and paste from imagettftext()...