Jump to content

Dynamic image problem


Tuk

Recommended Posts

I'm having an issue with dynamic image creation. I know what my problem is so I'm just wondering if there's a way to make it work, or if I'm out of luck.

 

Basically I have an image and I want to display a dynamic string on it. I've looked up tutorials, etc, and eventually I got it working, except the string isn't showing, and I think it's because it's a variable. I got it to work perfectly by putting a static word as the string ('hello'), but when I changed that to a variable, the main image shows up but with no text on it.

 

I believe my problem is that the variable is obtained by using $_GET then a select query to get the information from the database for that particular user. I have the dynamic image set up in a separate file (file names have been changed, but I know those aren't the problem):

<?
$image = imagecreatefrompng("image url");
$textcolor = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
imagestring($image, 1, 10, 10, "$variable", $textcolor);

header('Content-type: image/png');
imagepng($image);
?>

 

I then call on the image with this, in the file I want the image to be displayed in...:

<img src=imagefile.php>

 

So basically, here's my problem... I need to use a select query to get the proper information for $variable, but since the dynamic image file needs to define itself as an image/png content-type, adding in any other script such as a query will cause the image to break... I can't include anything for the same reason...

 

When I was looking up how to do a dynamic image in general, the only examples I found had static strings, but I kept reading that you could use a variable instead... but now I'm trying and it's not working. :/

 

As a note, I tried doing the select query in a separate file and including it, and the image broke again.

 

Any thoughts?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/193000-dynamic-image-problem/
Share on other sites

If it works with a static string but not a variable then the variable isn't being set. A function will not stop working based on if you pass a string or a variable with the same string on it. Firstly you need to learn how to make valid HTML

<img src="imagefile.php" alt="Dynamic Image" />

Secondly you say you are passing the text via $_GET but that is not the case. If you want to pass it via $_GET you would need

imagefile.php?var_name=some_value

and then use $_GET['var_name'] in your imagefile.php script to get the data and use it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.