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
https://forums.phpfreaks.com/topic/6797-solved-adding-user-input-to-imagestring/
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]
[!--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...


[/quote]


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

Michelle

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.