Jump to content

**SOLVED** Adding user input to imagestring


Recommended Posts

Hi,

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!
Link to comment
Share on other sites

Here's a sample

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

if (strpos($usertext,"\r")!==false)
    $lines = explode ("\r\n", $usertext); // win
else
    $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");
imagepng($im);
imagedestroy($im);
?>[/code]

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.