Click to See Complete Forum and Search --> : Looking for Text-to-PNG or Text-to-GIF software.


Craig McPherson
09-16-2001, 11:53 AM
Let's say you had a list of 100 e-mail addresses, and you needed to quickly create 100 small PNG (or GIF) images, each consisting of one of the e-mail addresses in white text on a black background, preferably with a little white border.

How would you do this?

I found a PHP script that'd allow the images to be created one at a time, but not only would that be rather slow, the script isn't working for me. Any other suggestions would be appreciated.

element-x
09-16-2001, 01:05 PM
<?php
header ("Content-type: image/png");
$im = @ImageCreate (50, 100)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
ImageString ($im, 1, 5, 5, "A Simple Text String", $text_color);
ImagePng ($im);
ImageDestroy($im);
?>
<?php
header ("Content-type: image/gif");
$im = @ImageCreate (50, 100)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
ImageString ($im, 1, 5, 5, "A Simple Text String", $text_color);
ImageGIF ($im);
ImageDestroy($im);
?>

Note: GD needs to be compiled into php. Otherwise this will fail :(
or intialize the GD module via the dl(); function before using these scripts.

Oh...and with a simple loop structure, you could set it up to go through a file/db full of text, and make images and save each to a seperate file.

Edit: to save to a particular file, use
ImagePng($im,"path");
or
ImageGIF($im,"path");

[ 16 September 2001: Message edited by: JAdrock ]

[ 16 September 2001: Message edited by: JAdrock ]

Craig McPherson
09-16-2001, 01:50 PM
Thanks for the suggestion, but this isn't working for me.

This is what I have in the PHP script:


<?php
header ("Content-type: image/png");
$im = @ImageCreate (50, 100)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
ImageString ($im, 1, 5, 5, "A Simple Text String", $text_color);
ImagePng ($im);
ImageDestroy($im);
?>


In the web browser, I get a "broken image" image, and a "Document contained no data" message.

element-x
09-16-2001, 03:02 PM
Do you have the latest of [zlib, libpng, gd, php] installed?

Also, if you do, did you explicitly tell php where the libs were? I noticed that in my configuration I had to do that, otherwise it wouldn't work.

element-x
09-16-2001, 03:11 PM
Also...I do have a working sample of it, so you know that it would work. (don't know if it makes much of a difference)
http://www.vbfx.com/gd_test.php

Craig McPherson
09-16-2001, 03:46 PM
Aha. GD was the problem. I got the first PHP script I downloaded working now. Thank you very much.

If you see my post in the Programming forum, you are now entitled to claim $5.00.

(But wouldn't you think that somebody would've come up with a Text-to-PNG program by now that doesn't require a web server, a web browser, and several obscure libraries to use? Just a thought.)

element-x
09-16-2001, 04:05 PM
Yea, you would think that by some order or progress that this type of thing would have been created, but I haven't seen anything like it, around.

I'll have a good search around freshmeat, and if all else fails, maybe I'll cook up some command-line converter via c or perl.

element-x
09-16-2001, 04:46 PM
<?php
$filepath = "/home/user/email-list.txt";
$output_dir = "/home/user/email-images/";

if (file_exists($filepath)) {
$fd = fopen ($filepath, "r");
while (!feof ($fd)) {
$buffer = fgets($fd, 4096);
$buffer = str_replace("\n",'',$buffer);
$buffer = str_replace("\r",'',$buffer);
$im = @ImageCreate (200, 28) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
ImageString ($im, 2, 5, 5, $buffer, $text_color);
if (is_dir($output_dir))
ImagePng ($im,$output_dir . addslashes(substr("$buffer", 0, 8)) . ".png");
else
$create_dir = @mkdir($output_dir, 0777);
ImagePng ($im,$output_dir . addslashes(substr("$buffer", 0, 8)) . ".png");

ImageDestroy($im);
}
fclose ($fd);
}
?>


This will not work if you don't have permissions to make the output directory. Otherwise it creates 8-character filenames for each of your email addresses. (listed one per line in the email-list.txt)

Pardon the crudeness of the coding, but it works nonetheless.

[ 16 September 2001: Message edited by: JAdrock ]

Craig McPherson
09-17-2001, 01:22 AM
Okay, let's say I have a web page that needs to have 10 of these little generated PNGs, and I'm using the PHP script I posted in the Programming forum. I could either generate the 10 PNGs, save them on the webserver, and then img src them as static images, or I could img src the script itself which would have the images generated during every page view. That would make it a lot easier to change the images later, because I could change font size and stuff without re-creating and saving the images, but I wonder about overhead. On a PIII/866MHz system, how much would it slow down the page load to have that PHP script run ten times while the page is loading?

element-x
09-17-2001, 08:19 AM
Depends on how heavy the traffic is, for an average home users web site, which gets 1000 views a day, I would say that the type of script you're talking about would be able to handle it, you'd just have to make sure to destroy the image (via ImageDestroy()) as to not lock up your machine due to memory issues.

I can't imagine the overhead being that much of an imposition unless you're into the 10's of thousands of views per day. Especially if the machine is dedicated to services such as your web-server.