imageftbbox returns wrong position for Chinese/Japanese font

Issue #79 new
eatnut created an issue

I have an updated code that fix the vertical position problem for all fonts(maybe my old code is wrong), but it still has the horizontal shift-right problem. Now the position issue is too hard to distinguish unless the font size is large enough..I have changed the priority to trivial.

<?php
$size = 160;
define('ANGLE', 0);
$fontfile = 'simhei.ttf';
$text = '你好';
$linespacing = 1;
$bbox = imageftbbox($size, ANGLE, $fontfile, $text, array('linespacing' => $linespacing));
$width = abs($bbox[0] - $bbox[2]);
$height = abs($bbox[1] - $bbox[7]);
$x = 0; //updated code
$y = - $bbox[7] - 1; //updated code
$image = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($image, 255, 0, 0);
imagefttext($image, $size, ANGLE, $x, $y , $color, $fontfile, $text, array('linespacing' => $linespacing));
header('content-type:image/png');
imagepng($image);

?>

Comments (11)

  1. eatnut reporter
    • edited description

    here's the code:

    <?php
    $size = 160;
    define('ANGLE', 0);
    $fontfile = 'simhei.ttf';
    $text = '你好';
    $linespacing = 1;
    $bbox = imageftbbox($size, ANGLE, $fontfile, $text, array('linespacing' => $linespacing));
    $width = abs($bbox[0] - $bbox[2]);
    $height = abs($bbox[1] - $bbox[7]);
    $x = 0;
    $y = $height ;
    $image = imagecreatetruecolor($width, $height);
    $color = imagecolorallocate($image, 255, 0, 0);
    imagefttext($image, $size, ANGLE, $x, $y , $color, $fontfile, $text, array('linespacing' => $linespacing));
    header('content-type:image/png');
    imagepng($image);
    ?>
    
  2. Pierre Joye

    Can you try using system's libgd (ideally using gd 2.1) instead of the bundled GD?

    Or do you use the system GD already? You can see it in phpinfo, in the GD section.

  3. eatnut reporter

    I'm wondering can GD 2.1 be installed on windows?

    I'm using WampServer 2.4. I think the GD included in WampServer is version 2.0.

    Is my code correct (when the angle is defined as 0)?

    The font is from windows. You can find it in windows\font folder

  4. Pierre Joye

    PHP Windows uses 2.1, but with part of it with a slightly different implementation. Freetype code is one of those. It seems that there is a bug in there, assigned to me.

  5. eatnut reporter

    Maybe my old code is wrong, I have a new code can fix the vertical shift problem, but still have the horizontal issue:

    <?php
    $size = 160;
    define('ANGLE', 0);
    $fontfile = 'simhei.ttf';
    $text = '你好';
    $linespacing = 1;
    $bbox = imageftbbox($size, ANGLE, $fontfile, $text, array('linespacing' => $linespacing));
    $width = abs($bbox[0] - $bbox[2]);
    $height = abs($bbox[1] - $bbox[7]);
    $x = 0; //updated code
    $y = - $bbox[7] - 1; //updated code
    $image = imagecreatetruecolor($width, $height);
    $color = imagecolorallocate($image, 255, 0, 0);
    imagefttext($image, $size, ANGLE, $x, $y , $color, $fontfile, $text, array('linespacing' => $linespacing));
    header('content-type:image/png');
    imagepng($image);
    
    ?>
    
  6. Log in to comment