October 20, 2018, 02:49:56 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to Revolution X, where Coding meets Graphics.
 
  Home Help Search Arcade Affiliates Staff List Calendar Members Login Register  

[PHP] Simple Dynamic Signature Tutorial

Pages: [1] 2 3   Go Down
  Print  
Author Topic: [PHP] Simple Dynamic Signature Tutorial  (Read 3664 times)
Celebrus
Artificially Conscious
Administrator
Adminitrator
Offline Offline

Gender: Male
Posts: 626



View Profile WWW
Badges: (View All)
« on: May 27, 2009, 12:09:31 pm »

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.
Code: (php)
<?php

?>


Now lets get the text that we will use. I am going to place whatever text I receive through a GET variable.
Code: (php)
<?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.
Code: (php)
<?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 -
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);

?>


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.
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);
$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).
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);
$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.
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);
$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.
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);
$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.
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);
$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 -
Code: (php)
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('images/dragon.png');
$color imagecolorallocate($image226220210);
$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=MyName

Now, 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 -
Code:
RewriteEngine On
RewriteRule (.+)/sig.png sig.php?txt=$1

That way, you can access the image as
http://YOURHOST/sigs/MyName/sig.png

The 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
Report Spam   Logged

Social Buttons

Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #1 on: June 17, 2009, 05:30:01 am »

Nice Guide.
But all of that explaining for that little thing?  Afro
Report Spam   Logged






Celebrus
Artificially Conscious
Administrator
Adminitrator
Offline Offline

Gender: Male
Posts: 626



View Profile WWW
Badges: (View All)
« Reply #2 on: June 17, 2009, 05:46:18 am »

I wouldn't say it's little. It saves a lot of time in the long run. Suppose I wanted to customize that sig for you, I wouldn't have to do anything but change the URL. xP

Report Spam   Logged

Exilis
Optimistic & Creative
Global Moderator
Global Moderator
Offline Offline

Posts: 929


View Profile
Badges: (View All)
« Reply #3 on: June 17, 2009, 05:47:57 am »

It's really helpful, you can do lots of things with GD. Think of this as a starting point for a new realm of possibilities.

This is something I used it for:
http://aetus.net/forum/index.php?topic=334.0
Hey, I should do one for RX too.
Report Spam   Logged

Global Mod, (sort of) at your service.

http://twitter.com/timothyaveni

Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #4 on: June 17, 2009, 06:06:31 am »

 Shocked Nice. Didn't think it was that easy XD looks very complicated.

Ahh... Too complicated for me Sad
« Last Edit: June 28, 2009, 09:58:31 pm by mojobojo82 » Report Spam   Logged






Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #5 on: June 29, 2009, 07:19:21 pm »

Few Question,
1.
How do you save it in your host?
I got it all now but idk how to save in host  Huh? My host is SMF For Free, I bought a domain so my url is http://www.darkilscape.com
And btw what do I save it as?
I opened it put the code (below) and just saved it as .php on my Desktop.
Now what I do?


2.
This is my code, please tell me if theres anything wrong.
Code:
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('real darkilscape userbar.JPEG');
$color imagecolorallocate($image255255255);
$font 'fonts/VISITOR.FON';
$x 275.0 px;
$y 8.6 px;
$size 15 pt;
$rotation 0;
imagefontext($image$size$rotation$x$y$color$font$str);

header('Content-Type: real darkilscape userbar.JPEG');
imagepng($image);
imagedestroy($image);

?>
« Last Edit: June 29, 2009, 10:39:28 pm by mojobojo82 » Report Spam   Logged






Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #6 on: June 30, 2009, 07:24:11 am »

Sorry... But bump...
Rlly want this 2 work  Sad
Report Spam   Logged






Exilis
Optimistic & Creative
Global Moderator
Global Moderator
Offline Offline

Posts: 929


View Profile
Badges: (View All)
« Reply #7 on: June 30, 2009, 08:12:33 am »

You have to use a host that supports PHP and GD.
Report Spam   Logged

Global Mod, (sort of) at your service.

http://twitter.com/timothyaveni

Agent Moose
Administrator
Adminitrator
Offline Offline

Gender: Male
Posts: 1,470



View Profile WWW
Badges: (View All)
« Reply #8 on: June 30, 2009, 04:48:24 pm »

For what you want to do with it, you can probably use http://ripway.com.
Report Spam   Logged


Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #9 on: July 01, 2009, 12:46:50 am »

Ok cool, So my code is all good?
I'm just going to use the one SyntaxBlitz is using. http://darkilscape.hostoi.com/


And I saved the code on my desktop as DarkilscapeDynamicUserbar.PHP now i went on upload file through my CP, and uploaded it but it's named DarkilscapeDynamicUserbar.PHP.TxT... How I make it not .TxT... Coz it makes it open in a txt doc.
Note I'm using Windows Vista Home Premium
« Last Edit: July 01, 2009, 02:49:52 am by mojobojo82 » Report Spam   Logged






Celebrus
Artificially Conscious
Administrator
Adminitrator
Offline Offline

Gender: Male
Posts: 626



View Profile WWW
Badges: (View All)
« Reply #10 on: July 01, 2009, 05:02:50 am »

in notepad while saving change the file type to 'all files'.
Report Spam   Logged

Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #11 on: July 01, 2009, 05:18:11 am »

ohk thanks. Is my code all good?
Because it's all good and everything, but when i do the link it comes up with this.

PHP Error Message

Parse error: syntax error, unexpected T_STRING in /home/a6419915/public_html/DynamicDarkilscapeUserbar.PHP on line 7

Free Web Hosting

Just an image to show you Tongue


Link - http://darkilscape.hostoi.com/DynamicDarkilscapeUserbar.PHP
« Last Edit: July 01, 2009, 05:24:00 am by mojobojo82 » Report Spam   Logged






Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #12 on: July 01, 2009, 05:54:01 am »

Sorry... But... Bump...
I realllllllllllllly want it...
It's just an error in the code I think.
Report Spam   Logged






Exilis
Optimistic & Creative
Global Moderator
Global Moderator
Offline Offline

Posts: 929


View Profile
Badges: (View All)
« Reply #13 on: July 01, 2009, 07:19:30 am »

You can't split a pixel in half. You also don't need the "px" at the end.
Do this:
Code:
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('real darkilscape userbar.JPEG');
$color imagecolorallocate($image255255255);
$font 'fonts/VISITOR.FON';
$x 275;
$y 9;
$size 15 pt;
$rotation 0;
imagefontext($image$size$rotation$x$y$color$font$str);

header('Content-Type: real darkilscape userbar.JPEG');
imagepng($image);
imagedestroy($image);

?>
Report Spam   Logged

Global Mod, (sort of) at your service.

http://twitter.com/timothyaveni

Mojobojo82
MVP
MVP
Offline Offline

Gender: Male
Posts: 838


View Profile
Badges: (View All)
« Reply #14 on: July 02, 2009, 12:49:09 am »

Ahh still not working  Sad

This is code-
Code:
<?php

$str 
$_GET['txt'];
$image imagecreatefrompng('real darkilscape userbar.JPG');
$color imagecolorallocate($image255255255);
$font 'fonts/VISITOR.FON';
$x 275;
$y 9;
$size 15 pt;
$rotation 0;
imagefontext($image$size$rotation$x$y$color$font$str);

header('Content-Type: real darkilscape userbar.JPG');
imagepng($image);
imagedestroy($image);

?>

Same error aswell.

In my Pictures on my computer it says JPEG Image.
But when I uploaded to Photobucket, it said .JPG

I'm not sure :S
Report Spam   Logged






Pages: [1] 2 3   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum | Buy traffic for your forum/website

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy