Page 1 of 1
UTF-8 Characters In Images
Posted: Thu Jan 07, 2010 6:05 am
by jackpf
Hi,
Basically, i was wondering if it's possible to use UTF-8 characters in a dynamic image.
For example, I'd like to display the user's nick rather than their username on this image, but nicks often contain UTF-8 characters which don't display properly.
Eg:

But my nick is Јαςқρғ, which does not display properly.
Is their a way to fix this?
Thanks,
Jack.
Re: UTF-8 Characters In Images
Posted: Thu Jan 07, 2010 6:08 am
by aravona
They're not normal characters. I've never done what you're asking before but I'm hoping this site may help?
http://www.webmonkey.com/reference/Special_Characters
should be the same as making a > < show up in html?
Re: UTF-8 Characters In Images
Posted: Thu Jan 07, 2010 6:28 am
by Apollo
1. Are you sure the font you're using contains those characters?
2. Are you sure you're passing the text in the encoding that your print-on-image-function expects?
Re: UTF-8 Characters In Images
Posted: Fri Jan 08, 2010 3:31 am
by jackpf
Well, this is in an image...so it has nothing to do with HTML. Thanks for trying though
Apollo wrote:1. Are you sure the font you're using contains those characters?
2. Are you sure you're passing the text in the encoding that your print-on-image-function expects?
1. I'm just using the default font. I tried downloading one and using it...but I got the same results.
2. I'm not sure tbh

If printed to the browser with UTF-8 encoding, it displays fine, so the data is stored correctly in the script. So it must be garbling it in imagestring().
Re: UTF-8 Characters In Images
Posted: Fri Jan 08, 2010 5:26 am
by Apollo
jackpf wrote:I'm not sure tbh

If printed to the browser with UTF-8 encoding, it displays fine, so the data is stored correctly in the script. So it must be garbling it in imagestring().
In that case, try the
imagettftext function instead. It accepts UTF-8 strings.
For example:
Code: Select all
<?php
$img = imagecreatetruecolor(100,50); // create image
imagefill($img,0,0,imagecolorallocate($img,0,50,0)); // fill background
$jackpf = "\xD0\x88\xCE\xB1\xCF\x82\xD2\x9B\xCF\x81\xD2\x93"; // this is 'jackpf' in freaky chars, UTF-8 encoded
$a = imagettftext($img,15,0,10,30,imagecolorallocate($img,255,255,0),'Arial.ttf',$jackpf ); // print text (note: Arial.ttf must exist!)
if (!$a) die('error');
// output image:
header("Pragma: public");
header("Expires: 0");
header('Content-Type: image/jpeg');
imagejpeg($img);
?>
Results in

on my machine.
Re: UTF-8 Characters In Images
Posted: Fri Jan 08, 2010 8:16 am
by jackpf
Ahh yeah that works perfectly thanks. I guess I needed the arial font.
Cheers.