Jump to content


Photo

**SOLVED** Adding user input to imagestring


  • Please log in to reply
2 replies to this topic

#1 grounded_vertigo

grounded_vertigo
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 07 April 2006 - 10:29 AM

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!

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 08 April 2006 - 09:44 AM

Here's a sample

Save this code as "textimage.php"
<?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);
?>

Save as "sample.php" and run in browser:
<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 grounded_vertigo

grounded_vertigo
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 10 April 2006 - 08:15 AM

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


[/quote]


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

Michelle




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users