<?php
include "../includes/config.php";
include "../includes/database.php";
include "../includes/session.php";
include "../includes/function.php";
?>
<style>
TD {
    
	FONT:  11px verdana, arial, helvetica, sans-serif
}
</style>
<?php
if(isset($_POST['upload']) && $_FILES['uploader_file']['size'] > 0)
{
    $fileName = $_FILES['uploader_file']['name'];
    $tmpName  = $_FILES['uploader_file']['tmp_name'];
    $fileSize = $_FILES['uploader_file']['size'];
    $fileType = $_FILES['uploader_file']['type'];
    
    
    $fp= fopen($tmpName, 'r');
	$content = fread($fp, filesize($tmpName));
	
	//$content = addslashes($content);
	fclose($fp);
					
	if(!get_magic_quotes_gpc())
		{
		$fileName = addslashes($fileName);
	}

	if(($fileType=="image/jpeg") or ($fileType=="image/pjpeg") or ($fileType=="image/gif"))
		{
						      
			//=========RESIZE===============
				$oSourceImage = imagecreatefromstring($content); 
						      
				$nWidth = imagesx($oSourceImage); // get original source image width
				$nHeight = imagesy($oSourceImage); // and height
							
				$img_ratio=$nHeight/$nWidth;
								
				// create small thumbnail
				$nDestinationWidth = 500;
				$nDestinationHeight = 500*$img_ratio;
                                $nDestinationWidth = $nWidth;
				$nDestinationHeight = $nHeight;


				$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
				//$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
							
				/*$oResult = imagecopyresampled(
					$oDestinationImage, $oSourceImage,
					0, 0, 0, 0,
					$nDestinationWidth, $nDestinationHeight,
					$nWidth, $nHeight); // resize the image
				*/
				imagecopyresized(
					$oDestinationImage, $oSourceImage,
					0, 0, 0, 0,
					$nDestinationWidth, $nDestinationHeight,
					$nWidth, $nHeight); // resize the image
							
				
				$query = "INSERT INTO t_".$key_table_prefix."_uploader
										(pho_name, 
										pho_size, 
										pho_type, 								
										CREATED_DATE,
										MODIFIED_DATE) 
						        		VALUES 
										('blog_".$CurrentShopID.".gif',
										'$fileSize', 
										'$fileType', 
										'".date("Y-m-d H:i:s")."',
										'".date("Y-m-d H:i:s")."')";
										
				mysqli_query($dbconn,$query) or die('Photo Saving Fail - Err msg  '.mysqli_error($dbconn));     			
				$blog_photo_id=mysqli_insert_id($dbconn);			
						
				if($_REQUEST["pho_stamp"]=="YES")
					{
									  
						$logo = imagecreatefromgif("../stamp_logo/logo.gif");
						imagealphablending ($logo, true);
						imagecopy($oSourceImage, $logo, $nWidth/2-40, $nHeight/2-40, 0, 0, 80, 80);
		
					}			
							
				ob_start(); // Start capturing stdout.
				//imageGIF($oDestinationImage); // As though output to browser.
				imageGIF($oSourceImage ,'../../uploads/uploader/photo_'.$blog_photo_id.'.gif');
				$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
				ob_end_clean(); // Dump the stdout so it does not screw other output.
		
			
				$sBinaryThumbnail = addslashes($sBinaryThumbnail);
		
				$content=$sBinaryThumbnail;
				$fileType="image/gif";		
						      
				//======END of RESIZE============
		   	
			
			
		}				

				          
        echo "<script type=\"text/javascript\" language=\"JavaScript\">parent.frame_uploader2.location=\"index.php\";location.href=\"add.php\";</script>";
    }
  


?>
<form name="uploader" action="add.php" method="post" enctype="multipart/form-data" >
<table><tr><td valign=top >
<b>Photo Uploader:</b>

<input size=10 type=file name="uploader_file" class=upload >
<input type=submit value=Upload name="upload" class=button >
<input type="checkbox" class="checkbox" name="pho_stamp" value="YES" checked><font color=FF0000> Stamp logo.</font><br><br>
<b>Step 1:</b> Browse a File and Click Upload
<br>
<b>Step 2:</b> Focus your mouse pointer on the Content area, Click the photo at the Gallery to insert into the Content area.
</td></tr></table>
</form>

