<?php
/**
 * @package Banner generator
 * @author Jure Ham
 * @version 0.6
 */
/*
Plugin Name: Banner Generator
Plugin URI: http://hamsworld.net/blog/
Description: Generates image with last posts title. Edit bGen settings under Settings menu. Your upload folder must be set and writable!
Author: Jure Ham
Version: 0.6
Author URI: http://hamsworld.net/blog/
*/
	
	function generate() {
	
		/* ------------------------ settings  ------------------------ */
		
		check_settings();

		$plugin_path = dirname(__FILE__);
		$path_to_image = get_option('upload_path')."/banner.png";	
		if ($path_to_image[0] != '/' && $path_to_image[1] != ':') {
			$path_to_image = ABSPATH.$path_to_image;
		}
		
		//Background image(png)
		$bg_name = "/bg.png";
		
		//Header text, font, color and size			
		$hdr_size = get_option('bgen_hdr_size');		
		$hdr_fc = sscanf(get_option('bgen_hdr_fc'), '%2x%2x%2x');									
		$head = get_option('bgen_hdr_text');
		$hdr_px = get_option('bgen_hdr_px');
		$hdr_py = get_option('bgen_hdr_py');
			
		//Title font, color and size	
		
		$logo = get_option('bgen_logo');
			
		$bd_fc = sscanf(get_option('bgen_bd_fc'), '%2x%2x%2x');				
		$bd_size = get_option('bgen_bd_size');	
		$bd_px = get_option('bgen_bd_px');
		$bd_py = get_option('bgen_bd_py');
		$maxlen = get_option('bgen_maxl');

		
		//Background
		$bg_w = get_option('bgen_bg_w');
		$bg_h = get_option('bgen_bg_h');
		$bg_co = sscanf(get_option('bgen_bg_co'), '%2x%2x%2x');
		$bg_tr = get_option('bgen_bg_tr');
		$bg_tc = sscanf(get_option('bgen_bg_tc'), '%2x%2x%2x');
		$bg_size = get_option('bgen_bg_size');
		$bg_px = get_option('bgen_bg_px');
		$bg_py = get_option('bgen_bg_py');
		$bg_bw = get_option('bgen_bg_bw');
		$bg_bc = sscanf(get_option('bgen_bg_bc'), '%2x%2x%2x');		
		$bg_title = get_option('bgen_bg_title');
		
		//Fonts
		$bg_font = $plugin_path.'/fonts/'.get_option('bgen_bg_font');
		$bd_font = $plugin_path.'/fonts/'.get_option('bgen_bd_font');
		$hdr_font = $plugin_path.'/fonts/'.get_option('bgen_hdr_font');

		/* ------------------------ End of settings ------------------------ */	
							
		$body = get_last_post_title();	
		
		/* Create bg image */
		$im = @imagecreatefrompng($plugin_path.$bg_name);		
		
		if (!$im) {
		  $im = imagecreatetruecolor($bg_w, $bg_h);
		  imagealphablending($im, true);
		  imagesavealpha($im, true);
		  $trans = imagecolorallocatealpha($im, 0, 0, 0, 127);
		  $color = imagecolorallocate($im, $bg_co[0], $bg_co[1], $bg_co[2]);
		  $border = imagecolorallocate($im, $bg_bc[0], $bg_bc[1], $bg_bc[2]);
		  imagefill($im, 0, 0, $trans);
		  
		  --$bg_w;
		  --$bg_h;
		  
		  imagefillroundedrect($im, 0, 0, $bg_w, $bg_h, $bg_tr, $border);
			imagefillroundedrect($im, $bg_bw, $bg_bw, $bg_w - ($bg_bw), $bg_h - ($bg_bw), $bg_tr, $color);
			
			//add alpha
			/*
			$im_alpha = imagecreatetruecolor(++$bg_w, ++$bg_h);
			imagealphablending($im_alpha, true);
			imagesavealpha($im_alpha, true);
			imagefill($im_alpha, 0, 0, $trans);
			ImageCopyMerge($im, $im_alpha, 0, 0, 0, 0, $bg_w, $bg_h, 10); 
			
			//new
			
			for ($x = 0; $x <= $bg_w; $x++) {
				for ($y = 0; $y <= $bg_h; $y++) {
					$colorxy = imagecolorat( $im, $x, $y ); 
					$alpha = ( $colorxy >> 24 ) & 0xFF; 
					$alpha += 50;
					if ($alpha > 127) $alpha = 127;
					$alphacolorxy = imagecolorallocatealpha( $im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha ); 
					imagesetpixel( $im, $x, $y, $alphacolorxy );
				}
			}
			*/
			
		}

		if ($bg_title == 'checked') {
		
			$im_logo = @imagecreatefrompng($logo);
			
			if ($im_logo) {

				imagealphablending($im_logo, true);
				imagesavealpha($im_logo, true);
				
				$logo_size = getimagesize($logo);
				
				imagecopy($im, $im_logo, $bg_px, $bg_py, 0, 0, $logo_size[0], $logo_size[1]);
								
				imagedestroy($im_logo);
			}	else {	
				$colort = imagecolorallocate($im, $bg_tc[0], $bg_tc[1], $bg_tc[2]);
				imagefttext($im, $bg_size, 0, $bg_px, $bg_py, $colort, $bg_font, get_option('blogname'));		
			}
		}
		
		if (strlen($body) > $maxlen) {
			$body = substr($body, 0, $maxlen - 3)."...";
		}

		$colorn = imagecolorallocate($im, $hdr_fc[0], $hdr_fc[1], $hdr_fc[2]);			
		$colorb = imagecolorallocate($im, $bd_fc[0], $bd_fc[1], $bd_fc[2]);	
		
		imagealphablending($im, true);
		imagesavealpha($im, true);
	
		imagefttext($im, $hdr_size, 0, $hdr_px, $hdr_py, $colorn, $hdr_font, $head);
		imagefttext($im, $bd_size, 0, $bd_px, $bd_py, $colorb, $bd_font, $body);
	
		imagepng($im, $path_to_image, 0, NULL);
		imagedestroy($im);

	}
	
	add_action('publish_post', 'generate');
	add_action('deleted_post', 'generate');
	register_activation_hook( __FILE__, 'generate' );
	
	function get_last_post_title() {
		
		
		global $wpdb;
		
		$post_title = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE ID = 
			(SELECT max(ID) FROM $wpdb->posts WHERE post_status = 'publish')");

		$title = $post_title[0]->post_title;
		
		//if db fails
		if (!$title) {
		
			$dom = new DomDocument;
			$dom->load(get_option('siteurl').'/?feed=rss');
		
			$title = $dom -> getElementsByTagName("title")->item(1)->nodeValue;		
		
		}
		
		return $title;
		
	}
	
	function imagefillroundedrect($im,$x,$y,$cx,$cy,$rad,$col) {
/*		
		$cx = ($cx < $x) ? $x : $cx; 
		$cy = ($cy < $y) ? $y : $cy;
		$rad = ($rad < 0) ? 0 : $rad;
*/		
	// Draw the middle cross shape of the rectangle

		imagefilledrectangle($im,$x,$y+$rad,$cx,$cy-$rad,$col);
		imagefilledrectangle($im,$x+$rad,$y,$cx-$rad,$cy,$col);

		$dia = $rad*2;

	// Now fill in the rounded corners

		imagefilledellipse($im, $x+$rad, $y+$rad, $dia, $dia, $col);
		imagefilledellipse($im, $x+$rad, $cy-$rad, $dia, $dia, $col);
		imagefilledellipse($im, $cx-$rad, $cy-$rad, $dia, $dia, $col);
		imagefilledellipse($im, $cx-$rad, $y+$rad, $dia, $dia, $col);
	}


	/* -------------- admin --------------- */
	
	add_action('admin_menu','bgen_admin');
	
	function check_settings() {
		$options = array(
			'bgen_hdr_size' => 9, 
			'bgen_hdr_text' => 'Last post:',
			'bgen_hdr_fc' => 'FFFFFF',
			'bgen_bd_size' => 11,
			'bgen_bd_fc' => 'FFFFFF', 
			'bgen_hdr_px' => 15,
			'bgen_hdr_py' => 43,
			'bgen_bd_px' => 20,
			'bgen_bd_py' => 58,
			'bgen_maxl' => 36,
			'bgen_logo' => '',
			'bgen_bg_w' => 350,
			'bgen_bg_h' => 65,
			'bgen_bg_co' => '999999',
			'bgen_bg_tr' => 10, 
			'bgen_bg_size' => 22, 
			'bgen_bg_px' => 10, 
			'bgen_bg_py' => 25, 
			'bgen_bg_bw' => 1, 
			'bgen_bg_bc' => '555555', 
			'bgen_bg_title' => 'checked',
			'bgen_bg_font' => 'MonoBold.ttf',
			'bgen_bd_font' => 'MonoBold.ttf',
			'bgen_hdr_font' => 'Mono.ttf',
			'bgen_bg_tc' => 'FFFFFF'
		);
		
		foreach ($options as $key => $value) {
			if (get_option($key) == "")
				update_option($key, $value);	
		}
	}
		
	function bgen_admin()
	{

		if ( function_exists('add_submenu_page') ) {
			add_options_page('bGen', 'bGen', 9, basename(__FILE__), 'bgen_manage');
		}
	
	}
		
	function bgen_manage() {
	
		if (isset($_POST['bgen_hdr_size'])) {
			update_option('bgen_hdr_size', $_POST['bgen_hdr_size']);
			update_option('bgen_hdr_text', $_POST['bgen_hdr_text']);			
			update_option('bgen_hdr_fc', $_POST['bgen_hdr_fc']);			
			update_option('bgen_bd_size', $_POST['bgen_bd_size']);		
			update_option('bgen_bd_fc', $_POST['bgen_bd_fc']);			
			update_option('bgen_hdr_px', $_POST['bgen_hdr_px']);
			update_option('bgen_hdr_py', $_POST['bgen_hdr_py']);
			update_option('bgen_bd_px', $_POST['bgen_bd_px']);
			update_option('bgen_bd_py', $_POST['bgen_bd_py']);			
			update_option('bgen_maxl', $_POST['bgen_maxl']);		
			update_option('bgen_logo', $_POST['bgen_logo']);		
			update_option('bgen_bg_w', $_POST['bgen_bg_w']);			
			update_option('bgen_bg_h', $_POST['bgen_bg_h']);	
			update_option('bgen_bg_co', $_POST['bgen_bg_co']);	
			update_option('bgen_bg_tr', $_POST['bgen_bg_tr']);	
			update_option('bgen_bg_tc', $_POST['bgen_bg_tc']);			
			update_option('bgen_bg_size', $_POST['bgen_bg_size']);	
			update_option('bgen_bg_px', $_POST['bgen_bg_px']);	
			update_option('bgen_bg_py', $_POST['bgen_bg_py']);	
			update_option('bgen_bg_bw', $_POST['bgen_bg_bw']);	
			update_option('bgen_bg_bc', $_POST['bgen_bg_bc']);	
			update_option('bgen_bg_font', $_POST['bgen_bg_font']);	
			update_option('bgen_bd_font', $_POST['bgen_bd_font']);	
			update_option('bgen_hdr_font', $_POST['bgen_hdr_font']);	
			
			
			if (isset($_POST['bgen_bg_title'])) {
				update_option('bgen_bg_title', 'checked');	
			} else {
				update_option('bgen_bg_title', 'unchecked');	
			}
			
			
			echo '<div class="updated"><p><strong>Options saved</strong></p></div>';
			
			generate();
		}
		
		check_settings();
		
		global $fonts;
		
		$hdr_text = get_option('bgen_hdr_text');
		$hdr_size = get_option('bgen_hdr_size');
		$bd_size = get_option('bgen_bd_size');
		
		$hdr_fc = get_option('bgen_hdr_fc');	
		$bd_fc = get_option('bgen_bd_fc');		

		$hdr_px = get_option('bgen_hdr_px');
		$hdr_py = get_option('bgen_hdr_py');		
		
		$bd_px = get_option('bgen_bd_px');
		$bd_py = get_option('bgen_bd_py');		
		
		$maxl = get_option('bgen_maxl');	
		$logo = get_option('bgen_logo');
		
		$bg_w = get_option('bgen_bg_w');
		$bg_h = get_option('bgen_bg_h');
		$bg_co = get_option('bgen_bg_co');
		$bg_tr = get_option('bgen_bg_tr');
		$bg_tc = get_option('bgen_bg_tc');
		$bg_size = get_option('bgen_bg_size');
		$bg_px = get_option('bgen_bg_px');
		$bg_py = get_option('bgen_bg_py');

		$bg_bw = get_option('bgen_bg_bw');
		$bg_bc = get_option('bgen_bg_bc');
		
		$bg_title = get_option('bgen_bg_title');
		
		$bg_font = get_option('bgen_bg_font');
		$bd_font = get_option('bgen_bd_font');
		$hdr_font = get_option('bgen_hdr_font');
		
		$fonts = array(
			'Mono' => 'Mono.ttf',
			'Mono Bold' => 'MonoBold.ttf',
			'Mono Bold Oblique' => 'MonoBoldOblique.ttf',
			'Mono Oblique' => 'MonoOblique.ttf',
			'Sans' => 'Sans.ttf',
			'Sans Bold' => 'SansBold.ttf',
			'Sans Bold Oblique' => 'SansBoldOblique.ttf',
			'Sans Oblique' => 'SansOblique.ttf',
			'Serif' => 'Serif.ttf',
			'Serif Bold' => 'SerifBold.ttf',
			'Serif BoldI talic' => 'SerifBoldItalic.ttf',
			'Serif Italic' => 'SerifItalic.ttf'
		);
		
	?>
	<div class="wrap">
	<h2>bGen Options
	</h2>
	
	<form name="form1" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
		<h3>Header settings:</h3>

		Message<br/>
		<input type="text" name="bgen_hdr_text" value="<?php echo $hdr_text;?>"><br/>
		
		Font<br/>
		<select name="bgen_hdr_font">
			<?php
			foreach ($fonts as $key => $value) {
				$selected = ($value == $hdr_font) ? 'selected' : '';
				echo '<option value="'.$value.'" '.$selected.'>'.$key.'</option>';
			}
			?>
		</select><br/>
			
		Font size<br/>
		<input type="text" name="bgen_hdr_size" value="<?php echo $hdr_size;?>"><br/>
		
		Font color (FFFFFF for white)<br/>
		<input type="text" name="bgen_hdr_fc" value="<?php echo $hdr_fc;?>"><br/>

		Text margin left<br/>
		<input type="text" name="bgen_hdr_px" value="<?php echo $hdr_px;?>"><br/>

		Text margin top<br/>
		<input type="text" name="bgen_hdr_py" value="<?php echo $hdr_py;?>"><br/>
		<br />
		<hr/>
		
		<h3>Last post title settings:</h3>
		
		Font<br/>
		<select name="bgen_bd_font">
			<?php
			foreach ($fonts as $key => $value) {
				$selected = ($value == $bd_font) ? 'selected' : '';
				echo '<option value="'.$value.'" '.$selected.'>'.$key.'</option>';
			}
			?>
		</select><br/>
		
		Font size<br/>
		<input type="text" name="bgen_bd_size" value="<?php echo $bd_size;?>">	<br/>
		
		Font color (FFFFFF for white)<br/>
		<input type="text" name="bgen_bd_fc" value="<?php echo $bd_fc;?>"><br/>
		
		Text margin left<br/>
		<input type="text" name="bgen_bd_px" value="<?php echo $bd_px;?>"><br/>

		Text margin top<br/>
		<input type="text" name="bgen_bd_py" value="<?php echo $bd_py;?>"><br/>
		
		Max text length<br/>
		<input type="text" name="bgen_maxl" value="<?php echo $maxl;?>"><br/>
		<br/>
		<hr/>
		
		<h3>Blog logo settings:</h3>
		<input type="checkbox" name="bgen_bg_title" <?php echo $bg_title;?> > Show blog logo<br/><br/>

		Try to use this logo (url or absoluth path to PNG image)<br/>
		<input style="width:300px;" type="text" name="bgen_logo" value="<?php echo $logo;?>"><br/><br/>
		
		Font<br/>
		<select name="bgen_bg_font">
			<?php
			foreach ($fonts as $key => $value) {
				$selected = ($value == $bg_font) ? 'selected' : '';
				echo '<option value="'.$value.'" '.$selected.'>'.$key.'</option>';
			}
			?>
		</select><br/>
		
		Font size<br/>
		<input type="text" name="bgen_bg_size" value="<?php echo $bg_size;?>"><br/>

		Font color (FFFFFF for white)<br/>
		<input type="text" name="bgen_bg_tc" value="<?php echo $bg_tc;?>"><br/><br/>
				
		Logo margin left<br/>
		<input type="text" name="bgen_bg_px" value="<?php echo $bg_px;?>"><br/>

		Logo margin top<br/>
		<input type="text" name="bgen_bg_py" value="<?php echo $bg_py;?>">	<br/>
		
		<br/>
		<hr/>
				
		<h3>Background settings (if there is no 'bg.png' file):</h3>
		Dimensions<br/>
		Width: <input style="width:60px;" type="text" name="bgen_bg_w" value="<?php echo $bg_w;?>"><br/>
		Height: <input style="width:60px;" type="text" name="bgen_bg_h" value="<?php echo $bg_h;?>"><br/>

		Color (FFFFFF for white)<br/>
		<input type="text" name="bgen_bg_co" value="<?php echo $bg_co;?>"><br/>

		Corners radius (in px)<br/>
		<input type="text" name="bgen_bg_tr" value="<?php echo $bg_tr;?>"><br/>
		
		Border width (in px)<br/>
		<input type="text" name="bgen_bg_bw" value="<?php echo $bg_bw;?>"><br/>
		
		Border color (FFFFFF for white)<br/>
		<input type="text" name="bgen_bg_bc" value="<?php echo $bg_bc;?>"><br/>
		
		<br/>
		<hr/>

		<h3>Codes:</h3>
		<?php $img_url = get_option('siteurl').'/'.str_replace(ABSPATH, '', get_option('upload_path')).'/banner.png'?>
		Image url:<br/>
		<div style="background:#fff; border: 1px solid #ddd; padding:3px"><a href ="<?php echo $img_url;?>"><?php echo $img_url;?></a></div><br/>
		
		HTML code:<br/>
		<div style="background:#fff; border: 1px solid #ddd; padding:3px">&lt;a href ="<?php echo get_option('siteurl');?>"&gt;&lt;img src="<?php echo $img_url;?>"&gt;&lt;/a&gt;</div><br/>

		BB code:<br/>
		<div style="background:#fff; border: 1px solid #ddd; padding:3px">[url=<?php echo get_option('siteurl');?>][img]<?php echo $img_url;?>[/img][/url]</div>
		<hr/>		
		<p class="submit">
		<input type="submit" name="Submit" value="Update Options &raquo;" />
		</p>
	</form>
	</div>
	<?php
	}
	
?>
