<?
//Header("Content-type: image/jpeg");
include("_db.connection.php");

// Website Settings
$setup_result = mysql_query("SELECT url,title,imgquality FROM setup WHERE setup_id = 1", $db);
$setup = mysql_fetch_object($setup_result);

// check renew date (all members)
//include("./renew.code.php");

$image_result = mysql_query("SELECT * FROM images WHERE image_id = '$i'");
$image_count = mysql_num_rows($image_result);
$image = mysql_fetch_object($image_result);
	$image_views = $image->viewed + 1;
	
	if($image_count == 0){
		Header("Content-type: image/gif");
		include("images/warn.deleted.gif");
		exit;	
	}

$member_result = mysql_query("SELECT * FROM members WHERE member_id = '$image->mid'");
$member = mysql_fetch_object($member_result);
	$cycle_views = $member->cycle_views + 1;
	$total_views = $member->total_views + 1;

if($member->active == 0){
	Header("Content-type: image/gif");
	include("images/warn.inactive.gif");
	exit;	
}

else{

	if($member->custom_plan == 1){
		$max_bandwidth = $member->custom_bandwidth;
		$branding = $member->custom_branding;
	}
	else{
		$plan_result = mysql_query("SELECT * FROM plans WHERE plan_id = '$member->plan'");
		$plan = mysql_fetch_object($plan_result);
		$max_bandwidth = $plan->bandwidth;
		$branding = $plan->branding;
		
	}

	if($max_bandwidth <= $member->used_bandwidth){
		Header("Content-type: image/gif");
		include("images/warn.bw.limit.gif");
		exit;
	}
	else{
		// calculate filesize
		$src = "./usr/" . $member->member_id . "/";
		
		$src .= $image->filename;
						
		$image_bytes= filesize($src);		
		$img_dim = getimagesize($src);
		$image_megs = $image_bytes/1000000;
		$update_bw = $member->used_bandwidth + $image_megs;
			
		// update the member database
		$sql = "UPDATE members SET used_bandwidth='$update_bw',cycle_views='$cycle_views',total_views='$total_views' WHERE member_id = '$member->member_id'";
		$result = mysql_query($sql);
		
		// update the image database
		$sql = "UPDATE images SET viewed='$image_views' WHERE image_id = '$i'";
		$result = mysql_query($sql);
		
		$imgquality = $image->imgquality;
		
		if($image->imagetype == "GIF"){
			Header("Content-type: image/gif");
			include($src);
		}
		
		else{
			Header("Content-type: image/jpeg");
			// create image
			if($branding == 1){
				// add branding
				$height = $img_dim[1];
				$width = $img_dim[0];
				
				$new_height = $height + 8;
				$new_width = $width;
							
				$src_img = imagecreatefromjpeg($src);
				
				$dst_img = imagecreate($new_width,$new_height);						
				imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_width, $new_height); 
				
				// Print Branding Text On Image
				$white = imagecolorallocate($dst_img, 255, 255, 255);
				$black = imagecolorallocate($dst_img, 0, 0, 0);
				//imagestring($src_img, 3, ($img_dim[0] / 2.95), ($img_dim[1] / 6.5), "This is a test", $black);
				// Align Left
				//imagestring($dst_img, 2, 5, ($img_dim[1] - 7), "Image Hosted By " . $setup->title, $white);
				imagestring($dst_img, 2, ($img_dim[0]/2 - 64), ($img_dim[1] - 7), "Hosted By " . $setup->title, $white);
				
				imagejpeg($dst_img,'', $imgquality);
				imagedestroy($src_img); 
				imagedestroy($dst_img);
			}
			else{
				// no branding
				$img = ImageCreateFromJpeg($src);
				Imagejpeg($img,'',$imgquality);
				ImageDestroy($img);
			}	
		}
	}
}
mysql_close($db);
?>