Jump to content


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


**SOLVED** Adding user input to imagestring

Recommended Posts


Sorry if this is an obvious question, I am wanting to place text that a user inputs into an imagestring, I know how to place standard text in an imagestring, but can't work out how to call information from a form into part of the imagestring....

I hope that makes some sense and someone can help me!

Share this post

Link to post
Share on other sites
Here's a sample

Save this code as "textimage.php"
// get user text from url querystring
$usertext = $_GET['txt'];

if (strpos($usertext,"\r")!==false)
    $lines = explode ("\r\n", $usertext); // win
    $lines = explode ("\n", $usertext); // nix

$maxlen = 0;
$lineht = 20;

// get max line length in pixels
foreach($lines as $k=>$txt) {
        $len = imagefontwidth(2)*strlen($txt);
        if ($len > $maxlen) $maxlen = $len;

// create image with 20px margins
$ix = $maxlen+40;
$iy = count($lines)* $lineht + 40;
$im = imagecreate($ix, $iy);
$black = imagecolorallocate($im, 0,0,0);
$textcol = imagecolorallocate($im, 0xFF, 0xFF, 0x00);

// write text lines in the image
foreach ($lines as $k=>$txt) {
         $y = 20 + $k * $lineht;
         $x = 20;
         imagestring($im, 2, $x, $y, $txt, $textcol);

header ("content-type: image/png");

Save as "sample.php" and run in browser:
if (isset($_GET['txt'])) {
    $txt =  urlencode($_GET['txt']);
    echo "<img src='textimage.php?txt=$txt'>";
<TEXTAREA  name='txt' rows='4' cols='30'></TEXTAREA>
<INPUT TYPE='SUBMIT'  name='sub' value='Show text image'>

Share this post

Link to post
Share on other sites
[!--quoteo(post=362759:date=Apr 8 2006, 04:44 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 8 2006, 04:44 AM) [snapback]362759[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Here's a sample...


Thanks Barand, you're a star just what I was looking for!


Share this post

Link to post
Share on other sites


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.