Random tag line in your sig. (kind of old school - code included)

Discussion in 'OT Technology' started by Astro, Jul 19, 2003.

  1. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Back in the old days, there were random tag lines. They've kind of disappeared along with email lists and the such. So here's a graphical edition:

    [​IMG]

    (Can an IE user with v6 or v5.5 let me know if you see text under the blue)

    How to do it:

    0. First you need a PHP + GD web server (recommend PHP v4.3.x which comes with GD)
    1. Grab the PHP code below
    2. Create a tag file. Its uber-easy. Look after the PHP code. Its named tagfile.txt in the code (rename as needed).
    3. Create a background image for your tag (I did one as 450x373 with the top 25 pixels used for my banner - the code reflects this. File is named "tag.png" in the code - rename as needed)
    4. Add .htaccess file if you want to get fancy (look after the code)

    The code:
    PHP:
    header('Content-Type: image/png');                        // its a png file - let the browser know that
    $lines file('tagfile.txt');                                // grab the file data - line by line
    $file implode("\n"$lines);                            // create one big string
    $tags explode('%'$file);                                // split all the tags apart
    $tag mt_rand(0count($tags));                        // pic a random tag
    $tag trim($tags[$tag]);                                // trim out any left over spaces
    $lines explode("\n\n"$tag);                            // imagestring() doesn't like carriage returns

    $im imagecreatefrompng('tag.png');                        // image to overlay 
    $offset 25;                                        // how high is your banner?
    $white ImageColorAllocate($im255255255);            // white
    $red ImageColorAllocate($im1872424);                // red
    $black ImageColorAllocate($im000);                    // black
    $font 2;                                            // GD font 1-5 (2 works for me)
    $fontwidth ImageFontWidth($font);                        // font width
    $fontheight ImageFontHeight($font);                        // font height

    // dump the text
    $maxlength 0;                                        
    for(
    $a 0$a count($lines); $a++)                        // dump lines out to the image
        
    {
        
    imagestring($im$font0, ($fontheight*$a) + $offset$lines[$a], $black);    // dump 1 line to image
        
    if(strlen($lines[$a]) > $maxlength)                    // is this the longest line?
            
    $maxlength strlen($lines[$a]);                    // max line length found
        
    }  // end for($a = 0; $a < count($lines); $a++)

    $imgwidth $fontwidth $maxlength;                        // figure out the max width
    $imgheight $fontheight count($lines);                    // figure out the max height
    $im2 imagecreate($imgwidth$imgheight $offset);            // create 2nd image sized to fit
    $white imagecolorallocatealpha($im2255255255100);    // gotta set the first color

    imagecopy($im2$im000045025);                    // makin' copies (copies the top half)
    imagecopy($im2$im025025450$imgheight);            // copy the bottom half
    imagedestroy($im);                                    // house cleaning

    imagepng($im2);                                    // write the file as a png
    imagedestroy($im2);                                    // no longer need the image

    The tag file:
    Code:
    Your tag line
    %
    Your 2nd tag line
    %
    Your 3rd tag line that 
    might have multiple lines
    - not a problem!
    %
    Tag lines can't have a percent sign or you botch things up
    %
    

    The .htaccess file (for those who are on Apache and want to kick it up a notch).
    Code:
    <Files tag.png>
    ForceType application/x-httpd-php
    </Files> 
    
    Notice "tag.png" is the name of the file. This will be whatever you want to reference your image as (ideally as .jpg, .png, or whatever other then .php). The trick is to persuade Apache/PHP to process the image file as a PHP file. When you go to do this, you WILL have to rename you PHP script as "tag.png" (or whatever you named it in the .htaccess file).

    If anyone gives this a whirl, let me know. I'd be curious. Let me know if you want to see the base PNG file I used (I can either email you the PNG or the PSD). I've also have several tag files in a format used above. If folks are interested, I can post them some where...
     
  2. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    astro I can see it I have 5.5
    a subject interesting to the ...
     
  3. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    i can see it in sp6.1 :big grin:

    cool idea. i should try it
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    astro with another cool post :cool:
     
  5. TheCowGod

    TheCowGod ushi no kami

    Joined:
    Aug 7, 2003
    Messages:
    90
    Likes Received:
    0
    Location:
    Guaynabo, PR
    very cool. i modified your code a bit to build a simple little sig for me:

    [​IMG]

    couldn't get the .htaccess trick to work, but i'm new to apache and linux, up until recently i was running win2k and iis. i assume there's some setting in the httpd.conf that i need to enable to tell it to parse directory-level .htaccess files. oh well, for now it works fine as a .php file. thanks for the code, man.
     
  6. TheCowGod

    TheCowGod ushi no kami

    Joined:
    Aug 7, 2003
    Messages:
    90
    Likes Received:
    0
    Location:
    Guaynabo, PR
    my version of the code, if anyone's interested:

    PHP:
    header('Content-Type: image/png'); 

    $sLogoFN "tcg.png"
    $sGradientFN "gradient.png"
    $borderWidth 1
    $borderSpacing 3
    $maxLength 60;      // Num chars at which to wrap truism 
    $font 2;            // GD font 1-5 (2 works for me) 

    $bDropShadow = ($_REQUEST['ds'] == '1'); 


    // ------------------- GET TRUISM FROM DB ---------------------------- 
    $dbUser "DB_USERNAME"
    $dbPass "DB_PASSWORD"

    $dbHost "localhost"
    $dbName "tcg"
    $dbTable "pgTruisms"

    $conn mysql_connect($dbHost$dbUser$dbPass) or die("ERROR: Couldn't connect to MySQL server '" $dbHost "'.<br/>Error msg: '" mysql_error() . "' (" mysql_errno() . ")<br/><br/>"); 
    mysql_select_db($dbName,$conn) or die("ERROR: Couldn't access MySQL database '" $dbName "' on server '" $dbHost "'.<br/>Error msg: '" mysql_error() . "' (" mysql_errno() . ")<br/><br/>"); 

    $dbSQL "SELECT truism FROM " $dbTable " ORDER BY RAND() LIMIT 1;"
    $rs mysql_query($dbSQL,$conn
       or die(
    "Error reading truism from database.<br>Error: " mysql_error() . " (" mysql_errno() . ")\n"); 

    if(
    mysql_num_rows($rs) > 0

       
    $truism mysql_result($rs00); 

    else 

       
    $truism "Dan sucks at programming. Be sure to convey this message to him repeteatedly, preferably accompanied by fists."
    // end if 

    $truism wordwrap($truism$maxLength"\n"1); 
    $lines explode("\n"$truism); 
    // -------------------------------------------------------------------- 



    // -------------------- CREATE IMAGE ---------------------------------- 
    $imgLogo imageCreateFromPng($sLogoFN); 
    $logoSize = array(imagesx($imgLogo), imagesy($imgLogo)); 

    $imgGradient imageCreateFromPng($sGradientFN); 
    $gradientSize = array(imagesx($imgGradient), imagesy($imgGradient)); 

    $fontWidth imageFontWidth($font); 
    $fontHeight imageFontHeight($font); 


    // create and prepare final image 
    $textWidth $fontWidth $maxLength
    $textHeight = ($fontHeight count($lines)) + $borderHeight 1
    $finalWidth $gradientSize[0]; 
    $finalHeight max($logoSize[1], $textHeight); 

    $imgFinal imageCreate($finalWidth$finalHeight); 
    $black imageColorAllocate($imgFinal000); 
    $gray imageColorAllocate($imgFinal127127127); 
    $white imageColorAllocate($imgFinal255255255); 

    // tile vertically with gradient 
    for($i 0$i $finalHeight$i += $gradientSize[1]) 

       
    imageCopy($imgFinal$imgGradient0$i00
                 
    min($gradientSize[0], $finalWidth), 
                 
    min($gradientSize[1], $finalHeight) ); 
    // end for($i = 0; $i < $finalHeight; $i += $gradientSize[1]) 

    // draw logo in top right corner 
    imageCopy($imgFinal$imgLogo, ($finalWidth $logoSize[0]), 000$logoSize[0], $logoSize[1]); 

    // draw border 
    imageRectangle($imgFinal00$finalWidth-1$finalHeight-1$black); 

    // draw the text 
    for($i 0$i count($lines); $i++) 

       if(
    $bDropShadow
       { 
          
    // draw a drop shadow 
          
    imageString($imgFinal$font
                      (
    $borderWidth $borderSpacing)+1
                      (
    $fontHeight $i)+1
                      
    $lines[$i], $gray); 
       } 
    // end if($bDropShadow) 

       // draw the real text 
       
    imageString($imgFinal$font
                   (
    $borderWidth $borderSpacing), 
                   (
    $fontHeight $i), 
                   
    $lines[$i], $black); 
    // end for($a = 0; $a < count($lines); $a++) 

    imagePng($imgFinal); 
    imageDestroy($imgFinal); 
     
    Last edited: Sep 26, 2003
  7. georgelb

    georgelb Guest

    nice code :bigthumb:

    I made one like that vipersig from danasoft.com

    [​IMG]
     
  8. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    where is the .htaccess file on apache 2 for windows? cause I cannot find it??

    and since I cannot find it, if it does not exist, can I still use this without including that?
     
  9. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,922
    Likes Received:
    11
    Location:
    Los Angeles
    LoL!! Nice man! Very nice! :bigthumb:
     
  10. Zourn

    Zourn 16-bit Ninja OT Supporter

    Joined:
    Apr 3, 2002
    Messages:
    2,354
    Likes Received:
    0
    Location:
    Texas
    the .htaccess is a directory specific setting file. you just creat the file in notepad, with wordwrap off, and save it as something.txt then open the sommand prompt, go to that directory and type "rename something.txt .htaccess"
    you can open it back up with notepad if you need to make any changes.
     
  11. georgelb

    georgelb Guest

    thanks toad :wiggle:

    And leb, you cannot see the .htaccess because the "." before it makes it hidden. If you have a file browser in your site control panel you should be able to see it there because they will have it set up to show hidden files. I would definately look and see if there is already one in place before uploading a new one...so you do not overwrite anything important. :werd:
     
  12. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    The .htaccess file is completely optional, it just allows you to pull the image up by .png instead of .php - in most cases the user won't know the image is .php... You should be able to just copy and paste the .htaccess stuff in a file file and put that file in the directory of you PHP file. You do need to enable the local .htaccess file direct in httpd.conf I believe. Someone catch me on IM and I'll look it up.

    Its nice to see this code getting some mileage...

    If anyone has questions or gets stuck, feel free to reach me.
     
  13. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    Astro whats your AIM name?
     
  14. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Its in my profile: RobSBauer
     
  15. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    what is GB server?
     

Share This Page