Help - Search - Members - Calendar
Full Version: Dynamic Signatures
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Gibbons
Dynamic Signatures

I would like someone to point me in the right direction to a tutorial on how to create a dynamic signature. The signature must include text from an input box that a user has entered.

An example of what I mean can be found here: http://www.asmallorange.com/extras/sig/

Thanks in advance.
Gibbons.
Alex
You'll probably want to look into PHP's GD image library functions.

http://php.net/gd

In particular in this case, the imagecreatefrom* and imagettftext functions.
Ed
Quite simple really, if you're aware of how to use GD then it'll be a total breeze I'll start off assuming you don't though.

First you need to create an image canvas using gd and load your image:

CODE
$canvas = imagecreatefrompng('canvas_template.png');


Next you have to assign the text colour (can use integers as opposed to hexadecimal):

CODE
$red = imagecolorallocate($canvas, 0xFF, 0x00, 0x00);


Now the placing the text, for the sake of clarity variables assigned for each argument:
CODE
$font_size  = 15;
$angle      = 0;
$x_coord    = 125;
$y_coord    = 110;
$font = 'FreeSansBold.ttf';
$text = 'dynamic text';

imagettftext($canvas, $font_size, $angle, $x_coord, $y_coord, $red, $font, $text);


Then to output:
CODE
header('Content-type: image/png');
imagepng($canvas);


Destroy the image resource and free up memory:
CODE
imagedestroy($canvas);


For source, font and example, see here:
http://bread.zymic.com/examples/procedual/...mic_image_text/

Links to functions used:
http://www.php.net/imagecreatefrompng
http://www.php.net/imagecolorallocate
http://www.php.net/imagettftext
http://www.php.net/imagepng
http://www.php.net/destroyimage
http://www.php.net/header
Gibbons
Thanks very much Alex and Bread. One thing im still stuck on, how would you link this to a form? The idea is so people can enter text and then click submit, and that text goes into the image.

Hope that makes sense.
Gibbons. rolleyes.gif
Alex
Erm... $_POST variables? If you have a input like this in your form:

CODE
<input type="text" name="example">

Then you submit the form to your script, then $_POST['example'] will contain whatever was typed into that input. Then, you do what you want with the input data, and then throw it into imgttftext (in the same place Bread's $text variable is).
Gibbons
Thanks Alex, i'll try and get it to work.
Ed
Probably better to use a GET method, then they can copy and paste the link.
Alex
That depends on how much information is going in, for low-mid inputs I'd agree with you, however if they're allowed a phrase or something then get would produce an unwieldy URL that would probably annoy people.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.