Image text via PHP using URL...

Soldato
Joined
28 Oct 2002
Posts
9,227
Location
Stockport / Manchester
Hi,

I've been looking without success for a way to do this, so I though I'd post here to see if anyone could help. I'm trying to find a way to insert text onto an image based on the URL, for example

http://mysite.com/image/dbmzk1 - or something similar like /?text=dbmzk1 etc, would insert dbmzk1 in a set place on the image in a set font. A bit like that flash "you got owned" thing, if you get what I mean, only not flash! I don't need it to save the image or anything, just output it. It does need to preserve the transparency of the 8bit PNG source image though.

All I've been able to find are things like watermark generators which are no good as the text is fixed.

Any ideas or links to existing solutions?

Cheers.
 
PHP:
$text = $_GET['text'];
$img = createimagefrompng("myimg.png");
$txt_colour = ImageColorAllocate ($img, 0, 0, 0);
imagestring($img, 7, 10, 10, $text, txt_colour);
imagepng($img);

Put it all in a php file, then
<img src="theimage.php?text=hellloooo" />

Untested but it should work. Look up the imagestring() function :)
 
Last edited:
That got me off to a good start thanks, though it took me a while to figure out why it wasn't working!

My current code is below. It works great, apart from the colour. It seems the colour of the text is basically transparent - its the same as whatever the background is, on these forums it would be blue for example. Using imagestring has the same effect, I just used imagettftext so it could use a TrueType font for the text.

PHP:
<?PHP
$text = $_GET['text'];;
$image = imagecreatefrompng("mypic.png");
$colour= imagecolorallocate($image, 0, 0, 0);
$font = 'myfont.ttf';
imagettftext($image, 12, 0, 20, 50, $colour, $font, $text);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

Any ideas? Using PHP 5.2.6 and GD 2.0.34 if it matters.
 
Last edited:
This seems to work for me. Here it is running on my server at home:
http://hexoc.ath.cx/alpha/imgt.php?text=testtest

I do apologise for the (silly) mistakes in the code I posted, I was in a rush this morning and typed it out without checking it!

Which font are you using? I'm using arial, not that it should matter.

Jon
I've been having a play and it seems to be an issue with the alpha transparency on the 8bit PNG I'm using. It's almost as if the image is to complex or something, as if I replace it with a simple block of colour, or simply reduce the complexity of the image then script it works fine, as you demonstrated in your example. Index transparency works as well, but it's not pretty of course! 32bit fails totally.

Here is a test page I made to help show what is going on. http://blueinferno.co.uk/bloodclawpack/sigs/blanks/

I'm lost now! Any ideas?

perhaps use a contrasting background colour to put the text on? use imagefilledrectangle to do this. :)
Ideally I need to retain the transparent background. The image is a sig as you can see above and will be used in multiple locations.
 
This honestly has me stumped now. This is the furthest I've got:
http://hexoc.ath.cx/alpha/img2.php?text=testtest
PHP:
<?PHP
header("Content-type: image/png");
$itxt = stripslashes(trim($_GET['text']));
$src_img = ImageCreateFrompng("mypic.png");
$src_w = ImageSX($src_img);
$src_h = ImageSY($src_img);

$txt_img = ImageCreateTrueColor($src_w,$src_h);

$black = ImageColorAllocate ($txt_img, 0, 0, 0);
$tcol = ImageColorAllocate ($txt_img, 0, 0, 255);

ImageTTFText ($txt_img, 30, 0, 20, 100, $tcol, "myfont.ttf", $itxt);

ImageColorTransparent($txt_img, $black);
ImageCopyMerge($src_img,$txt_img,0,0,0,0,$src_w,$src_h,100);
Imagepng($src_img);
imagedestroy($src_img);
?>

As you can see I've tried to create a black layer, put the text on, make the black transparent and then layer it over the original image.

Problems:
- Hasn't been tested on a coloured background.
- The text, whilst you can set its RGB colours, only comes up in grayscale.

I'm sure the colour/grayscale problem is something stupid I've overlooked, however.

Sorry to not be of more help, but I'm keeping an eye on this thread!
 
Not my area of expertise but wouldn't a layer use the Z axis/index to set where it sits in relation to another layer?
 
This honestly has me stumped now. This is the furthest I've got:
http://hexoc.ath.cx/alpha/img2.php?text=testtest
PHP:
<?PHP
header("Content-type: image/png");
$itxt = stripslashes(trim($_GET['text']));
$src_img = ImageCreateFrompng("mypic.png");
$src_w = ImageSX($src_img);
$src_h = ImageSY($src_img);

$txt_img = ImageCreateTrueColor($src_w,$src_h);

$black = ImageColorAllocate ($txt_img, 0, 0, 0);
$tcol = ImageColorAllocate ($txt_img, 0, 0, 255);

ImageTTFText ($txt_img, 30, 0, 20, 100, $tcol, "myfont.ttf", $itxt);

ImageColorTransparent($txt_img, $black);
ImageCopyMerge($src_img,$txt_img,0,0,0,0,$src_w,$src_h,100);
Imagepng($src_img);
imagedestroy($src_img);
?>

As you can see I've tried to create a black layer, put the text on, make the black transparent and then layer it over the original image.

Problems:
- Hasn't been tested on a coloured background.
- The text, whilst you can set its RGB colours, only comes up in grayscale.

I'm sure the colour/grayscale problem is something stupid I've overlooked, however.

Sorry to not be of more help, but I'm keeping an eye on this thread!

Works well, apart from the grey scale thing as mentioned.

http://blueinferno.co.uk/bloodclawpack/sigs/blanks/ 5th one uses your script.

Can't seem to make it colour though no matter what I do! Ideally I need it to be a coloured text with a white border, but that might be pushing it! haha.

Hmm!
 
Last edited:
Well at least it works OK with the background.

I've got a couple of things I would like this script for as well, but I really can't work out why it's doing the grayscale thing.

Hopefully I'll have some time today during college and I'll have another play around with it. Hopefully someone else will be along soon to point out that which I've overlooked!

Anyway I'll post back if I find anything useful or somehow get it working.
 
Back
Top Bottom