By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Character set problems with imagettftext

P: n/a
I cannot get the swedish character set on images. What can be wrong?
I would very much appreciate help.

regards, axier

I can supply some code here and a link:
http://www.mymobile.nu/services/fontgen/fonttest.php

Some people looks like they can get it to work with no effort at all
with the same script.
The code looks like this:

<?
setlocale (LC_ALL, 'swedish');
$sizes = 10;
$startsize = 6;
$printText = "A B C D E F G H ABCDEFGH AOU EIYÄ ";
# $printText = htmlentities ($printText,ENT_QUOTES, 'UTF-8');
# $printText = utf8_encode ($printText);
$convertExec = "/usr/bin/convert";

$thisDir = '/fonts/';
$thisFont ='arial.ttf';
echo "$thisFont<br>";

for($i=$startsize;$i<$sizes;$i++) {
$im = imagecreate (400, 30);
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);

ImageTTFText ($im, $i , 0, 10, 20, -$white, $thisDir.$thisFont, $printText);
ImagePng ($im,'test'.$i.'.png');
echo $i.' <img src="test'.$i.'.png"><br>';
ImageDestroy ($im);
}
?>
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 5 Jan 2005 13:35:24 -0800, jf****@hotmail.com (Axier) wrote:
I cannot get the swedish character set on images. What can be wrong?
I would very much appreciate help.
I submitted a documentation bug not long ago
(http://bugs.php.net/bug.php?id=30801) against the imagettftext function as the
text parameter's description was wrong. Just checked the page and it looks like
the latest update has now been put on the PHP site.

The main points are:

(1) The text parameter requires UTF8 encoding.
(2) It only accepts decimal numeric character entities; not named ones nor
hexadecimal ones.
I can supply some code here and a link:
http://www.mymobile.nu/services/fontgen/fonttest.php

Some people looks like they can get it to work with no effort at all
with the same script.
The code looks like this:

<?
setlocale (LC_ALL, 'swedish');
$sizes = 10;
$startsize = 6;
$printText = "A B C D E F G H ABCDEFGH AOU EIYÄ ";
isn't valid UTF8 so you're potentially in trouble there to start with. That
character is 0xC5; in binary it's 11000101 so it looks like the lead byte of a
two-byte character. The next byte is a space, which isn't a valid trail byte,
so you've got an invalid string there from the point of view of imagettftext.

Having said that, it looks like it has a go at interpreting as
ISO-8859-something when it hits invalid UTF-8.

Ä looks better though, that's 'LATIN CAPITAL LETTER A WITH DIAERESIS'
(U+00C4).
# $printText = htmlentities ($printText,ENT_QUOTES, 'UTF-8');
htmlentities will convert to named entities, which imagettftext doesn't
accept, and additionally you're claiming the string is UTF-8 in this call when
it's not.

And it'll escape the Ä so you end up with that literally in your text
rather than .
# $printText = utf8_encode ($printText);


OK, perhaps along the right lines to get the literal working...

In fact the code you posted gives the results I'd expect when I run it on PHP
5.0.3 on Windows. Haven't tried it on Unix though as it looks like I've not
compiled TTF support into my test machine's PHP.

Are you running the same code? Looks like you may have uncommented some of the
lines you have commented in your post as you've got the literal Ä in the
images?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.