This tutorial will teach you how to create a signature with dynamic text using PHP with the GD library installed.
Preview -
This was the original -
The 'omikron' text on that was dynamically put there, so I can change it on the fly.
First you need an image to place text on. Once you have that, create a new php file with the usual tags.
<?php
?>
Now lets get the text that we will use. I am going to place whatever text I receive through a GET variable.
<?php
$str = $_GET['txt'];
?>
You might even want to secure the text that you receive, although I won't be doing that right now.
My signature is stored as 'images/dragon.png' relative to the PHP file. Let's load the image now.
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
?>
You should also select the color you want. You can't use a hexadecimal value here, you will need the appropriate amounts of red, green and blue. The format for this function is
imagecolorallocate($image, red, blue, green);$image is the variable to which we are allocating the color to.
red,
blue and
green are the appropriate amounts of red blue and green you will use. I wanted a nice brownish color, so I used
226,
220,
210. These value are out of a total of 255. If all three are 255, the color will be white and if all of those are 0, the color would be black.
So now the code is -
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
?>
Now select the font you want to use. I will use Palatino Linotype, which is located as 'fonts/pala.ttf' relative to the PHP file. Let's store this in a variable so that we can use it later. The font has to be a
TTF font.
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
$font = 'fonts/pala.ttf';
?>
You also need the X and Y coordinates where your text will be placed. You will need to play around with these for a while. I ended up with 12(X) and 86(Y).
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
$font = 'fonts/pala.ttf';
$x = 12;
$y = 86;
?>
You also need to know the font size you are going to use. 10 fits in well for me.
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
$font = 'fonts/pala.ttf';
$x = 12;
$y = 86;
$size = 10;
?>
The last thing you need is the rotation of your text. This is in an anti-clockwise direction. I am leaving mine at 0.
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
$font = 'fonts/pala.ttf';
$x = 12;
$y = 86;
$size = 10;
$rotation = 0;
?>
Now we are ready to actually add the text. I will be using the
imagettftext function for this.
imagettftext($image, $size, $rotation, $x, $y, $color, $font, $str);The parameters should be self-explanatory, since we already stored them in variables before.
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
$font = 'fonts/pala.ttf';
$x = 12;
$y = 86;
$size = 10;
$rotation = 0;
imagettftext($image, $size, $rotation, $x, $y, $color, $font, $str);
?>
Now that we are done with the image, let's send it to the client.
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
The
header('Content-Type: image/png'); is to tell the browser that we are sending an image.
imagepng($image); is to actually output the image.
imagedestroy($image); will just destroy the image since it has already been sent and isn't needed any more.
So the complete code for the image is -
<?php
$str = $_GET['txt'];
$image = imagecreatefrompng('images/dragon.png');
$color = imagecolorallocate($image, 226, 220, 210);
$font = 'fonts/pala.ttf';
$x = 12;
$y = 86;
$size = 10;
$rotation = 0;
imagettftext($image, $size, $rotation, $x, $y, $color, $font, $str);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
If you saved this on your host as
sigs/sig.php, you can access it like this:
http://YOURHOST/sigs/sig.php?txt=MyNameNow, the problem is that some forums don't allow you to add a signature image unless it ends with an image extension(PNG, GIF or JPEG). We can easily fix this using HTACCESS. Your host needs to have mod_rewrite enabled. Open your .htaccess file, or create one if it does not exist. It needs to be in the 'sigs' directory. On a new line, add -
RewriteEngine On
RewriteRule (.+)/sig.png sig.php?txt=$1
That way, you can access the image as
http://YOURHOST/sigs/MyName/sig.pngThe first line in the HTACCESS code simply enables the rewrite engine.
The second line is
RewriteRule (.+)/sig.png sig.php?txt=$1.
RewriteRule - A new URL rewriting rule.
(.+)/sig.png -
(.+) is a 'catch-all expression'. This represents what your URL will look like.
sig.php?txt=$1 - This represents what the actual file is. $1 is what was caught by the 'catch-all' expression above.
So,
http://YOURHOST/sigs/Anything/sig.png will become the same as
http://YOURHOST/sigs/sig.php?txt=Anthing