Making a dynamic text sig

Anything technology related that does not fit in other categories
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

Ever wonder how to make a sig that shows the viewer's IP or other info? This article will show you how, it's actually easier then you think, as long as you have a linux/apache host with php.

http://www.iceteks.com/articles.php?act=vi...cle=textsig&p=1

Archived topic from Iceteks, old topic ID:2012, old post ID:16573
Honk if you love Jesus, text if you want to meet Him!
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

I wanted to keep it simple so it's easier to understand so I did not include any fancy stuff. But there's a way to get it centred you need to first determine the side of the string and such. I did not try it myself though.

Chances are for the screen res you probably do need javascript but I'm sure php probably has a way of doing it too. I could be wrong though. I know there's more to the $_SERVER vars then IP, referrer and such. User agent is another bit of info that could be added but it's usually quite a long string of text. You'd want to have some if statements that check for specific things so if if says MSIE you can make it say "you use IE" and such.

Archived topic from Iceteks, old topic ID:2012, old post ID:16734
Honk if you love Jesus, text if you want to meet Him!
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

unfortunatly not, since you need that php .jpg file to act as php and not as an image. Unless there's another way you can change the mime types for that folder, you're out of luck. :( But you should consider better hosting if they don't let you access to .htaccess.

Archived topic from Iceteks, old topic ID:2012, old post ID:20917
Honk if you love Jesus, text if you want to meet Him!
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

can you give me a link to this article? Perhaps I accidentally put the wrong number in the db for the number of pages... but that's unlikely. Perhaps my conclusion just sucks and that it actually ended. :lol:

Archived topic from Iceteks, old topic ID:2012, old post ID:21193
Honk if you love Jesus, text if you want to meet Him!
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

Oh that's a different article... but that's really weird that it's doing that... Thanks for letting me know. It's acting as if it's not in the database, but it has to be since it shows up on the article list.

I'll look into it, meanwhile just manually add page 2 in the url.

P1: http://www.iceteks.com/articles.php?act=vi...cle=dynsig&p=1&
P2: http://www.iceteks.com/articles.php?act=vi...cle=dynsig&p=2&

Archived topic from Iceteks, old topic ID:2012, old post ID:21229
Honk if you love Jesus, text if you want to meet Him!
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

That was weird. In the database there was a return somewhere for some reason so it messed up. Got rid of the return and boom, it worked! I should plug in a rtrim() in there somewhere to avoid this from hapening again.

Archived topic from Iceteks, old topic ID:2012, old post ID:21231
Honk if you love Jesus, text if you want to meet Him!
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

That's great. All working now.

Thanks! :)

Archived topic from Iceteks, old topic ID:2012, old post ID:21232
Image
Anonymous

Making a dynamic text sig

Post by Anonymous »

just so u know, i';ve registered an account so i am e-mailed wen u reply know ;)

Archived topic from Iceteks, old topic ID:2012, old post ID:21344
Anonymous

Making a dynamic text sig

Post by Anonymous »

I can't edit a post, how stupid, i'll have to post again, if u tried e-mailing me it won't of worked caus my website is down, therefore reply to this post.

Archived topic from Iceteks, old topic ID:2012, old post ID:21360
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

With participation you get more priviliges such as longer edit limits so keep on participating and eventually you can edit any posts. Welcome aboard!

Archived topic from Iceteks, old topic ID:2012, old post ID:21362
Honk if you love Jesus, text if you want to meet Him!
Anonymous

Making a dynamic text sig

Post by Anonymous »

U didn't acutally answer ne of my question there btw, lol.

Archived topic from Iceteks, old topic ID:2012, old post ID:21375
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

I swear I must of answered it I remember but my post ain't there. :lol: I tend to stay up past 4:00am so maybe it was one of those moments where I just hit the X.


Anyway, most of your questions are answered in the article.

There's a variable to set the position of the text.

imagestring($im, 3, $px, $py, $string, $color);

$px and $py are the position


Variables are unlimited, you can use the function like you would use echo() so you can replace $string with anything you like, including something with more then one variable. ex: $string = $var1." some text ".$var2


It's also possible to pull info off a site but it's more complex, since you also have to parse it. So if the info you want is in a table, you need to find that exact table, then get all the data in it. It all depends on the formatting of the web page and stuff, how you do it. I never really played with that myself so I can't really help you. But you can fopen() a http url then get all the data of the web page like you would if you read a file, but parsing it is the challenge.


Archived topic from Iceteks, old topic ID:2012, old post ID:21376
Honk if you love Jesus, text if you want to meet Him!
Anonymous

Making a dynamic text sig

Post by Anonymous »

I was so hoping it wasn't that, ok i gunna have a lot of work to do on that then, but i'll see if i can do it, i neva know i mite pull it off, i'll post my achievement on here to show u how i do.

Archived topic from Iceteks, old topic ID:2012, old post ID:21379
Anonymous

Making a dynamic text sig

Post by Anonymous »

Argh i need edit, i double posting.
Neway can u see what's wrong with this code?

<?php

header("Content-type: image/png");


$number = rand(1,5);

if($number==1)$string2 = "I hope this works";
if($number==2)$string2 = "I hope this works well";
if($number==3)$string2 = "I hope this works really well";
if($number==4)$string2 = "I hope this works recon it will?";
if($number==5)$string2 = "I hope this works, i fink it mite";

if($number==1)$string4 = "This is string2";
if($number==2)$string4 = "This is string 2 again";
if($number==3)$string4 = "I wonder";
if($number==4)$string4 = "I hope it does";
if($number==5)$string4 = "i fink it will";


$im = imagecreatefrompng("http://www.dynamicsigs.larnu.co.uk/test.PNG");
$color = imagecolorallocate($im, 255, 255, 255);
$color2 = imagecolorallocate($im, 0, 0, 0);


$px=1;
$py=50;

$px2=20
$py2=120

imagestring($im, 3, $px, $py, $string, $color);
imagestring($im, 2, $px, $py, $string2, $color);
imagestring($im, 4, $px2, $py2, $string4, $color2);

imagepng($im);
imagedestroy($im);
?>
I get this error Parse error: parse error, unexpected T_VARIABLE in /home/larnuc00/public_html/dynamicsigs/test.php on line 30


Archived topic from Iceteks, old topic ID:2012, old post ID:21380
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

$px2=20
$py2=120

You forgot your semi colons.

Archived topic from Iceteks, old topic ID:2012, old post ID:21397
Honk if you love Jesus, text if you want to meet Him!
Anonymous

Making a dynamic text sig

Post by Anonymous »

Ahh yeah, tyvm, i can't test it now unfortunatly, my website is suffering a lot of down time as the server's crashed and i'm constantly going back in time on my website pages, it's really annoying!

Archived topic from Iceteks, old topic ID:2012, old post ID:21398
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

Our old host used to be like that (rcthost) so I switched to some private server host. If youc an find someone who offers private hosting it's the best way to go, and sometimes even cheaper then normal hosting. Only downside is that you don't exactly get support. I mean I can email the guy but I'm sure he does not want to start responding to everyone's questions and stuff since he's not an actual hosting company. Eventually I'd like to start my own hosting company, but I only want to do it if I can get my own servers and a cheap connection, I would not trust myself remotely managing servers since more things can go wrong.

Archived topic from Iceteks, old topic ID:2012, old post ID:21399
Honk if you love Jesus, text if you want to meet Him!
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

It's been a while since I posted here but I managed to get my dynamic sig working a while back and now I'll share the code.

Firstly, the files I have in my dynamic sig folder.

Code: Select all

dynamic_sig
|
|
+--- .htaccess
|
|
+--- index.html
|
|
+--- official_sig.png
|
|
+--- text.txt
[code]

index.html is just a redirect to my website homepage.  Nothing terribly complex or relevant there.

.htaccess is there so that the png file is treated as php.

[code]
<Files *.png>
      ForceType application/x-httpd-php
</Files>
[code]

official_sig.png is the dynamic signature image.  It goes as follows.

[code]
<?php
// tell the user's browser that it is an image
header("Content-type: image/png");

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);



//Get quotes
$lines = file('text.txt');

//Count quotes
$num_quotes = count($lines);
$num_quotes_minus1 = $num_quotes - 1;

//Choose random quote
mt_srand((double)microtime()*1000000);
$quotes_choice = mt_rand(0, $num_quotes_minus1);
$text = $lines[$quotes_choice];
$text = trim($text);

//get the length of the string
$text_length = strlen($text);

//max line length is 65


//Create image
$lines_needed = $text_length / 65;
$lines_needed = ceil($lines_needed);
$height = $lines_needed * 17;

$image = @ImageCreate(500, $height)
  	or die("Cannot Initialize new GD image stream");


//select random colors
mt_srand((double)microtime()*1000000);
$result = mt_rand(0, 5);


switch($result)
{
	case 0:
	$bg_color = imagecolorallocate($image, 0, 0, 0);//black
	$text_color = imagecolorallocate($image, 255, 255, 255);//white
	break;
	case 1:
	$bg_color = imagecolorallocate($image, 0, 0, 0);//black
	$text_color = imagecolorallocate($image, 255, 0, 0);//red
	break;
	case 2:
	$bg_color = imagecolorallocate($image, 0, 0, 0);//black
	$text_color = imagecolorallocate($image, 0, 255, 0);//green
	break;
	case 3:
	$bg_color = imagecolorallocate($image, 0, 0, 0);//black
	$text_color = imagecolorallocate($image, 0, 255, 255);//blue
	break;
	case 4:
	$bg_color = imagecolorallocate($image, 192, 192, 192);//grey
	$text_color = imagecolorallocate($image, 0, 0, 0);//black
	break;
	case 5:
	$bg_color = imagecolorallocate($image, 192, 192, 192);//grey
	$text_color = imagecolorallocate($image, 0, 0, 255);//blue
	break;
}



//apply background color
ImageFill($image, 0, 0, $bg_color); 
	

//Break string up into words
$explode_text = explode(" ", $text);


//Re-construct the exploded string into lines
$line_length = 0;
$line = 0;
foreach ($explode_text as $token) {
	$token_length = strlen($token);
	$output_text[$line] .= $token;
	$output_text[$line] .= " ";
	$line_length = $line_length + $token_length + 1;
	if ($line_length > 62){
  $line++;
  $line_length = 0;
	}
}
    
// define the font, x_position
// how much the y_position changes (increments)
$font = 3;
$x_pos = 5;
$y_inc = 5;

$line_number = 1;


//Output each line to the image
$line_number = 1;
foreach ($output_text as $print)
{
    if ($line_number == 1) {$y_inc = 1;}
    else {$y_inc = 17;};
    	
    	imagestring($image, $font, $x_pos, "$y_inc * $line_number", $print, $text_color);
    $line_number++;
};



// Display the image
imagepng($image);
imagedestroy($image);
?> 
[code]

Hopefully the notes will help but I'll go into a little bit of detail at the end.

text.txt is where I keep the text I want to display

[code]
. . . File not found. Should I fake it? (Y/N)
For any problem there is a solution that is simple, quick, and ultimately worse than the problem.
Hardware: The parts of a computer system that can be kicked.
If a train station is where the train stops, what is a work station?
I haven't lost my mind; it's backed up on tape somewhere.
Is reading in the bathroom considered Multi-Tasking?
It works! Now if only I could remember what I did...
To err is human, but to really foul things up requires a computer.
Will the information superhighway have any rest stops?
Never interrupt your opponent while he is making a mistake.
Life is like a roll of toilet paper . . . the closer you get to the end the faster it goes.
It is much easier to be critical than to be correct.
I have no problem keeping secrets. It's the people I tell...
Some people grin and bear it . . . others smile and change it.
The man who can smile when things go wrong has thought of someone else he can blame it on.
Acquaintance: a person whom we know well enough to borrow from, but not well enough to lend to.
Advice is what we ask for when we already know the answer but wish we didn't.
I couldn't repair your brakes, so I made your horn louder.
It's only funny until someone gets hurt. Then it's hilarious.
Lead me not into temptation. I can find it myself.
Never underestimate the power of stupid people in large groups.
The downside of being better than everyone else is that people tend to assume you're pretentious.
It is morally wrong to allow a sucker to keep his money.
Money can't buy you happiness, but it does bring you a more pleasant form of misery.
Many men would rather die than think.  Many do.[code]

Each quote is on a seperate line so that when I open the file it is broken up into an array.  I wanted to update the list of quotes without updating my code so I first count how many quotes I have.  Knowing that I'm able to pick one at random.  

The image is of a set width and I don't want my text running off the edge of the image so I break it up into lines.  I originally did this by just chopping it up into strings of a arbitrary length but this didn't look good as words were broken up.  Instead I broke the quote up into a series of words so I am able to reconstruct it up to a set length and then start on the next line.  This means that no words are broken up and it just looks better.

Obviously as the length of quotes changes so does the number of lines and hence the height of the image.  This is again dedided on-the-fly.  One problem with this code is that the height is not decided based on the number of lines but on the length of the quote divided by the maximum number of characters I want.  This means that occasionally you get an extra line with no text on it.  This was a throw back to the earlier version of this code that I never changed.  Bigger and better projects and all that.  B) 

Okay, so we've picked the quote, decided how many lines we want and hence how high the image wil be and then broken the quote down into lines.  Between all this though is a switch statement.  This just randomly chooses a set of colours for the text and background.  I did consider randomly selecting colours but on consideration of the possible combinations decided against it.  You could do it so you always get complimentary colours but I decided not to go that far.

After that it is a simple case of constructing the image.

All this gives the following final product.

[img]http://www.jmstreet.info/images/dynamic_sig/official_sig.png[/img]

Obviously you'll need to refresh the page a few times to see it in its full glory.  :P 

Any question and I'll be happy to answer them.  :)  

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:2012, old post ID:25581[/size][/color]
Image
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Making a dynamic text sig

Post by Red Squirrel »

That's pretty good. Probably simpler then the one in the article too. Horrible code in mine, I need to update it some day. :D

Archived topic from Iceteks, old topic ID:2012, old post ID:25583
Honk if you love Jesus, text if you want to meet Him!
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

We can always improve our code. I would like to give it a more interesting background and fix the line issue but it just isn't worth it for the effort. It boosted my confidence and allowed me to tackle more challenging problems though so it has done its job.

Archived topic from Iceteks, old topic ID:2012, old post ID:25588
Image
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

You're absolutely right. None of the quotes I use go to line 3 so I've never seen this problem before. The height of the image is handled seperately to the lines so I'm glad that part of the code is still working ok.

I'll have a play around and see what the problem is.

Archived topic from Iceteks, old topic ID:2012, old post ID:26685
Image
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

We're all still learning, even (or perhaps especially) the people who like to sound as if they know what they are doing.

The offending code is this:

Code: Select all

if ($line_number == 1) {$y_inc = 1;}
    else {$y_inc = 17;};[code]

It is in the last foreach of the code.

If the line_number variable is 1 y_inc (the vertical distance from the top of the image) is 1.  If it is anything else it is 17.  A really quite stupid error but then they usually are. :rolleyes:   So as the line number increases the vertical height remains at 17.

The correct code is this:

[code]$y_inc = 1 + (17 * ($line_number - 1));[code]

Just delete out the if statement and replace it with this.  Now however many lines you have the lines should appear exactly 17 pixels below the previous line.

An example using a longer piece of text is the following.

[img]http://www.jmstreet.info/images/dynamic_sig/long_sig.png[/img]

I hope that helped.  :) 

 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:2012, old post ID:26687[/size][/color]
Image
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

No problem, I'm glad you pointed out there was a error. I'd love to see the finished result if you come back to the site again. :)

Archived topic from Iceteks, old topic ID:2012, old post ID:26690
Image
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

A decent background is definitely an objective. I'll probably come back to this at some point and the background and matching up the height and line number are my two priorities.

You should be able to stretch a background image to certain dimensions but I'm not sure how to do that as yet.

You certainly have some interesting quotes there. :)

Archived topic from Iceteks, old topic ID:2012, old post ID:26693
Image
Streety
Posts: 182
Joined: Mon Aug 16, 2004 7:12 pm

Making a dynamic text sig

Post by Streety »

lol, that brings a whole new meaning to the online boyfriend quote as well! :D

Archived topic from Iceteks, old topic ID:2012, old post ID:26700
Image
Locked