Page 1 of 2

imagettftext non-antialised

Posted: Mon Jan 16, 2006 1:40 am
by plavko
http://bugs.php.net/bug.php?id=35808
http://phpbuilder.com/board/showthread.php?t=10206621

Can somebody help to solve the problem?
I need desperately a solution?
I read that some of older beta installs had grid-fitting (hinting) enabled, dose somebody now the right vision?

Posted: Mon Jan 16, 2006 3:31 am
by onion2k
Interesting bug.

I can't think of a proper fix. However.. I've come up with this:

Code: Select all

<?php

	$width = 100;
	$height = 30;
	$back_r=255; $back_g=255; $back_b=255;
	$fore_r=0; $fore_g=0; $fore_b=0;
	$ttf_font = 'AIRSTREA.TTF';
	$text = 'test';
	
	$image = ImageCreate($width,$height);
	$back_color = ImageColorAllocate($image, $back_r, $back_g, $back_b);
	$fore_color = ImageColorAllocate($image, $fore_r, $fore_g, $fore_b);
	ImageTtfText($image ,40 ,0 ,23 ,23 ,$fore_color, $ttf_font, $text);

	for ($color=0;$color<imagecolorstotal($image);$color++) {
		$c = imagecolorsforindex($image, $color);
		$bdiff = abs($c['red']-$back_r)+abs($c['green']-$back_g)+abs($c['blue']-$back_b);
		$fdiff = abs($c['red']-$fore_r)+abs($c['green']-$fore_g)+abs($c['blue']-$fore_b);
		if ($bdiff>$fdiff) {
			imagecolorset($image,$color,$fore_r,$fore_g,$fore_b);
		} else {
			imagecolorset($image,$color,$back_r,$back_g,$back_b);
		}
	}
	
	header("Content-type: image/png");
	ImagePNG($image);
	ImageDestroy($image);

?>
It loops through colors in the palette of the image, works out if they're closer to the background or foreground colour, and sets them to whichever is nearer. It'll still leave you with more than 2 entries in the palette, but there'll only be two colors.

If you really need to have two colors in the palette then you'll need to loop through every pixel in the image..

Code: Select all

<?php

	$width = 100;
	$height = 30;
	$back_r=255; $back_g=255; $back_b=255;
	$fore_r=0; $fore_g=0; $fore_b=0;
	$ttf_font = 'AIRSTREA.TTF';
	$text = 'test';
	
	$temp = ImageCreate($width,$height);
	$back_color = ImageColorAllocate($temp, $back_r, $back_g, $back_b);
	$fore_color = ImageColorAllocate($temp, $fore_r, $fore_g, $fore_b);
	ImageTtfText($temp ,40 ,0 ,23 ,23 ,$fore_color, $ttf_font, $text);

	$image = ImageCreate($width,$height);
	$back_color = ImageColorAllocate($image, $back_r, $back_g, $back_b);
	$fore_color = ImageColorAllocate($image, $fore_r, $fore_g, $fore_b);

	for ($y=0;$y<$height;$y++) {
		for ($x=0;$x<$width;$x++) {
			$c = imagecolorsforindex($temp, imagecolorat($temp, $x, $y));			
			$bdiff = abs($c['red']-$back_r)+abs($c['green']-$back_g)+abs($c['blue']-$back_b);
			$fdiff = abs($c['red']-$fore_r)+abs($c['green']-$fore_g)+abs($c['blue']-$fore_b);
			if ($bdiff>$fdiff) {
				imagesetpixel($image,$x,$y,$fore_color);
			} else {
				imagesetpixel($image,$x,$y,$back_color);
			}
		}
	}
	
	header("Content-type: image/png");
	ImagePNG($image);
	ImageDestroy($image);
	ImageDestroy($temp);

?>
I don't think this is a very good solution .. the first version does a total of 9 iterations, this one does 3000. Bit of a leap just to remove a few palette entries.

Posted: Mon Jan 16, 2006 4:01 am
by redmonkey
Rolling back to an older beta version seems a potentially unsecure solution to the problem to me.

Presumably if you are in the position to install and different version, you are in the position to compile PHP/GD?

Hinting is enabled/disabled within the Freetype library, it's disabled by default (I believe it has something to do with potential US patent infringement within a commercial environment).

You need to compile (or find) a Freetype library with hinting enabled prior to building PHP with GD support. If your PHP version is loading GD as an external module/extension then you only really need to rebuild that module/extension.

To enable hinting within the library, locate the file 'include/freetype/config/ftoption.h' from the Freetype source then from within that file find and uncomment the line (line 439 the last time I compiled it)....

Code: Select all

#define TT_CONFIG_OPTION_BYTECODE_INTERPRETER
... then build as normal.

For what it's worth, I get this......

With hinting disabled....
Image

With hinting enabled....
Image

Posted: Mon Jan 16, 2006 1:28 pm
by plavko
Thank you for jour time onion2k and redmonkey.

onion2k:
I have tested your sample code and it produces the same results as my sample.
The problem is smaller font size as jour sample, about 10.

Redmonkey:
The real difference should be at smaller font sizes.
I must admit that I don’t have enough knowledge of C++ to build the freetype/php_gd.
Until now I have used the windows zip package to run PHP on IIS / winXP.
Is it possible that you build me the php_gd2.dll for testing, I don’t know if it’s worth it to learn (pm5884 at email dot si).Thanks in advance :roll:

In the mine time I have tested one of my oven ideas. I have made 10 times bigger image and have it resized to the right size. I tough the effect of rough would be smaller, but then the other details are messed up.
:cry:

Posted: Tue Jan 17, 2006 12:06 am
by redmonkey
plavko wrote:Is it possible that you build me the php_gd2.dll for testing, I don’t know if it’s worth it to learn
Unfortunately not. I currently do not have access to a Windows build environment, my Windows build box is in the office (which is 3500miles away) and I currently have no plans to be there any time soon. Also, I don't run Windows servers and have not built a native Win32 version of PHP for sometime so I would need to essentially setup the environment from scratch which is not exactly a trivial task in Windows and TBH not something I'm likely to do unless I needed it personally.

Is it worth your time to learn? Depends on how big a problem it is to you and also if you are likely to run into other problems/situations that may require custom builds.

Posted: Tue Jan 17, 2006 2:59 am
by plavko
redmonkey:

Can I ask you to generate me this sample with hard to hinting characters (better wood be CJK characters), so I can decide how match time I put into building GD and if it worth the afford.

Code: Select all

<?php
$font_size         = 10.6;
$font_file         = "arial.ttf";
$text              = "&#1692;&#1709;&#1769;&#64383;&#64410;&#64408;&#64406;&#64407;@¥Ç¼½¾&#1181;&#381;pjg";

$size_get_Y       = imagettfbbox($font_size, 0, $font_file,$text);
$ysize            = abs($size_get_Y[5]) + abs($size_get_Y[1]);


$size             = imagettfbbox($font_size, 0, $font_file, $text);
$xsize            = abs($size[0]) + abs($size[2]) + 1;

$image            = imagecreate($xsize,$ysize);
$background_color = imageColorAllocate($image,255,255,255);
$black            = ImageColorAllocate($image, 0, 0,0);
imageTTFtext($image, $font_size, 0, abs($size[0]), abs($size_get_Y[5]), -$black, $font_file, $text);

Header("Content-type: image/png");
imagePNG($image);
imagePNG($image,"./sample.png");
imageDestroy($image);
?>

Posted: Tue Jan 17, 2006 3:13 am
by redmonkey
Yep, that I can do.....

With hinting enabled....
Image

With hinting disabled....
Image

HTH

Posted: Tue Jan 17, 2006 4:23 am
by plavko
Thank you.
The results are good , as i thoth. :)

Posted: Tue Jan 17, 2006 8:40 am
by plavko
Redmonkey:
I have downloaded the freetype source and managed to build the freetype2110ST_D.lib with the header enabled.
I don’t know how to include the freetype2110ST_D.lib GD source and make the php_gd2.dll. Can you give me some directions?

Posted: Tue Jan 17, 2006 11:24 am
by redmonkey
Which compiler are you using? Do you have any experience in compiling apps previously?

You are going to need the PHP source for which ever version you are running as GD needs to built against PHP's header files. Note: to build a native Windows version you will also require the wintools (I think that's what it's called from memory available from the PHP site somewhere) and there may be a few others too, exact specifics escape me.

You will also probably require a few more libraries....

libjpeg source ( ftp://ftp.uu.net/graphics/jpeg/ ) if you want jpeg support in GD

zlib ( http://www.gzip.org/zlib/ ) & libpng ( http://www.libpng.org/pub/png/libpng.html ) if you want png support in GD ( libpng requires zlib so zlib needs compiling first)

I recommend compiling GD using the bundled source from the PHP sources so there is no need to download and compile the GD sources from the GD site.

Compiling PHP for Windows is notoriously annoying, I'm not sure how much I can help as I don't have my build box here for reference and as I mentioned it's been some so it's not exactly fresh in my head.

Posted: Wed Jan 18, 2006 3:35 am
by plavko
I have Visual C++ 6 installed.
My experience is none + 1 day looking at freetype source.

I have looked at the build notes, I don’t understand how from PHP source to php_GD2.DLL???

http://www.php.net/manual/en/install.wi ... ilding.php

Must be build the freetype LIB and placed to the \win32build\lib\ and than somehow build the php_gd2.dll???

Posted: Wed Jan 18, 2006 3:10 pm
by redmonkey
Here are the *rough* steps involved, note this is from memory so I may well miss a few critical steps....

Assuming you have already been through the 'setting up your environment' as outlined in the build notes....

You will need the sources for the following.. zlib, libjpeg, libpng and freetype (note download v2.1.9 of freetype as v2.1.10 does not include autohint.c)

If you are using winzip to extract the tar.gz files make sure that smart CR\LF option is checked.

You need to build *Release* versions of each of the above (zlib will be included in the libpng build so you don't need to build that seperately).

For libjpeg, you need to rename jconfig.vc to jconfig.h there is no .dsw file for libjpeg so you need to run nmake. It will also most likely complain about libjpeg.lib being missing so create a blank file and sabe it as libjpeg.lib in the source directory. Then run....

Code: Select all

nmake /f makefile.vc nodebug=1 all
This should overwrite your blank libjpeg.lib with an actual libjpeg.lib

For libpng, open the workspace file (projects/visualc6/libpng.dsw) you need to set it to build a release version (Build->Set Active Configuration) then choose (Build->Rebuild All).

For Freetype is pretty much the same thing (remember to change the config file to enable hinting). You need to rename the freetype lib (it should be called freetype219.lib) to freetype2.lib

You can either then add each directory where the .lib files have been created to your library settings within Visual Studio or (and probably safer) copy each of the .lib file over to win32build/lib/

You will also need to add the paths of the individual source packages include directories to the include directory list in Visual Studio.

for GD, open the gd.dsw file which is contained within the source distribution of your PHP version (php-x.x.x/etx/gd/gd.dsw). You need to edit the libgd/gdft.c file. Find the line .....

Code: Select all

#include "freetype/freetype.h"
And add a line above it which should be...

Code: Select all

#include "ft2build.h"
Next, from the menu choose Project->Settings then choose the link tab, in the field labeld 'Ignore libraries' type 'libc.lib' (without the quotes)

Now, locate your php4ts.lib from within your *binary distribution* of PHP and put it in win32build/lib

Set the active configuration to release and choose Build->Rebuild All

You should now have a php_gd2.dll which you can use.

Alternatively, if you can tell me which version of PHP you are using I could probably VNC into my build box in the office and attempt to build you one.

Posted: Thu Jan 19, 2006 6:15 am
by plavko
Thank you redmonkey .
I have successfully built the php_gd2.dll, thanks to your description.
Everything is working now.
I learned quit a load.


Thanks, thanks, thanks
:D :D :D

Posted: Thu Jan 19, 2006 6:18 am
by redmonkey
So I decided to put my memory to the test, it's not too bad as it goes. Some of the details in my previous post are some what dated, and the libjpeg details are wrong.

So, it turns out that newer sources of PHP don't require editing of gdft.c

libjpeg, you need to edit the makefile.vc, find the line....

Code: Select all

$(cc) $(CFLAGS) $*.c
...and change to....

Code: Select all

$(cc) $(CFLAGS) /MD $*.c
And running nmake.....

Code: Select all

nmake /f makefile.vc nodebug=1 libjpeg.lib
You also don't need to ignore libc.lib either, although it won't be searched for anyway but you should remove it from the 'Ignore Libraries' options.

Anyway, to safe some potential headaches, I built a couple of php_gd2.dlls which (for a limited time only) you can pick up from ftp://ftp.redmonkey.org/pub/distrib/php ... dle.tar.gz

The archive contains 3 php-4.3.x_gd2.dlls all for different flavours of PHP-4.3.x the archive also contains a small readme file with a few more details. If you're having problems getting a working .dll feel free to give these a go. Let me know if you have any problems.

Posted: Thu Jan 19, 2006 6:19 am
by redmonkey
Doh!