Forum Moderators: coopster

Message Too Old, No Replies

Generate image with text on alpha transparent background

Looks like crap

         

Mike521

4:06 pm on Aug 9, 2011 (gmt 0)

10+ Year Member



I need to generate an image dynamically with embedded text supplied by the user. The image must have a transparent background, so I'm using PNG as the output format.

I can generate the image with the text embedded and with a transparent background, however the text looks awful. I think it's because PHP adds the text, then anti-aliases the text (so the edges are slightly blurred with the background color). Then I make the background color transparent, which leaves the blurred edges of the text.

I can set the text color to negative which blocks the anti-aliasing, but then the text is jagged and ugly.

Has anyone found a way around this?


edit: I just tried an alternative - use imagecreatefrompng and start with a big transparent png as the source file, then add some text. This is a little better but it still looks like crap overall.

httpwebwitch

6:32 pm on Aug 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ugh, I've encountered the same problem, didn't solve it, and so now I only use PHP Imagick and GD for images where aliasing isn't a hindrance.

And I gave up using images to render text, because it always looked like crap. (besides being iffy for SEO and awkward for i18n)

Sorry, no good advice from me but I'll be watching this thread. :(

deepthi

10:02 am on Dec 8, 2011 (gmt 0)

10+ Year Member



Hi all

Please check this link for text over images,it works me

[webhostingtalk.com...]

*******************************************************
my code

<?php

/* Text to write */
$text = "I love my India";

/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent

/* Font properties */
//$draw->setFont('verdana');
$draw->setFontSize(10);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $text);

/* Create text */
$draw->annotation(0, $metrics['ascender'], $text);

/* Create image */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);

/* Save image */
file_put_contents('file.png', $image);
?>

*********************

<?php
//Creating two Imagick object
$second = new Imagick('file.png');
$first = new Imagick('un.jpg');

// Set the colorspace to the same value
$first->setImageColorspace($second->getImageColorspace() );

//Second image is put on top of the first
$first->compositeImage($second, $second->getImageCompose(), 10, 10);

//new image is saved as final.jpg
$first->writeImage('final.jpg');
?>

Thanks

rocknbil

4:37 pm on Dec 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



haha . . .that was what I was going to suggest, verify the ImageMagick binaries are installed and recompile PHP with IMagick, ImageMagick has far superior functions for advanced image automation. Also it is far more efficient and can handle larger images - GD reconstructs images as uncompressed bitmaps in memory, which can choke on large images, ImageMagick does it on disk. From what I can remember of the projects I dealt with, ImageMagick does a pretty good job and has better font support.