Generate graphic from text?

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Generate graphic from text?

Post by visionmaster »

To avoid the download of adress data, to be precise the evaluation, the from the database generated adresses should be displayed as a graphic.
For example the company name should be fetched from the database and automatically be converted to a graphic.

1. How do I do that? Are there any GPL scripts out there?
2. Is there any other solution avoiding the evaluation of adresses? GIF graphics will at the end enlarge the output file, which ofcourse is not so nice for the user.

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Holy whoa! You can generate Flash movies with PHP??? Visionmaster, as for your problem, may I present to you, the solution using GD. The following script will pull the data out of a mysql database and output it on a white image in black letters. I will also attempt to explain each line:

Code: Select all

<?php
//I assume you are pulling your information from a database, so you want to perform a query:
$db = mysql_connect ("localhost", "", "");
$result = mysql_query ("SELECT address FROM table WHERE /* condition */", $db);
$information = mysql_fetch_array($result);

//generate a new image, allocate space for it
$newimage = imagecreatetruecolor(100, 33); //10 and 33 is the image size in pixels

//set the background colour to white, and define what "black" means
$background = imagecolorallocate($newimage, 255, 255, 255); 
$fill = imagefill($newimage, 0, 0, $background);
$black = imagecolorallocate($newimage, 0, 0, 0);

//write your information in black letters to the image
imagestring($newimage, 5, 10, 10, $information['address'], $black); //5 is font information, 10,10 are the coordinates where the text will be placed

//create, output, and destroy the image
header("Content-type: image/png"); 
imagepng($newimage); 
imagedestroy($newimage); 
?>
Assuming you saved this script as image.php, you can access it from a regular html page using

Code: Select all

&lt;img src = "image.php"&gt;
You can even pass variables like you would to any other php script:

Code: Select all

&lt;img src = "image.php?var=value&amp;anothervar=anothervalue"&gt;
Small note, this actually outputs a png, not a gif. You can change this a little so it outputs a jpeg if that's your preference, personally, I prefer png's. This will increase the loading time of the page (obviously!) because not only does the image have to be downloaded by the end user, it also has to be created by the server. However, for what you ask, I don't see any other solution.

Hope this helps you!

~evilmonkey.
Post Reply