skip to Main Content

The following php code produces correct results on Windows, but not on Linux.
I have no idea how to solve the problem, does anyone can give me a hint? The font is the standard Roboto font from google, which contains all required characters, as I can see under Windows.

<?php
header('Content-Type: image/jpeg');
$total_width   = 500; // Breite Gesamtbild
$total_height  = 120; // Höhe Gesamtbild
$dst_img = imagecreatetruecolor($total_width, $total_height);  // leeres Bild anlegen
$bg_color = imagecolorallocate($dst_img, 255, 255, 240);    // Hintergrundfarbe festlegen
$rand_color = imagecolorallocate($dst_img, 224, 123, 21);    // Randfarbe festlegen
$text_color = imagecolorallocate($dst_img, 1, 1, 1);        // Textfarbe festlegen
imagefill($dst_img, 0, 0, $bg_color);                       // Hintergrundfarbe malen
imagerectangle ($dst_img, 0, 0, $total_width-1, $total_height-1, $rand_color);  // Rand malen
imagerectangle ($dst_img, 1, 1, $total_width-2, $total_height-2, $rand_color);  // Rand malen (zweites Pixel)
$rp = realpath('');
$txtfont = "$rp/Roboto/Roboto-Regular.ttf";
    
$txt1 = "Viele Gr&uuml;&szlig;e";
$txt2 = html_entity_decode($txt1, ENT_QUOTES, 'UTF-8');
$txt3 = "Viele Grüße";
imagettftext($dst_img, 12, 0, 15, 28, $text_color, $txtfont, $txt1);
imagettftext($dst_img, 12, 0, 15, 58, $text_color, $txtfont, $txt2);
imagettftext($dst_img, 12, 0, 15, 88, $text_color, $txtfont, $txt3);

imagejpeg($dst_img);  // Bild liefern
imagedestroy($dst_img); // Speicher freigeben
?>

Result on Windows:
Windows:

Result on Linux:
Linux:

Expexted was to see the same result on Linux as on Windows

2

Answers


  1. Chosen as BEST ANSWER

    After some more experiments, I know the problem is not the font. On Windows all works as expected. On the Linux of my provider, the problem is that imagettftext does not work with umlauts created by html_entity_decode. But it works with numeric character references.

    So I wrote the following helper function for use as a replacement of html_entity_decode:

    function html2ttf($s): string
    {
        return str_replace(["&uuml;", "&szlig;", "&Auml;", "&auml;", "&Ouml;", "&ouml;", "&Uuml;", "&copy;", "&euro;"],
                           ['&#xFC;', '&#xDF;', '&#xC4;', '&#xE4;', '&#xD6;', '&#xF6;', '&#xDC;', '&#xA9;', '&#x20AC;'], $s);
    }
    

  2. Just to see whether the problem is with the font or with the function imagettftext, I wrote a small test. It should produce the same table twice. On Windows, it works as expected, on Linux it does not.

    This is the output on Windows:
    This is the output on Windows

    This is the output on Linux:
    This is the output on Linux

    And this is the test code:

    <?php
    header('Content-Type: image/jpeg');
    $total_width   = 860; // Breite Gesamtbild
    $total_height  = 420; // Höhe Gesamtbild
    $dst_img = imagecreatetruecolor($total_width, $total_height);  // leeres Bild anlegen
    $bg_color = imagecolorallocate($dst_img, 255, 255, 240);    // Hintergrundfarbe festlegen
    $rand_color = imagecolorallocate($dst_img, 224, 123, 21);    // Randfarbe festlegen
    $text_color = imagecolorallocate($dst_img, 1, 1, 1);        // Textfarbe festlegen
    imagefill($dst_img, 0, 0, $bg_color);                       // Hintergrundfarbe malen
    imagerectangle ($dst_img, 0, 0, $total_width-1, $total_height-1, $rand_color);  // Rand malen
    imagerectangle ($dst_img, 1, 1, $total_width-2, $total_height-2, $rand_color);  // Rand malen (zweites Pixel)
    $rp = realpath('');
    $txtfont = "$rp/Roboto/Roboto-Regular.ttf";
    
    $li1 = 20;
    $li2 = 440;
    $ob = 20;
    $abst = 24;
    $format = '&#x%X%X;';
    imagettftext($dst_img, 12, 0, $li1, 24, $text_color, $txtfont, 'chr()');
    imagettftext($dst_img, 12, 0, $li2, 24, $text_color, $txtfont, 'numeric character references');
    for ($i = 2; $i < 0x10; $i++) {
        for ($j = 0; $j < 16; $j++) {
             imagettftext($dst_img, 12, 0, $li1+$j*$abst, $ob+$i*$abst, $text_color, $txtfont, chr(16*$i + $j));
             $z = sprintf($format, $i, $j);
             imagettftext($dst_img, 12, 0, $li2+$j*$abst, $ob+$i*$abst, $text_color, $txtfont, $z);   
        }
    }
    
    imagejpeg($dst_img);  // Bild liefern
    imagedestroy($dst_img); // Speicher freigeben
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search