Anti Aliasing

Riggs333 - November 30, 2008 - 00:21
Project:Signwriter
Version:6.x-2.x-dev
Component:Code
Category:bug report
Priority:minor
Assigned:Unassigned
Status:active
Description

Not really a bug: I realized problems with missing anti aliasing effect on some fonts.

After looking at the code I added at line 704 in signwriter.module the following:

    imageantialias($im, true);

it's before
    $background = imagecolorallocate($im, $bg[0], $bg[1], $bg[2]);
    $foreground = imagecolorallocate($im, $fg[0], $fg[1], $fg[2]);
    $shadow_color = imagecolorallocate($im, $shadow_rgb[0], $shadow_rgb[1], $shadow_rgb[2]);

This worked for me.
Maybe it should be added in the next version? Or maybe there's a better way to handle it!?

#1

szy - December 3, 2008 - 17:51

In order to have good looking antialiased heading in 6th Drupal, it's enough to define
your color of the background, even if transparent.

:]

Agileware & Co. - thanks for your work! :]

Szy.

#2

Justin W Freeman - December 3, 2008 - 18:40
Status:active» postponed (maintainer needs more info)

Thanks szy!

@Riggs333 - Can you provide some URL examples where anti-aliases does not work? And details of the signwriter configuration used?

#3

szy - December 3, 2008 - 18:46

@Agileware - have a white (or any other light one) background on your page,
tickle 'transparent' and leave color value for background empty - then you'll get
ugly black dots around the letters.

Szy.

#4

dman - December 20, 2008 - 09:51

True.
The answer is indeed to set a proper background color.
BUT it would be better if PNG transparency was being antialiased correctly through the alpha channel. Even with PNG I was seeing black chunks.
imagesavealpha() may help if imageantialias() doesn't.

Another suggested approach I saw in php.net was to clear the background by filling the image with solid transparent bg color before starting to draw on it.

#5

smithn.nc - April 6, 2009 - 03:40

Subscribing. This module seems perfect for a project of mine, but only if the images are PNGs with alpha channel transparency.

#6

jide - May 13, 2009 - 01:30

I borrowed this little piece of code from textimage and it works for 6.x-1.1.
Add this just before line 927 :

    $blankpng = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m"
        ."dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg"
        ."dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN"
        ."egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ"
        ."oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA"
        ."DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";

    $im = imagecreatetruecolor($width, $height);
    $b = imagecreatefromstring(base64_decode($blankpng));
    imagealphablending($im, FALSE);
    imagesavealpha($im, TRUE);
    imagecopyresized($im, $b, 0, 0, 0, 0, $width, $height, imagesx($b), imagesy($b));

This should also work with 5.x.
Should I open a separate issue for 6.x branch ?

#7

Justin W Freeman - May 13, 2009 - 01:54
Version:5.x-1.5» 6.x-1.x-dev
Status:postponed (maintainer needs more info)» active

No, it's the same issue for both so this one will do for both versions.

#8

Deciphered - May 14, 2009 - 04:39

Hi guys,

If you're stealing code from my dear Textimage (I jest, steal away :)) you should probably take code from the latest versions.
The transparency support has been vastly improved since the code in #6.

Try the following when creating a transparent background:

  $image = imagecreatetruecolor(width, $height);
  $back = imagecolorallocatealpha($image, 0, 0, 0, 127);
  imagefill($image, 0, 0, $back);

Cheers,
Deciphered (maintainer of Textimage).

#9

dman - May 14, 2009 - 05:25

Indeed. an encoded PNG looks like a terrible idea. Yes, just filling in with transparent is what you need!

#10

jide - May 14, 2009 - 11:13

Deciphered & dman,

I am not even a good thief ;) Code from latest version seems more efficient, I'll give it a try, thanks !

#11

jide - May 14, 2009 - 12:15

I have tried to use the #8 code snippet from Deciphered, but i have unconsistent results. Preview seems fine but generated images appear with a black background and fuzzy edges. For now i will stick with the transparent pixel trick, but i'll try harder later.

#12

jide - May 14, 2009 - 12:17

btw, to work with Signwriter, code snippet should be :

$im = imagecreatetruecolor($width, $height);
$back = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $back);

#13

Cool_Goose - June 1, 2009 - 13:38

jide's version works nicely. Thanks a lot ;). That was exactly what I was looking for.

#14

h-man24 - July 10, 2009 - 15:25

Hello,

i'Ve got a question, i tried using the version 5.x-1.5 and 5.x-1.x-dev, but in both i have no anti-aliasing. I tried using the background-transparent-but-color-trick but it won't work.

Can somebody tell me, where the lines

$im = imagecreatetruecolor($width, $height);
$back = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $back);

should be placed in 5.x-1.5 to get i working?

Thanks in advanced for your help and thanks for this awesome and useful tool.

regards

Stefan

#15

Justin W Freeman - July 14, 2009 - 12:38
Version:6.x-1.x-dev» 6.x-2.x-dev

#16

jide - August 21, 2009 - 17:00

To use my little hack on the 6.x-2.0-beta1 version, add the following snippet on line 732 :

<?php
  $blankpng
= "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m"
     
."dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg"
     
."dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN"
     
."egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ"
     
."oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA"
     
."DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";

 
$image->im = imagecreatetruecolor($width, $height);
 
$b = imagecreatefromstring(base64_decode($blankpng));
 
imagealphablending($image->im, FALSE);
 
imagesavealpha($image->im, TRUE);
 
imagecopyresized($image->im, $b, 0, 0, 0, 0, $width, $height, imagesx($b), imagesy($b));
?>

#17

donquixote - August 27, 2009 - 14:23

http://us.php.net/manual/en/function.imageantialias.php says:

Using antialiased primitives with transparent background color can end with some unexpected results. The blend method uses the background color as any other colors. The lack of alpha component support does not allow an alpha based antialiasing method.

Maybe the same is true for antialiased text.

Last time when I wanted to anti-alias some GD imagery (rounded corners), I simply created the image in a higher res w/o antialiasing, and then scaled it down with a home-made alpha-aware per-pixel sampling.

It is important to do the weights correctly when scaling down.
(I could post some code, but I would have to spice it up a bit with comments - and I'm too lazy for that)

EDIT:
The hack #16 works for me.

#18

Deciphered - August 27, 2009 - 22:20

I'm not trying to drive traffic to TextImage, if anything I'm still interested in talking about a merge, but I spent a huge amount of time tweaking the anti-aliasing, transparency and rotation functionality and the code I used and provided a snippet at #8 is much more efficient than that of #16. I would urge the developers to look at the TextImage code (http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/textimage/?...) and borrow code where needed.

Cheers,
Deciphered.

#19

donquixote - August 27, 2009 - 23:10

Or alternatively, paint a transparent rectangle (imagefilledrectangle).
Of course, in combination with imagealphablending and imagesavealpha.

#20

dcasey - November 6, 2009 - 00:09

Using: signwriter 6.x-2.0-beta1

Am I missing something? I can't seem to get the jaggies to go away, even with the above hacks.

  • First try: I've checked the transparent box. → no joy
  • Second try: Transparent box checked, picked a color #00ff00 → nifty green jaggies around
  • Third try: Same as above, uploaded & added a background image with #00ff00 solid fill → same result as previous, sadness
  • Forth try: Added hack #12 on line 732 → same…
  • Fifth – seventh: first three trys with new hack → same, frustration setting in…
  • …tried hacking the hack (changing $im to $image->im) → unsuccessful. Giving up because maybe I misunderstood what this module can do. :(

Is anyone able to get this to work with levels of alpha transparency? I'm able to get the jaggies to match the background color, but I am using random images and Flash movies behind the rendered type. I need PNG-24 levels of transparency or have to go back to sIFR.

AttachmentSize
Picture 1.png 88.9 KB

#21

jide - November 7, 2009 - 16:10

If i remember well, you should NOT check the transparency checkbox when using the hack to make it work.

 
 

Drupal is a registered trademark of Dries Buytaert.