<?php
function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height,$resizetype) { //$imgSrc is a FILE - Returns an image resource.
    //getting the image dimensions  
    list($width_orig, $height_orig) = getimagesize($imgSrc);   
    $myImage = imagecreatefromjpeg($imgSrc);
    $ratio_orig = $width_orig/$height_orig;

	// greater-than-sign fits the image into the space (no cropping). less than sign fills the image into the space (and cropping may occur).    
	if ($resizetype=='expand') {
      if ($thumbnail_width/$thumbnail_height < $ratio_orig) {
         $new_width = $thumbnail_height*$ratio_orig;
         $new_height = $thumbnail_height;
      } else {
         $new_height = $thumbnail_width/$ratio_orig;
         $new_width = $thumbnail_width;
      }
	} else {
      if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
         $new_width = $thumbnail_height*$ratio_orig;
         $new_height = $thumbnail_height;
      } else {
         $new_height = $thumbnail_width/$ratio_orig;
         $new_width = $thumbnail_width;
      }
	}
    
    $x_mid = $new_width/2;  //horizontal middle
    $y_mid = $new_height/2; //vertical middle
    
    $process = imagecreatetruecolor(round($new_width), round($new_height)); 
    
    imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
    $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
    imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);

    imagedestroy($process);
    imagedestroy($myImage);
    return $thumb;
}





// ==========================
// MAIN UPLOAD SEQUENCE START
// ==========================

	$target = $_GET['dir'];
	$target = $target.basename( $_FILES['uploaded']['name']);
	$ok=1;

	//This is our size condition
	if ($uploaded_size > 7500000) {
		echo "Your file is too large.<br>";
		$ok=0;
	}

	//This is our limit file type condition
	if ($uploaded_type =="text/php") {
		echo "No PHP files<br>";
		$ok=0;
	}

	//Here we check that $ok was not set to 0 by an error
	if ($ok==0) {
		echo "Sorry your file was not uploaded";
	} else {

    //If everything is ok we try to upload it	
    if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
    	echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded";

    	// set-up the random number filename and target variables
    	$jumblerand = rand(10000000,99999999);
    	$jumblekey = md5("random jumble" . $jumblerand);
		$jumblecode = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz1234567890'),0,8);
		$jumbledate = substr("00".idate(y),-2)."".substr("00".idate(m),-2)."".substr("00".idate(d),-2);
    	$uldir = $_GET['dir'];
    	$targetnew = $_FILES['uploaded']['name'];

    	// is auto-adjusting enabled?
    	if ($_POST['autoadjust']=="yes") {
    
        	// BANNERS AUTO-ADJUST:
        	if (strpos($uldir, "/banners/") >0) {
        		$targetnew = "ba_".$jumbledate."_".$jumblecode.".jpg";
                	$newThumb = CroppedThumbnail($target,980,388,"expand");
                	imagejpeg($newThumb , $uldir . $targetnew, 90);
        	}


        	// CATEGORIES AUTO-ADJUST:
        	if (strpos($uldir, "/categories/") >0) {
        		$targetnew = "ca_".$jumbledate."_".$jumblecode.".jpg";
                	$newThumb = CroppedThumbnail($target,640,240,"expand");
                	imagejpeg($newThumb , $uldir . $targetnew, 90);
        	}

        	// PRODUCTS AUTO-ADJUST (with thumbnail generation):
        	if (strpos($uldir, "/products/") >0) {
        		$targetnew = "pr_".$jumbledate."_".$jumblecode.".jpg";

		    	// the adjusted image (large) -- assume 3:2 aspect ratio
                	$newThumb = CroppedThumbnail($target,800,600,"expand");
                	imagejpeg($newThumb , $uldir . "" . $targetnew, 85);

			// the thumbnail with a "tn_" in front -- assume 3:2 aspect ratio
                	$newThumb = CroppedThumbnail($target,200,150,"expand");
                	imagejpeg($newThumb , $uldir . "tn_" . $targetnew, 85);
			// and do the chmod for this guy too...
			chmod($uldir . "tn_" . $targetnew, 0604);

			// delete the original uppie
			unlink($uldir . $_FILES['uploaded']['name']);
        	}


        	// SIDEBARS AUTO-ADJUST:
        	if (strpos($uldir, "/sidebars/") >0) {
        		$targetnew = "sb_".$jumbledate."_".$jumblecode.".jpg";
                	$newThumb = CroppedThumbnail($target,600,480,"expand");
                	imagejpeg($newThumb , $uldir . $targetnew, 80);
        	}
        
        	// PAGES AUTO-ADJUST (with thumbnail generation):
        	if (strpos($uldir, "/pages/") >0) {
        		$targetnew = "pg_".$jumbledate."_".$jumblecode.".jpg";

			// the adjusted image (large)
                	$newThumb = CroppedThumbnail($target,320,300,"expand");
                	imagejpeg($newThumb , $uldir . "" . $targetnew, 80);

			// the thumbnail with a "lrg_" in front
                	$newThumb = CroppedThumbnail($target,640,600,"expand");
                	imagejpeg($newThumb , $uldir . "lrg_" . $targetnew, 80);
			// and do the chmod for this guy too...
			chmod($uldir . "tn_" . $targetnew, 0604);

			// delete the original uppie
			unlink($uldir . $_FILES['uploaded']['name']);
						
        	}

        	// GALLERY AUTO-ADJUST (with thumbnail generation):
        	if (strpos($uldir, "/gallery/") >0) {
        		$targetnew = "ga_".$jumbledate."_".$jumblecode.".jpg";

			// the adjusted image (large) -- assume 3:2 aspect ratio
                	$newThumb = CroppedThumbnail($target,900,600,"expand");
                	imagejpeg($newThumb , $uldir . "" . $targetnew, 85);

			// the thumbnail with a "tn_" in front -- assume 3:2 aspect ratio
                	$newThumb = CroppedThumbnail($target,300,200,"expand");
                	imagejpeg($newThumb , $uldir . "tn_" . $targetnew, 85);
			// and do the chmod for this guy too...
			chmod($uldir . "tn_" . $targetnew, 0604);

			// delete the original uppie
			unlink($uldir . $_FILES['uploaded']['name']);
        	}

    	} else {

		}

    	// now, try to CHMOD it to 604
    	chmod($uldir . $targetnew, 0604);
        ?>
        <form name="backtohost" method="post" action="http://<? echo $_GET['host']; ?>/cm/upload/remote_upload_key_end.asp">
        <input type="hidden" name="filename" value="<? echo $targetnew ?>">
        <input type="hidden" name="field" value="<? echo $_GET['field']; ?>">
        <input type="hidden" name="filesize" value="<? echo $_FILES['uploaded']['size'] ?>">
        </form>
        <script language="javascript">
        <!--
         	setTimeout("document.backtohost.submit();",500);
        // -->
        </script>
        <?		
  } else {
  	echo "Sorry, there was a problem uploading your file.";
  }
}
?>
