0

I try to draw some text on the image. For that I use imagettftext():

$sign_str = "плюс";
$img = imagecreatetruecolor(360, 150);
$font = "StalinistOne-Regular.ttf";
$background_color_arr = array(rand(0, 255), rand(0, 255), rand(0, 255));
$background_color = imagecolorallocate($img, $background_color_arr[0], $background_color_arr[1], $background_color_arr[2]);
$font_color = imagecolorallocate($img, 255-$background_color_arr[0], 255-$background_color_arr[1], 255-$background_color_arr[2]);
imagefill($img, 0, 0, $background_color);

$x = rand(0, 135);
for($i = 0; $i < strlen($sign_str); $i++) {
    $x+=25;
    $letter=substr($sign_str, $i, 1);
    imagettftext ($img, rand(20, 30), rand(2, 4), $x, rand(72, 80), $font_color, $font, $letter);
}
ImagePNG ($img);
ImageDestroy ($img);

But I get some strange symbols (not cyrillic, not empty rectangles like if the font doesn't support it). enter image description here

And in "fonts" app in my system I can see that this font support cyrillic: enter image description here

What do I do wrong? I tried to look some solutions on SO and not only, but they didn't work for me.

Working with GD ( imagettftext() ) and UTF-8 characters

PHP function imagettftext() and unicode

etc.

Dmitry
  • 160
  • 9

1 Answers1

0

Solved by using mb_substr instead of subst here:

$letter=substr($sign_str, $i, 1);

Found sollution here (russian): https://ru.stackoverflow.com/questions/285231/php-gd-%D0%BD%D0%B5%D0%BF%D0%BE%D0%BD%D1%8F%D1%82%D0%BD%D1%8B%D0%B5-%D1%81%D0%B8%D0%BC%D0%B2%D0%BE%D0%BB%D1%8B

Translated: "The substr function works, in fact, with bytes. Russian letters are encoded in several bytes, so as a result the string is cut not by characters, but by parts of characters, and the "part of a character" in this case is decoded as a completely different character."

Dmitry
  • 160
  • 9