Jump to content


Photo

Variable Text to PNG


  • Please log in to reply
2 replies to this topic

#1 sinfield

sinfield
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 March 2006 - 10:08 PM

I am trying to assign a variable "$fullname" from database, and then output that variable text content to png. The $fullname script works if I echo it to the screen. And the text-to-png part works if I either manually declare the text to be transformed or if I manually declare the value of the $fullname variable (see commented test in code). But if I use two together to dynamically set the value of the $fullname variable, it does not work. Is there something glaring and obvious I am missing?

I know the query is not pretty. The DB schema I am working with is ... bad. But it is what I have to work with right now.

Here's my code:
I was unable to post code, so check it here:
[a href=\"http://bogobug.com/uname_to_png.txt\" target=\"_blank\"]http://bogobug.com/uname_to_png.txt[/a]

#2 sinfield

sinfield
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 17 March 2006 - 04:02 AM

Ok, I managed to work it out. The script was sending headers to the browser. Since all I need it to do is to output the png to a file, rather than to the browser, I just commented out the header declaration. All is well now.

On to further refinements!

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 17 March 2006 - 09:48 AM

If you want tp create them on-the-fly then save this bit in a separate script, say, "imagetext.php"

<?php
$fullname = $_GET['fn'];

// create an image with width 'x', height 'y'
$image = imagecreate(200, 100);

// create a background color and then set it to transparent
$background = imagecolorallocate($image, 255, 255, 255);
ImageColorTransparent($image, $background);
ImageInterlace($image, false);

// set black color for the text
$black = imagecolorallocate($image, 0, 0, 0);

// write the variable string to the image
// the top left of the text is at the position (10, 2)
imagestring($image, 4, 10, 2, $fullname, $black);

// tell the browser what we're sending it
Header('Content-type: image/png');

// output the image as a png
// imagepng($image);
imagepng($image);

// tidy up
imagedestroy($image);
?>

And in main script, to display the image

echo "<IMG src='imagetext.php?fn=$fullname'>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users