Advertisement
saka

Untitled

Nov 19th, 2011
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.39 KB | None | 0 0
  1. <?php
  2. @chdir(dirname(__FILE__)); // corrects environment path (ignore this)
  3.  
  4.  
  5. $username = "KHRFanclub";
  6. $background = "background.png";
  7.  
  8. $font = "OratorStd.otf";
  9. $fontsize = 8;
  10. $fontcolor = '863816'; // any html-style color
  11. $fontx = 5; // text x coord
  12. $fonty = 25; // text y coord
  13. $fontalign = 'l'; // alignment.... 'l' = left, 'r' = right, 'c' = center
  14. $maxlength = 415; // maximum length of message, in pixels
  15.  
  16. $numtweets = 8;
  17. $lineheight = 5 + $fontsize; // the increment for each line, in pixels
  18.  
  19. $timeformat = 'M. jS';
  20.  
  21. $cachedpath = "cache/cache.png"; // where the cache image will be saved
  22. check_cache(5); // cache time in minutes (twitter caches for 1 minute, so no less than that)
  23.  
  24. /////////////////////////////////////////////
  25.  
  26. // json is faster to parse than xml
  27. $json = download("http://twitter.com/statuses/user_timeline/$username.json?count=$numtweets");
  28. $data = @json_decode($json);
  29. if(isset($data->error)) {
  30.     if($data->error == "Not found") $err = "Twitter user '$username' does not exist.";
  31.     else $err = "Twitter returned error: ".$data->error;
  32. } else if(!$data) $err = ("Could not find any tweets in feed for user '$username'");
  33. if ($err) {
  34.     touch($cachedpath); // wait for cache time if an error occured
  35.     die($err);
  36. }
  37.  
  38.  
  39. $im = open_image($background);
  40. $c = html2rgb($fontcolor); $c = imagecolorallocate($im,$c['red'],$c['green'],$c['blue']);
  41.  
  42. for($i=0; $i < $numtweets; $i++) {
  43.     if( !isset($data[$i]) ) break; // ignore if not enough tweets in feed
  44.     $tweet = $data[$i]->{'text'};
  45.     $tweet = preg_replace('/\s+/',' ',$tweet);
  46.     $date = date($timeformat,strtotime($data[$i]->{'created_at'}));
  47.     $string = textlimitpx($date.' - '.$tweet,$maxlength,$font,$fontsize);
  48.     imagettftextalign($im,$fontsize,0,$fontx,$fonty+$i*($lineheight),$c,$font,$string,$fontalign);
  49. }
  50.  
  51. header('Content-type: image/png');
  52. imagepng($im);
  53.  
  54.  
  55. /////////////////////////////////////////////
  56.  
  57. // open_image($path) will load an image into memory so we can work with it, and die with an error if we fail
  58. function open_image($path) {
  59.     $image = @imagecreatefromstring(file_get_contents($path));
  60.     if (!$image) die("could not open image ($path) make sure it exists");
  61.     imagealphablending($image,true); imagesavealpha($image,true); // preserve transparency
  62.     return $image;
  63. }
  64.  
  65. // imagettftextalign() is basically a wrapper for imagettftext() to add the ability to center/right-align text to a point
  66. // the $align argument can be 'c' for center, 'r' for right align, or 'l' for left align (default)
  67. function imagettftextalign(&$img,$size,$angle,$x,$y,&$c,$font,$string,$align='l') {
  68.     $box = imagettfbbox($size,$angle,$font,$string);
  69.     $w = $box[2] - $box[0];
  70.     $h = $box[3] - $box[1];
  71.     switch (strtolower($align)) {
  72.         case 'r': $x -= $w; $y -= $h; break;
  73.         case 'c': $x -= $w/2; $y -= $h/2; break;
  74.     }
  75.     imagettftext($img,$size,$angle,$x,$y,$c,$font,$string);
  76. }
  77.  
  78. // html2rgb() converts html-style colors to rgb array as return
  79. function html2rgb($color) {
  80.     if ($color[0] == '#') $color = substr($color, 1);
  81.     if (strlen($color) == 6) list($r, $g, $b) = array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
  82.     elseif (strlen($color) == 3) list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
  83.     else return false;
  84.    
  85.     return array('red'=>hexdec($r),'green'=>hexdec($g),'blue'=>hexdec($b));
  86. }
  87.  
  88. // textlimit($string, $length) takes any $string you pass it and resturns it shortened to $length characters (use it to limit title length)
  89. function textlimit($string, $length=15) {
  90.     return ( strlen(trim($string)) > $length ? trim( substr(trim($string),0,$length-3) )."..." : $string );
  91. }
  92.  
  93. // textlimitpx($string, $pixels, $font, $size) returns the shortened $string that fits within exactly $pixels width horizontally when using $font and $size
  94. function textlimitpx($string, $pixels, $font, $size) {
  95.     for($k = strlen(trim($string)); $k > 0; $k--) { if (textwidth(textlimit($string,$k), $font, $size) <= $pixels) break;   }
  96.     return textlimit($string,$k);
  97. }
  98.  
  99. // textwidth($string, $font, $size) returns the pixel width of the final text with those parameters
  100. function textwidth($string, $font, $size) {
  101.     $box = imagettfbbox($size,0,$font,$string);
  102.     return $box[2] - $box[0];
  103. }
  104.  
  105. // download() downloads the content at $url and returns the raw string, not including the http header
  106. function download($url) {
  107.     if(function_exists('curl_init')) {
  108.         $ch = curl_init($url);
  109.         curl_setopt($ch,CURLOPT_HEADER,false);
  110.         curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
  111.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  112.         $results = curl_exec($ch); // download!
  113.         curl_close($ch);
  114.     }
  115.     if (!$results) $results = file_get_contents($url); // curl failed, try url_fopen
  116.     if (!$results) die("Could not download from $url"); // give up
  117.     return $results;
  118. }
  119.  
  120. // check_cache($minutes) returns a cached image and stops execution if $minutes has not passed since the last update
  121. function check_cache($minutes) {
  122.     global $cachedpath;
  123.     if ( !( is_writable($cachedpath) or is_writable(dirname($cachedpath)) and !file_exists($cachedpath) ) )
  124.         die("The cache is not writable; please change it to 777 permissions using FTP.\n<br />\$cachedpath = {$cachedpath}");
  125.     if ( time() - @filemtime($cachedpath) < 60*$minutes and @filemtime(basename($_SERVER['SCRIPT_NAME'])) < @filemtime($cachedpath) and !$GET['debug']) {
  126.         header("Content-type: image/png");
  127.         echo file_get_contents($cachedpath);
  128.         exit(0);
  129.     }
  130. }
  131.  
  132. ?>
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement