Closed Thread Icon

Topic awaiting preservation: Aliased text on image with GD/PHP (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12696" title="Pages that link to Topic awaiting preservation: Aliased text on image with GD/PHP (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Aliased text on image with GD/PHP <span class="small">(Page 1 of 1)</span>\

 
Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 04-16-2003 15:42

As some of you may know, I am currently working on a "text on image" PHP tutorial for the Guru's Network. I've got just about all the pieces into place, exhibit for the pesky little problem of aliased text.

The default behavior for the imagettftext() function is to anti-alias the text. According to the notes on the function page (previous link), turning off anti-aliasing can be achieved by simply making the text color negative. In other words, if your text color is defined as $color, you would reference -$color in the function.

Apparently, though, this does not work as planned. This is a very simple example, with only two colors allocated (black and white), but if other colors are allocated for the image things get really funky--the text might decide to incorporate other colors, or it may come out looking like a very jaggy outline of the text. Suffice it to say, though, that turning off anti-aliasing does not appear to work even without having to worry about other colors.

On a hunch, I took a screen cap of the above example and pulled it up in PhotoShop. Then I filled all the non-white pixels in the anti-aliased text with black and compared it with the "aliased" text--it was a perfect match, down to the last pixel (try it for yourself if you're bored). So, in essence, what is happening is that anti-aliasing is being turned off after the text has already been anti-aliased, resulting in the chunky nightmare in the example. In other words, the function itself seems to be buggy.

Emps has done some searching around, and it appears that we're not the only ones asking questions about this. We have yet to find a satisfactory answer, though. That may be, of course, because there is no satisfactory answer, but we have some of the best and brightest minds on the Net gathered here, and it never hurts to ask.

For your reading pleasure, the source code for the images (the only difference between the anti-aliased image and the aliased image is that there is a minus sign in front of "$black" in the imagettftext() function for the aliased version, as explained above):

code:
<?php
// set image variables
$img = imagecreate(100,50);
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);

// set text variables
$font_file = "(absolute path)/verdana.ttf";
$font_size = 12;
$angle = 0;
$horz_pos = 20;
$vert_pos = 25;
$text = "Testing";

// draw button image
imagerectangle($img,0,0,99,49,$black);

// write text on image
imagettftext($img, $font_size, $angle, $horz_pos, $vert_pos, $black, $font_file, $text);

// display image
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
?>






www.liminality.org

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 04-16-2003 15:48

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 04-17-2003 01:36

Yes, that does sound very reasonable... I'll go give that a shot.

[Edit: *sigh* Well, that may very well be the answer, but it looks like I won't be finding out any time soon--my server doesn't have GD 2.0 or later, which is required for imagecreatetruecolor() (I knew there was a reason I didn't use that from the start...). I guess I'll have to go pester Michal (no, not Michael, my host guy) and see if he'll install it on the server...]

[This message has been edited by Suho1004 (edited 04-17-2003).]

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 04-21-2003 17:12

Ok, I haven't played with GD in a very *very* long time as I ditced it in favour of another package but I'll lay down the concept and let you try and figure out how to do it.

First of all, GD will need to be able to support the creation of images with an 8-bit (256 colour) alpha channel, at least memory. This way you can create the text with a colour of your choosing on a transparent background. Then you copy the pixels from that image into the source image. Even using a limited 256 colour pallett copying an image with an 8-bit alpha channel should match the nearest colour as they are copied over. It may also help to limit the pallet of the source image before you do the copy so that it has some colours free to allocate when it merges the alpha values. I've been doing a lot of this with lingo scripting lately and it works a treat.

Although I don't know if GD has the function needed to do all of that, even then it'd be a bit of a pain to code it and it would slow things down a bit too.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 04-22-2003 05:50

I may be missing the point here (which is likely), but I think everything that's been discussed so far is moot. The root of the problem is that there is no effective way to turn off anti-aliasing in GD, and thus none of this true color image/alpha transparency stuff is going to matter in the end. If anti-aliasing is turned off, we don't have to worry about transparency, since there is no transparency issue for aliased text: either a pixel is the text color or it isn't. There is no in between.

For what it's worth, I did try this with imagecreatetruecolor() on my system (using phpdev), and the results were exactly the same with anti-aliasing turned off. As I mentioned above, it appears that GD tries to turn anti-aliasing off after having already applied it, which will obviously not work. What it needs to do is just not apply anti-aliasing in the first place, but I haven't discovered any way to do that yet. And until I do, nothing else is going to matter, because you can't "un-anti-alias" text once it has been anti-aliased.

That's it, isn't it? Or is there something that I'm missing?

(This is not to say that I don't appreciate InI and Drac's efforts. I do. Thanks guys. )

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 04-23-2003 03:04

turn off anti-aliasing?

Why would you want to do that? Just use a pixel font if you want something really crisp.

Edit:

I went to peek in the PHP manual after I first made this post and well, have you played with ImageString() yet?...



[This message has been edited by Dracusis (edited 04-23-2003).]

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 04-24-2003 04:40
quote:
Just use a pixel font if you want something really crisp.



Heh. We tried that, but it anti-aliases those too, and it seems to screw up the "u"s, don't ask me why.

As for why we want to turn off anti-aliasing, well, anti-aliasing should really be used on 12 px and higher. Once you start getting down to 10 px and lower, anti-aliasing just makes things look like crap.

quote:
have you played with ImageString() yet?



Yup. But ImageString() doesn't allow you to specify a font, and the size choices are limited to HTML sizes (1-5). It's a very imprecise way of putting text on an image, and only really useful when you don't care what the text looks like.

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 04-26-2003 17:27

Damn, that sucks.

Maybe you could shoot grumble an email. I think he's done stuff with PHP and pixel fonts before. Errmm.. hang on, check your email.

Suho1004
Maniac (V) Inmate

From: Seoul, Korea
Insane since: Apr 2002

posted posted 04-28-2003 02:05

Got it. Thanks, Drac. I'll look into that.

« BackwardsOnwards »

Show Forum Drop Down Menu