Log in

View Full Version : PHP image text



benslayton
05-19-2007, 04:57 AM
A small script that uses php to make text based images. EX:
http://www.domain.com/test/index.php?text=MY_TEXT
would show up with a png or jpg saying "MY_TEXT".

thetestingsite
05-19-2007, 05:03 AM
Take a look at this:
http://us2.php.net/manual/en/function.imagecreate.php

Hope this helps.

benslayton
05-19-2007, 05:18 AM
Thank you!!! I edited the file a bit to work out just in case u wanted to know.

<?php
header("Content-type: image/png");
$im = @imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
$text = $_GET["text"];
imagestring($im, 1, 5, 5, $text, $text_color);
imagepng($im);
imagedestroy($im);
?>

index.php?text=texthere

benslayton
05-19-2007, 05:30 AM
actually, one more thing. If my the text is longer than the image it doesnt expand the image it just kinda runs off the edge. How ould I fix this?


<?php
header("Content-type: image/png");
$im = @imagecreate(100, 15)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 142, 72, 151);
$text_color = imagecolorallocate($im, 0, 0, 0);
$text = $_GET["text"];
imagestring($im, 3, 1, 1, $text, $text_color);
imagepng($im);
imagedestroy($im);
?>

EDIT>
Just like always i figured it out. heres my solution.

<?php
header("Content-type: image/png");
//Ben's Inserts
$text = $_GET["text"];
$str = strlen($text);
$str2 = 7;
$total = $str * $str2;
//End Bens inserts
$im = @imagecreate($total, 15)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 142, 72, 151);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 3, 0, 0, $text, $text_color);
imagepng($im);
imagedestroy($im);
?>

I seen that each character takes up 7 pixels including a space. So it takes the string from the url and multiplies it by 7. haha!