Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- @chdir(dirname(__FILE__)); // corrects environment path (ignore this)
- $username = "KHRFanclub";
- $background = "background.png";
- $font = "OratorStd.otf";
- $fontsize = 8;
- $fontcolor = '863816'; // any html-style color
- $fontx = 5; // text x coord
- $fonty = 25; // text y coord
- $fontalign = 'l'; // alignment.... 'l' = left, 'r' = right, 'c' = center
- $maxlength = 415; // maximum length of message, in pixels
- $numtweets = 8;
- $lineheight = 5 + $fontsize; // the increment for each line, in pixels
- $timeformat = 'M. jS';
- $cachedpath = "cache/cache.png"; // where the cache image will be saved
- check_cache(5); // cache time in minutes (twitter caches for 1 minute, so no less than that)
- /////////////////////////////////////////////
- // json is faster to parse than xml
- $json = download("http://twitter.com/statuses/user_timeline/$username.json?count=$numtweets");
- $data = @json_decode($json);
- if(isset($data->error)) {
- if($data->error == "Not found") $err = "Twitter user '$username' does not exist.";
- else $err = "Twitter returned error: ".$data->error;
- } else if(!$data) $err = ("Could not find any tweets in feed for user '$username'");
- if ($err) {
- touch($cachedpath); // wait for cache time if an error occured
- die($err);
- }
- $im = open_image($background);
- $c = html2rgb($fontcolor); $c = imagecolorallocate($im,$c['red'],$c['green'],$c['blue']);
- for($i=0; $i < $numtweets; $i++) {
- if( !isset($data[$i]) ) break; // ignore if not enough tweets in feed
- $tweet = $data[$i]->{'text'};
- $tweet = preg_replace('/\s+/',' ',$tweet);
- $date = date($timeformat,strtotime($data[$i]->{'created_at'}));
- $string = textlimitpx($date.' - '.$tweet,$maxlength,$font,$fontsize);
- imagettftextalign($im,$fontsize,0,$fontx,$fonty+$i*($lineheight),$c,$font,$string,$fontalign);
- }
- header('Content-type: image/png');
- imagepng($im);
- /////////////////////////////////////////////
- // open_image($path) will load an image into memory so we can work with it, and die with an error if we fail
- function open_image($path) {
- $image = @imagecreatefromstring(file_get_contents($path));
- if (!$image) die("could not open image ($path) make sure it exists");
- imagealphablending($image,true); imagesavealpha($image,true); // preserve transparency
- return $image;
- }
- // imagettftextalign() is basically a wrapper for imagettftext() to add the ability to center/right-align text to a point
- // the $align argument can be 'c' for center, 'r' for right align, or 'l' for left align (default)
- function imagettftextalign(&$img,$size,$angle,$x,$y,&$c,$font,$string,$align='l') {
- $box = imagettfbbox($size,$angle,$font,$string);
- $w = $box[2] - $box[0];
- $h = $box[3] - $box[1];
- switch (strtolower($align)) {
- case 'r': $x -= $w; $y -= $h; break;
- case 'c': $x -= $w/2; $y -= $h/2; break;
- }
- imagettftext($img,$size,$angle,$x,$y,$c,$font,$string);
- }
- // html2rgb() converts html-style colors to rgb array as return
- function html2rgb($color) {
- if ($color[0] == '#') $color = substr($color, 1);
- if (strlen($color) == 6) list($r, $g, $b) = array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
- elseif (strlen($color) == 3) list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
- else return false;
- return array('red'=>hexdec($r),'green'=>hexdec($g),'blue'=>hexdec($b));
- }
- // textlimit($string, $length) takes any $string you pass it and resturns it shortened to $length characters (use it to limit title length)
- function textlimit($string, $length=15) {
- return ( strlen(trim($string)) > $length ? trim( substr(trim($string),0,$length-3) )."..." : $string );
- }
- // textlimitpx($string, $pixels, $font, $size) returns the shortened $string that fits within exactly $pixels width horizontally when using $font and $size
- function textlimitpx($string, $pixels, $font, $size) {
- for($k = strlen(trim($string)); $k > 0; $k--) { if (textwidth(textlimit($string,$k), $font, $size) <= $pixels) break; }
- return textlimit($string,$k);
- }
- // textwidth($string, $font, $size) returns the pixel width of the final text with those parameters
- function textwidth($string, $font, $size) {
- $box = imagettfbbox($size,0,$font,$string);
- return $box[2] - $box[0];
- }
- // download() downloads the content at $url and returns the raw string, not including the http header
- function download($url) {
- if(function_exists('curl_init')) {
- $ch = curl_init($url);
- curl_setopt($ch,CURLOPT_HEADER,false);
- curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
- $results = curl_exec($ch); // download!
- curl_close($ch);
- }
- if (!$results) $results = file_get_contents($url); // curl failed, try url_fopen
- if (!$results) die("Could not download from $url"); // give up
- return $results;
- }
- // check_cache($minutes) returns a cached image and stops execution if $minutes has not passed since the last update
- function check_cache($minutes) {
- global $cachedpath;
- if ( !( is_writable($cachedpath) or is_writable(dirname($cachedpath)) and !file_exists($cachedpath) ) )
- die("The cache is not writable; please change it to 777 permissions using FTP.\n<br />\$cachedpath = {$cachedpath}");
- if ( time() - @filemtime($cachedpath) < 60*$minutes and @filemtime(basename($_SERVER['SCRIPT_NAME'])) < @filemtime($cachedpath) and !$GET['debug']) {
- header("Content-type: image/png");
- echo file_get_contents($cachedpath);
- exit(0);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement