0

When I create an image with PHP, it does not display UTF-8 characters properly.

$imgPath = 'uplatnica1.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 000, 000, 000);

$string = "šđčćž";
$x = 70;
$y = 200;

$fontSize = 3;

imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image, 'qwe.jpg');

How can I solve this problem?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
user2701076
  • 25
  • 2
  • 4
  • Take a look at this: http://stackoverflow.com/questions/9458317/working-with-gd-imagettftext-and-utf-8-characters – becquerel Oct 09 '13 at 07:49
  • Maybe your problem is in the font you are using; In some post I found that you must have both the GD library and the FreeType library installed in the server. – Lan Oct 09 '13 at 07:54

2 Answers2

0

It's always good to read documentation prior asking

font

Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont().

and UTF8 is not subset of latin2

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

You could check out imagettftext.

Your code will become:

$imgPath = 'uplatnica1.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 000, 000, 000);

$string = "šđčćž";
$x = 70;
$y = 200;

$fontSize = 3;
$fontFile = 'verdana.ttf'; // This file must reside on your system

imagettftext($image, $fontSize, 0, $x, $y, $color, $fontFile, $string);
imagejpeg($image, 'qwe.jpg');

Don't forget that your $fontFile must exist on the server and be accessible by the code.

James
  • 5,137
  • 5
  • 40
  • 80
  • Warning: imagettftext() [function.imagettftext]: Invalid font filename in C:\xampp\htdocs\prirucnici-akcija\slika2.php on line 13 – user2701076 Oct 09 '13 at 08:10
  • Have you placed `verdana.ttf` in the same directory as the script? – James Oct 09 '13 at 08:12
  • Warning: imagettftext() [function.imagettftext]: any2eucjp(): something happen in C:\xampp\htdocs\prirucnici-akcija\slika2.php on line 13 – user2701076 Oct 09 '13 at 08:19
  • Have you read the documentation? I may have missed something. Just double check that `verdana.ttf` is in the **same** directory as this script. – James Oct 09 '13 at 08:57