Hobo Script I - Using PHP to display LiveJournal user images based on referring URL, and modifying resulting image  
     
      By Brendan CM <verifex (a t) g m a i l (0) com>  
     
  Here is a little PHP script I wrote to pull the referring URL from a page, parse the possible LiveJournal username from the URL, then connect to LiveJournal and parse the user images page so I have a array of user images to play with. Pick a random one, put some text over it, and Viola!  
     
       
<?PHP
  
  //      Go to MaxMind.com to get these spiffy country-ip PHP functions.
  include("geoip.inc");
  $gi = geoip_open("\GeoIP.dat",GEOIP_STANDARD);
  
  
  $comefrom=$_SERVER['HTTP_REFERER'];  // the referring URL
  
  //      Regular expression to check referring URL to see if it has livejournal username in it.
  $pattern="/http\:\/\/www\.livejournal\.com\/users\/([a-zA-Z_]+)\//U";
  
  //      Regular expression to check userpic page on livejournal to grab all the user image urls.
  $pattern2="|<img src=\'http:\/\/www\.livejournal\.com\/userpic\/([0-9]+)\/([0-9]+)\'|U";

  //      If this person isn't from Livejournal, grab the domain name if it has www in it
  $pattern3="/http\:\/\/www\.([a-z0-9-])\./U";

  //      PHP regular expression functions, pass found data into array $users and $users2
  preg_match_all($pattern,$comefrom,$users);
  preg_match_all($pattern3,$comefrom,$users2);
  
  //      cordinates of picture, and resized cordinates of userpic grabbed.
  $thex=200; $they=200; $thumbx=200; $thumby=200;
  
  //      Actual picture that will be served to browser
  $im = imagecreatetruecolor ($thex, $they) or die ("Cannot Initialize new GD image stream");
  
  //      The text strings to add onto the image
  $textx="WELCOME"; $textz="FAN";
  
  //      Lets make sure my random numbers are really random
  function make_seed()
  {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
  }
  srand(make_seed());

  if ($users[1][0]!="") {

    //      I'm using a custom http access class, but you can use snoopy http://snoopy.sourceforge.net/
    require("HttpClient.class.php");
    $client = new HttpClient('www.livejournal.com');
    $client->setDebug(false);
    $client->setPersistCookies(true);
    $client->timeout = 20;
    $client->max_redirects = 50;
    $client->cookie_host = 'www.livejournal.com';
    $client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207');
    $client->get('http://www.livejournal.com/allpics.bml?user='.$users[1][0]);
    $picss = $client->getContent();

    //     Pull out image urls from page content
    preg_match_all($pattern2,$picss,$picurls);
    if (!$im) { die("ERROR"); }
    
    //     Pick a number between the first and last of available user images on LJ
    $randpic=rand(0,count($picurls[1])-1);
    
    //     Generate the URL of the user image based on the data parsed from the page
    $picurl="http://www.livejournal.com/userpic/".$picurls[1][$randpic]."/".$picurls[2][$randpic];
    
    //     Pull image data so we can check what it is and how big it is
    list($width, $height, $type, $attr) = getimagesize($picurl);
    
    //     Check which image type it is, and use appropriate function to create the new image
    switch ($type) {
    	case 1:	$imgar = @imagecreatefromgif($picurl);
    					break;
    	case 2: $imgar = @imagecreatefromjpeg($picurl);
    					break;
	    case 3: $imgar = @imagecreatefrompng($picurl);
    					break;
    	case 6: $imgar = @imagecreatefromwbmp($picurl);
    					break;
    	default:unset($imgar);
						break;
    }
    
    //     If image was created, resize image to take up full size of image you are creating
    if (isset($imgar)) {
      imagecopyresized($im, $imgar, 0, 0, 0, 0, $thumbx, $thumby, $width, $height);
    }

    //     Making username uppercase, for extra pizazz!
    $texty=strtoupper($users[1][0]);
  } elseif ($users2[1][0]!="") {
    //     If LJ user wasn't found, just spout domain name.
    $texty=$users2[1][0];
  } else {
    //     Looks like they didn't give me a referring URL, or they had some funky url
    $texty="MYSTERIOUS";
  }
  
  //      This function displays the country name per IP
  $textw="You from ".geoip_country_name_by_addr($gi, $_SERVER["REMOTE_ADDR"])."?";
  $red=imagecolorallocate($im,255,0,0);
  $green=imagecolorallocate($im,0,255,0);
  $purple=imagecolorallocate($im,255,0,255);
  $blue=imagecolorallocate($im,255,255,255);
  
  //      I'm using this font for everything
  $font="C:/WINDOWS/FONTS/Jokerman.ttf";
  imagettftext($im, 10, 0, 24, 182, $blue, $font, $textw);
  imagettftext($im, 15, 0, 44, 61, $red, $font, $textx);
  imagettftext($im, 15, 0, 15, 100, $green, $font, $texty);
  imagettftext($im, 15, 0, 80, 142, $purple, $font, $textz);
  
  //      Display header or else browser will get cranky!
  header("Content-type: image/jpeg");
  
  //      Finally display the darned picture!
  imagejpeg($im);
?>
       
   
     
 
Creative Commons: Some Rights Reserved Valid HTML 4.01! Valid CSS!
slogan
slogan
slogan
slogan