Page 1 of 1

Generate graphic from text?

Posted: Sat Jul 31, 2004 5:29 pm
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!

Posted: Sat Jul 31, 2004 5:37 pm
by feyd

Posted: Sat Jul 31, 2004 5:59 pm
by m3mn0n

Posted: Sat Jul 31, 2004 8:47 pm
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.