User:Xhantar/Sandbox1

From Wikipedia, the free encyclopedia
<?

/* 
	REQUIRED: 
	   * MagpieRSS, XML-based RSS parser - http://magpierss.sourceforge.net/
	   * $rss - The RSS feed URL
	   * GD library for PHP - http://www.php.net/gd	   
*/

require_once('magpierss/rss_fetch.inc');

// Send headers so browser knows a PNG is being served
header("Content-type: image/png");
header("Pragma: no-cache\nCache-control: no-cache\n"); 

class textPNG {
	var $font = 'couriernew.ttf'; // Default font. Directory relative to script directory.
	var $size = 8; // Font size.
	var $rot = 0; // Rotation in degrees.
	var $pad = 20; // Padding.
	var $transparent = 0; // Transparency; 1 = on.
	var $red = 26; // Text colour; RGB.
	var $grn = 8;
	var $blu = 158;
	var $bg_red = 178; // Background colour; RGB.
	var $bg_grn = 227;
	var $bg_blu = 234;

	function draw() {

		// REQUIRED: RSS feed URL
		$rss = fetch_rss("http://qdb.us/qdb.xml");

		$rand_element = rand(0,count($rss->items));

		$desc = html_entity_decode($rss->items[$rand_element]['description'],ENT_QUOTES); // Convert HTML entities to characters
		$search = array('@<br />@'); $desc = preg_replace($search, '', $desc); // Remove trailing <br />

		$width = 0;
		$height = 0;
		$offset_x = 0;
		$offset_y = 0;
		$bounds = array();
		$image = "";
			
		// Determine font height
		$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
		if ($this->rot < 0) {
			$font_height = abs($bounds[7]-$bounds[1]);                
		} else if ($this->rot > 0) {
			$font_height = abs($bounds[1]-$bounds[7]);
		} else {
			$font_height = abs($bounds[7]-$bounds[1]);
		}

		// Determine bounding box
		$bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $desc);
		if ($this->rot < 0) {
			$width = abs($bounds[4]-$bounds[0]);
			$height = abs($bounds[3]-$bounds[7]);
			$offset_y = $font_height;
			$offset_x = 0;
				
		} else if ($this->rot > 0) {
			$width = abs($bounds[2]-$bounds[6]);
			$height = abs($bounds[1]-$bounds[5]);
			$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
			$offset_x = abs($bounds[0]-$bounds[6]);
					
		} else {
			$width = abs($bounds[4]-$bounds[6]);
			$height = abs($bounds[7]-$bounds[1]);
			$offset_y = $font_height;;
			$offset_x = 0;
		}
			
		// Create the image
		$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
			
		$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
		$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
			
		if ($this->transparent) ImageColorTransparent($image, $background);
		ImageInterlace($image, false);
			
		// Render the image
		ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $desc);

		// Output the PNG object
		imagePNG($image);
	}
}

$text = new textPNG;
$text->draw();

?>