php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50194 imagettftext broken on transparent background w/o alphablending
Submitted: 2009-11-16 20:55 UTC Modified: 2016-09-14 13:26 UTC
Votes:25
Avg. Score:4.7 ± 0.7
Reproduced:24 of 25 (96.0%)
Same Version:3 (12.5%)
Same OS:4 (16.7%)
From: stanislav at ww9 dot ru Assigned: cmb
Status: Closed Package: GD related
PHP Version: 5.2.11 OS: Windows
Private report: No CVE-ID:
 [2009-11-16 20:55 UTC] stanislav at ww9 dot ru
Description:
------------
Just wanted to write a text on an image with tranperent image and got 
strange result. Writing text on an image using some true type fonts like this one http://ww9.ru/zeferinotwo.ttf causes letters to overlap each other like here http://ww9.ru/test.php

Reproduce code:
---------------
// Set the content-type
header('Content-type: image/png');

// Create the image
//$im = imagecreatefromjpeg('./user/images/tpls/headbg.jpg');
$im = imagecreatetruecolor(400, 100);

// Create some colors
$background = imagecolorallocatealpha($im, 60, 60, 60, 127);
$black = imagecolorallocate($im, 0, 0, 0);


imagealphablending($im, false);

imagefilledrectangle($im, 0, 0, 399, 99, $background);

// The text to draw
$text = 'Tesing ... ';
// Replace path by your own font path
$font = './zeferinotwo.ttf';

// Add the text
imagettftext($im, 40, 0, 10, 40, $black, $font, $text);

imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);

Expected result:
----------------
The text "Tesing ..." written on an image

Actual result:
--------------
http://ww9.ru/test.php

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-20 11:34 UTC] fa@php.net
Reproduced with PHP5.3.1RC3 on Ubuntu LTS

I tend to blame the font.
 [2009-11-20 11:35 UTC] pajoye@php.net
I have to try them :)
 [2009-11-20 11:42 UTC] fa@php.net
It works with 5.2.6-1+lenny3 from Debian.

Broken as well in 5.3.1 (php.net) on Debian lenny
 [2009-11-20 14:15 UTC] stanislav at ww9 dot ru
I don't think it's a font problem.
It draws text well when 
-the background tranparency is swiched off
-alphablending is set to true
-savealpha is set to false

Here is the modifed code:

<?php
header('Content-type: image/png');
$im = imagecreatetruecolor(400, 100);
$background = imagecolorallocatealpha($im, 60, 60, 60, 0)// no tranparency;
$black = imagecolorallocate($im, 0, 0, 0);
//imagealphablending($im, false); 
imagefilledrectangle($im, 0, 0, 399, 99, $background);
$text = 'Tesing ... ';
$font = './zeferinotwo.ttf';
imagettftext($im, 40, 0, 10, 40, $black, $font, $text);
//imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
?>
 [2009-11-20 14:19 UTC] stanislav at ww9 dot ru
Sorry, imagesavealpha has no effect on a result
 [2010-02-08 02:33 UTC] aharvey@php.net
Bug #50958 is a dupe of this, and has a couple more exemplars that might be useful for isolating the problem.
 [2010-02-08 20:21 UTC] pajoye@php.net
Tabe, can you take a look at this one too please? :)
 [2010-04-22 11:12 UTC] alvaro at demogracia dot com
I've noticed a similar issue with the good old fonts bundled in Windows XP (Arial, Verdana and specially Tahoma). Fonts look good in PHP/5.3.0 and overlap in PHP/5.3.2 (official VB6 thread-safe binaries).

Test script:

<?

$text_ansi = 'áéíóú ÁÉÍÓÚ';
$text_utf8 = utf8_encode('áéíóú ÁÉÍÓÚ');
$font = 'C:/Windows/fonts/tahoma.ttf';

$im = imagecreatetruecolor(400, 60);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 59, $white);
imagettftext($im, 15, 0, 10, 25, $black, $font, $text_ansi);
imagettftext($im, 15, 0, 10, 55, $black, $font, $text_utf8);

header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

?>

Rendering sample:

http://img408.imageshack.us/img408/5959/php530.png
http://img8.imageshack.us/img8/4013/php532.png
 [2010-04-23 10:21 UTC] rezny at pharmdata dot cz
Seems that imagefttext is affected too, resulting image looks exactly the same. Does it use the same library?
 [2010-04-29 12:46 UTC] rezny at pharmdata dot cz
There is already a solution for this problem in svn for both 5.2 and 5.3 branches 
(submited by tabe more than 5 weeks ago) - look for the file gdft.c . I suppose 
that this bug can be closed now.
 [2010-09-21 21:06 UTC] brunofgaspar at live dot com dot pt
Hi,

This is happening on PHP 5.3.3 on Linux ( CentOS 5 )

Some letters are overlapped...
 [2010-09-21 21:10 UTC] brunofgaspar at live dot com dot pt
Sorry for double post, but i forgot to include examples

Linux:
http://img836.imageshack.us/i/linuxr.png/

Windows:
http://img829.imageshack.us/i/windowsy.png/
 [2015-07-17 01:00 UTC] cmb@php.net
-Assigned To: tabe +Assigned To: cmb
 [2015-07-17 01:00 UTC] cmb@php.net
Um, not sure what's the actual issue reported in this ticket.
Unfortunately, most of the links to results, fonts, etc. in this
report and in #50958 are unavailable in the meantime.

I can reproduce overlapping (respectively insufficient horizontal
letter distance) as reported by alvaro with PHP 5.3.2, but this
has been fixed as of PHP 5.4.0, if not earlier.

However, the original supplied test script gives indeed completely
broken results due to `imagealphablending($im, false);` even on
recent versions (PHP 5.6.11 and 7.0.0beta1) and apparently not
related to the font. See <http://imgur.com/UPowpes>. I'll try to
investigate.
 [2016-09-14 13:26 UTC] cmb@php.net
-Summary: With some fonts, letters are overlapped with imagettftext +Summary: imagettftext broken on transparent background w/o alphablending -Status: Assigned +Status: Analyzed
 [2016-09-14 13:26 UTC] cmb@php.net
The problem is that the background of the character glyphs must
not be drawn, and actually, this issue has already been fixed as
of GD 2.0.26[1], but that fix never made it to PHP's bundled
libgd. I'm going to port the fix.

[1] <https://github.com/libgd/libgd/blob/GD_2_0_26/src/gdft.c#L748-L749>
 [2016-09-14 14:20 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=09eb6ed35eb5b0d164aa9fbad52dd8d873166294
Log: Fix #50194: imagettftext broken on transparent background w/o alphablending
 [2016-09-14 14:20 UTC] cmb@php.net
-Status: Analyzed +Status: Closed
 [2016-10-17 10:08 UTC] bwoebi@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=09eb6ed35eb5b0d164aa9fbad52dd8d873166294
Log: Fix #50194: imagettftext broken on transparent background w/o alphablending
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 22 07:01:53 2017 UTC