dwdata 0 Light Poster

I have an upload page which allows a user to SELECT a file from their hard drive, then UPLOAD it to ftp folder, and then store the path of the image to mySQL db.

It functions just fine except BUT we want to guard against large file being saved. If the dimensions are greater that width=100 or Height=80, we want to proportionately scale the image down to a thumbnail size. The logic for sizing is in the code and working but I don't know how to convert the temp image and save THAT to the ftp. There are a few things in there that I was messing around with. Here is the code:

<?

require_once('auth.php');
	session_start();
	include("core.php");

//print_r($_POST);

$step = $_GET['step'];

$error = "";

if($_POST["action"] == "Upload Image")
{
if(!isset($_FILES['image_file']))
$error["image_file"] = "Error: An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "Error: Please select an image file from you hard drive first.";

if(!$error)
{
$content = file_get_contents($_FILES['image_file']['tmp_name']);
list($width, $height, $imtype, $attr) = getimagesize($_FILES['image_file']['tmp_name']);


            if ($imtype == 3) // cheking image type
                $ext="png";   // to use it later in HTTP headers
            elseif ($imtype == 2)
                $ext="jpg";
            elseif ($imtype == 1)
                $ext="gif";
            else
                $error["image_file"] = "Error: unknown file format (images should be .jpg, .gif, or .png files) or<br>image was not found on your hard drive.";
				
		
if(empty($error)) {

$newimage = "m_images/" . $_SESSION['SESS_MEMBER_ID'].".".$ext;

// Resample

list($width, $height, $imtype, $attr) = getimagesize($content);

If($width > 100 or $height > 80) {
 	If($width > $height) {
 
 	$w=floor($width/($width/100));
 	$h=floor($height/($width/100));
 
 	}else{
 
 	$w=floor($width/($height/80));
 	$h=floor($height/($height/80));
	}
	//$width = $w;
 	//$height = $h;
//echo $width."____".$height;
}
@move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
$image = imagecreatefromjpeg($newimage);



//$image_p = imagecreatetruecolor($w, $h);

$test = imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width, $height);
print_r($test);
//imagejpeg($image_p, $newimage);


$result = mysql_query("UPDATE `Members` SET `Picture`='$newimage' WHERE `AUCCIIM_ID`='".$_SESSION['SESS_MEMBER_ID']."'");
if($step == 1) {
//header("location: m2ViewDetails.php?message=".urlencode("Image updated successfully...")."");
}else{
//header("location: mViewDetails.php?message=".urlencode("Image updated successfully...")."");
}
}
}
}

?>
<style type="text/css">
<!--
.style1 {
	color: #EE0000;
	font-weight: bold;
}
-->
</style>


Select the BROWSE button to choose an image from your hard drive.<br />Then select the UPLOAD IMAGE button to save the image to you information record.
<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?=$_SERVER["PHP_SELF"];?>">
<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action">
  <br />
  <br />
  <span class="style1">
  <?
//print_r($error);
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>
</span></p>
</form>

I appreciate if someone could tweak my code so it will work and I can educate myself with the logic.

Thanks ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.