<?
session_start();
include('_db.connection.php');
include('_public.setup.php');
include('_functions.php');
?>

	<table cellpadding="0" cellspacing="0" width="780" border="0">
		<?php include("i.header.php"); ?>
		<tr>
			<td>
				<table cellpadding="0" cellspacing="0" border="0">
					<tr>
						<td valign="top">
							<!--
							**************************
							**** BEGIN BODY TABLE ****
							**************************
							-->
							<table cellpadding="0" cellspacing="0">
								<tr>
									<td><img src="./images/HeaderUploadImages.gif"></td>
								</tr>
								<tr>
									<td colspan="2" align="center">
										<!--
										**************************
										*** BEGIN BODY CONTENT ***
										**************************
										-->
										<table width="90%">
											<script language="javascript">
											function copyit(theField) {
												var tempval=eval("document."+theField)
												tempval.focus()
												tempval.select()
												therange=tempval.createTextRange()
												therange.execCommand("Copy")
											}
											</script>
											<form name="link_code">
											<tr>
												<td><font face="verdana" color="#335F7A" style="font-size: 12;">Your images have been uploaded.<br><br></td>
											</tr>
												<?
												// get quality setting
												$setup_result = mysql_query("SELECT imgquality,url FROM setup WHERE setup_id = 1", $db);
												$setup = mysql_fetch_object($setup_result);
												// store images on server at this quality	
												$imgquality = $setup->imgquality;
												
												$num_files = count($image);		
												$path = "usr/" . $mid . "/";
												
												for($i = 0; $i < $num_files; $i++){													
													if($image_size[$i] > 0) {
														
														$image_key = rand(10,99999999);
																						
														// Replace spaces
														$name = str_replace(" ", "_", $image_name[$i]);
														
														// Image Type
														$type = $image_type[$i];
														
														// Check if the file exsists
														// Rename to a .jpg
														//$length = strlen($name);
														//$finalname = substr($name, 0, ($length - 4));
														//$finalname = $finalname . ".jpg";
														if (!file_exists($path . $name)) {
															// no image exists
														}
														else {
															// image exists, rename
															$name = rand(10,9999) . $name;
														}
														
														// Max Width
														$max_width = 2800;
														
														// Upload the Image
														switch($type){
															case "image/pjpeg":															
																// Rename to a .jpg < lower
																//$length = strlen($name);
																//$rename = substr($name, 0, ($length - 4));
																//$name = $rename . ".jpg";
															
																copy($image[$i], $path . $name);
																
																$the_size = getimagesize($path . $name);
																$new_width = 300;
																
																// Resize If Needed				
																if($resize == 1) {
																	if($the_size[0] > $new_width) {
																		$src_img = ImageCreateFromJPEG($path . $name);								
																		$ratio = $new_width/$the_size[0];
																		$new_height = $the_size[1] * $ratio;
																		
																		$dst_img = ImageCreate($new_width, $new_height);
																		imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																		//imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
																		imagejpeg($dst_img, $path . $name, $imgquality);
																		imagedestroy($src_img); 
																		imagedestroy($dst_img);
																	}
																	
																}
																else{
																	// If the image is greater than 800 then resize it
																	if($the_size[0] > $max_width) {
																		$src_img = ImageCreateFromJPEG($path . $name);								
																		$ratio = $max_width/$the_size[0];
																		$new_height = $the_size[1] * $ratio;
																		
																		$dst_img = ImageCreate($max_width, $new_height);
																		imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $max_width, $new_height, imagesx($src_img), imagesy($src_img));
																		//imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
																		imagejpeg($dst_img, $path . $name, $imgquality);
																		imagedestroy($src_img); 
																		imagedestroy($dst_img);
																	}
																}
																	
																
																// Create Icon
																$new_width = 150;
																$src_img = ImageCreateFromJPEG($path . $name);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "i_" . $name, 65);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																// Create HTA Icon
																$new_width = 50;
																$src_img = ImageCreateFromJPEG($path . $name);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "hta_" . $name, 85);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																
																$status = 1;
																
															// UPLOAD TO DATABASE --------------------------------------
																// File Size
																$the_filesize = filesize($path . $name);
																$k_size = round($the_filesize/1000);
																
																// Dimentions
																$the_size = getimagesize($path . $name);
																
																if($quality == ""){
																	$quality = "60";
																}
																
																$sql = "INSERT INTO images(filename,width,height,mid,fid,caption,imgquality,image_key,file_size) VALUES ('$name','$the_size[0]','$the_size[1]','$mid','$fid','$caption[$i]','$quality','$image_key','$k_size')";
																$result = mysql_query($sql);
															// ---------------------------------------------------------
																
															break;
															
															case "image/jpeg":															
																// Rename to a .jpg < lower
																//$length = strlen($name);
																//$rename = substr($name, 0, ($length - 4));
																//$name = $rename . ".jpg";
															
																copy($image[$i], $path . $name);
																
																$the_size = getimagesize($path . $name);
																$new_width = 300;
																
																// Resize If Needed				
																if($resize == 1) {
																	if($the_size[0] > $new_width) {
																		$src_img = ImageCreateFromJPEG($path . $name);								
																		$ratio = $new_width/$the_size[0];
																		$new_height = $the_size[1] * $ratio;
																		
																		$dst_img = ImageCreate($new_width, $new_height);
																		imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																		//imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
																		imagejpeg($dst_img, $path . $name, $imgquality);
																		imagedestroy($src_img); 
																		imagedestroy($dst_img);
																	}
																	
																}
																else{
																	// If the image is greater than 800 then resize it
																	if($the_size[0] > $max_width) {
																		$src_img = ImageCreateFromJPEG($path . $name);								
																		$ratio = $max_width/$the_size[0];
																		$new_height = $the_size[1] * $ratio;
																		
																		$dst_img = ImageCreate($max_width, $new_height);
																		imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $max_width, $new_height, imagesx($src_img), imagesy($src_img));
																		//imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
																		imagejpeg($dst_img, $path . $name, $imgquality);
																		imagedestroy($src_img); 
																		imagedestroy($dst_img);
																	}
																}
																	
																
																// Create Icon
																$new_width = 150;
																$src_img = ImageCreateFromJPEG($path . $name);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "i_" . $name, 65);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																// Create HTA Icon
																$new_width = 50;
																$src_img = ImageCreateFromJPEG($path . $name);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "hta_" . $name, 85);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																
																$status = 1;
																
															// UPLOAD TO DATABASE --------------------------------------
																// File Size
																$the_filesize = filesize($path . $name);
																$k_size = round($the_filesize/1000);
																
																// Dimentions
																$the_size = getimagesize($path . $name);
																
																if($quality == ""){
																	$quality = "60";
																}
																
																$sql = "INSERT INTO images(filename,width,height,mid,fid,caption,imgquality,image_key,file_size) VALUES ('$name','$the_size[0]','$the_size[1]','$mid','$fid','$caption[$i]','$quality','$image_key','$k_size')";
																$result = mysql_query($sql);
															// ---------------------------------------------------------
																
															break;
															
															/*
															case "image/gif":
																copy($image[$i], $path . $name);
																
																$the_size = getimagesize($path . $name);
																$new_width = 300;
																
																// Create Icon
																$new_width = 150;
																$src_img = ImageCreateFromGIF($path . $name);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "i_" . $name, 65);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																// Create HTA Icon
																$new_width = 50;
																$src_img = ImageCreateFromGIF($path . $name);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "hta_" . $name, 85);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																//unlink($path . $name);
																//$name = $rename;
																
																$status = 1;
																
															// UPLOAD TO DATABASE --------------------------------------
																// File Size
																$the_filesize = filesize($path . $name);
																$k_size = round($the_filesize/1000);
																
																// Dimentions
																$the_size = getimagesize($path . $name);
																
																if($quality == ""){
																	$quality = "60";
																}
																
																$sql = "INSERT INTO images(filename,width,height,mid,fid,caption,imgquality,image_key,imagetype,file_size) VALUES ('$name','$the_size[0]','$the_size[1]','$mid','$fid','$caption[$i]','$quality','$image_key','GIF','$k_size')";
																$result = mysql_query($sql);
															// ---------------------------------------------------------
																
															break;
															
															*/
															
															case "image/x-png":
																copy($image[$i], $path . $name);
																
																$the_size = getimagesize($path . $name);
																$new_width = 300;
																
																// Resize If Needed				
																if($resize == 1) {
																	if($the_size[0] > $new_width) {
																		$src_img = ImageCreateFromPNG($path . $name);								
																		$ratio = $new_width/$the_size[0];
																		$new_height = $the_size[1] * $ratio;
																		
																		$dst_img = ImageCreate($new_width, $new_height);
																		imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																		//imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $new_w, $new_h);
																		
																		// Rename to a .jpg
																		$length = strlen($name);
																		$rename = substr($name, 0, ($length - 4));
																		$rename = $rename . ".jpg";
																		
																		imagejpeg($dst_img, $path . $rename, $imgquality);
																		imagedestroy($src_img); 
																		imagedestroy($dst_img);
																	}
																	else{
																		$src_img = ImageCreateFromPNG($path . $name);							
																		$dst_img = ImageCreate($the_size[0], $the_size[1]);
																		imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $the_size[0], $the_size[1], imagesx($src_img), imagesy($src_img));
																		
																		// Rename to a .jpg
																		$length = strlen($name);
																		$rename = substr($name, 0, ($length - 4));
																		$rename = $rename . ".jpg";
																		
																		imagejpeg($dst_img, $path . $rename, $imgquality);
																		imagedestroy($src_img);
																		imagedestroy($dst_img);	
																	}
																	
																}						
																else{
																	$src_img = ImageCreateFromPNG($path . $name);							
																	$dst_img = ImageCreate($the_size[0], $the_size[1]);
																	imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $the_size[0], $the_size[1], imagesx($src_img), imagesy($src_img));
																	
																	// Rename to a .jpg
																	$length = strlen($name);
																	$rename = substr($name, 0, ($length - 4));
																	$rename = $rename . ".jpg";
																	
																	imagejpeg($dst_img, $path . $rename, $imgquality);
																	imagedestroy($src_img);
																	imagedestroy($dst_img);	
																}
																
																// Create Icon
																$new_width = 150;
																$src_img = ImageCreateFromJPEG($path . $rename);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "i_" . $rename, 65);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																// Create HTA Icon
																$new_width = 50;
																$src_img = ImageCreateFromJPEG($path . $rename);								
																$ratio = $new_width/$the_size[0];
																$new_height = $the_size[1] * $ratio;						
																$dst_img = ImageCreate($new_width, $new_height);
																imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, imagesx($src_img), imagesy($src_img));
																imagejpeg($dst_img, $path . "hta_" . $rename, 85);
																imagedestroy($src_img); 
																imagedestroy($dst_img);
																
																unlink($path . $name);
																$name = $rename;
																
																$status = 1;
																
															// UPLOAD TO DATABASE --------------------------------------
																// File Size
																$the_filesize = filesize($path . $name);
																$k_size = round($the_filesize/1000);
																
																// Dimentions
																$the_size = getimagesize($path . $name);
																
																if($quality == ""){
																	$quality = "60";
																}
																
																$sql = "INSERT INTO images(filename,width,height,mid,fid,caption,imgquality,image_key,file_size) VALUES ('$name','$the_size[0]','$the_size[1]','$mid','$fid','$caption[$i]','$quality','$image_key','$k_size')";
																$result = mysql_query($sql);
															// ---------------------------------------------------------
																						
															break;
															
															default:
																$status = 0;
															break;
														}
														
														if($status == 1){
															$img_result = mysql_query("SELECT * FROM images where image_key = '$image_key'", $db);
															$img = mysql_fetch_object($img_result);
															
														?>														
														<tr>
															<td>
																<table width="511" style="border: 1px solid #eeeeee">
																	<tr>
																		<td width="65" valign="top" align="center" rowspan="2" bgcolor="#eeeeee"><a href="image.details.php?img=<? echo $img->image_id . "&fid=" . $img->fid; ?>"><img src="usr/<? echo $mid . "/hta_" . $img->filename; ?>" border="0"><br>Click to View Details</a></td>
																		<td><b>Uploaded Successfully</b><br>Link to this image:<br><input type="textbox" name="link_<? echo $image_key; ?>" value="<? echo $setup->url . "/is.php?i=" . $img->image_id . "&img=" . $img->filename; ?>" style="width: 330; font-size: 10;"></td>
																		<td valign="bottom" style="padding-bottom: 3px;"><a href="javascript:copyit('link_code.link_<? echo $image_key; ?>')"><img src="images/ButtonCopy.gif" border="0"></a></td>
																	</tr>
																	<tr>
																		<td width="100%"><br>HTML for this image: <textarea name="link2_<? echo $image_key; ?>" style="width: 330; height: 50;"><img src="<? echo $setup->url . "/is.php?i=" . $img->image_id . "&img=" . $img->filename; ?>" border="0"></textarea></td>
																		<td valign="bottom" style="padding-bottom: 3px;"><a href="javascript:copyit('link_code.link2_<? echo $image_key; ?>')"><img src="images/ButtonCopy.gif" border="0"></a></td>
																	</tr>
																</table>
															</td>
														</tr>
														<?
															}
															else {
																
														?>
														<tr>
															<td>
																<table width="511" style="border: 1px solid #eeeeee">
																	<tr>
																		<td width="65" align="center"><img src="images/failed.image.gif"></td>
																		<td><font color="#ff0000"><b>Upload Failed:</b> <font color="#000000">possible reason- File type not supported.</td>
																	</tr>
																</table>
															</td>
														</tr>
														<?
															}
													}
												}
												?>											
												</td>
											</tr>
											<tr>
												<td align="right"><br><br><font face="verdana" size="1"><b>Problems Uploading? <a href="contact.php">Contact Us</a></td>
											</tr>
											</form>
										</table>
										<!--
										**************************
										**** END BODY CONTENT ****
										**************************
										-->
									</td>
								</tr>
							</table>
							<!--
							**************************
							***** END BODY TABLE *****
							**************************
							-->
						</td>
						<td valign="top"bgcolor="#295670"><img src="./images/IndexDropShadow.gif"></td>
						<td valign="top" bgcolor="#083C58">
							<?php
								if($access == "true"){
									include("i.member.rn.php");
								}
								else{
									include("i.rn.plans.php");
								}	
							?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td background="./images/Footer.gif"><img src="./images/Footer.gif" border="0"></td>
		</tr>
		<tr>
			<td>
				<?php include("i.footer.php"); ?>
			</td>
		</tr>
	</table>

<?
mysql_close($db);
print($close);
?>