UTF-8 Characters In Images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

UTF-8 Characters In Images

Post 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:
Image
But my nick is Јαςқρғ, which does not display properly.

Is their a way to fix this?

Thanks,
Jack.
Last edited by jackpf on Sat Jan 09, 2010 5:56 am, edited 1 time in total.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: UTF-8 Characters In Images

Post 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?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: UTF-8 Characters In Images

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UTF-8 Characters In Images

Post by jackpf »

aravona wrote: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?
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 8O 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().
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: UTF-8 Characters In Images

Post by Apollo »

jackpf wrote:I'm not sure tbh 8O 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 Image on my machine.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: UTF-8 Characters In Images

Post by jackpf »

Ahh yeah that works perfectly thanks. I guess I needed the arial font.

Cheers.
Post Reply