Jump to content

Problem upload+resize script


arbitter

Recommended Posts

So, I need to have a script that makes images with a height more than 1000 pixels to get shrunk to a height of 1000 pixels ,and images smaller than 1000 pixels to just be stored. The problem is that the images smaller than 1000 pixels don't get stored at all. Also the maximum width isn't really good, seen that if the images has a bigger height than width, the image gets resized to 1000 pixels height instead of width.

 

And yes, this script is from another site, tried to adjust it to my wantings but it still isn't quite right.

<?php
if (!isset($_SESSION["email"])){
$tekst = "

	<table width='*' height='*'  align='center' valign='center' border='0' bgcolor='white' cellpadding='20'><tr><td>
	<center><h2>U bent nog niet ingelogd.</h2>
	Om een foto te uploaden, dien je ingelogd te zijn.<br>
	U kunt <a href=\"login.php\">hier inloggen</a>.
	</td></tr></table>";
echo($tekst);
include("footer.php");
exit();
}

if(isset($_POST['submit'])){
$upload_dir = 'uploads/' . date("F") . date("y");
if(!file_exists($upload_dir)) {
	mkdir($upload_dir);
}		

list($width, $height) = getimagesize($_FILES['vImage']['tmp_name']);
$path_thumbs = $upload_dir;		

//the new width of the resized image, in pixels.
$img_thumb_width = 1000; // 
$extlimit = "yes"; 

//Limit allowed extensions? (no for all extensions allowed)
//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");		

//the image -> variables
$file_type = $_FILES['vImage']['type'];
$file_name = $_FILES['vImage']['name'];
$file_size = $_FILES['vImage']['size'];
$file_tmp = $_FILES['vImage']['tmp_name'];

//check if you have selected a file.

if(!is_uploaded_file($file_tmp)){
echo $begintabel2 . "<center><font color='red'>Selecteer een bestand!</font><br><br><a href=\"upload.php\">Terug.</a></center>" . $eindtabel;
include('footer.php');
exit(); //exit the script and don't process the rest of it!
}

//check the file's extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);

//uh-oh! the file extension is not allowed!

if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo $begintabel2 . "<center><font color='red'>Verkeerd bestandstype.</font><br><br><a href=\"upload.php\">Terug.</a></center>" . $begintabel;
exit();

}

//so, whats the file's extension?
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());


//the new width variable

$ThumbWidth = $img_thumb_width;



/////////////////////////////////

// CREATE THE THUMBNAIL //

////////////////////////////////
if($width <1000){
		$imagesave = $_upload_dir . $file_name;
		move_uploaded_file($file_tmp, $imagesave);

}
else{





//keep image type

if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
	$new_img = imagecreatefromjpeg($file_tmp);
	}
elseif($file_type == "image/x-png" || $file_type == "image/png"){
	$new_img = imagecreatefrompng($file_tmp);
	}
elseif($file_type == "image/gif"){
	$new_img = imagecreatefromgif($file_tmp);
	}



//calculate the image ratio

$imgratio=$width/$height;

if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}
else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}

//function for resize image.

if (function_exists(imagecreatetruecolor)){

$resized_img = imagecreatetruecolor($newwidth,$newheight);

}else{

die("$begintabel2 <font color='red'>Mislukt.</font> Contacteer de administrator voor verdere informatie. $eindtabel");

}

//the resizing is going on here!

imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//finally, save the image

ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");

ImageDestroy ($resized_img);

ImageDestroy ($new_img);
}
//ok copy the finished file to the thumbnail directory

//

//move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext");

/*

Don't want to copy it to a separate directory?

Want to just display the image to the user?

Follow the following steps:

2. Uncomment this code:
/*
/* UNCOMMENT THIS IF YOU WANT */
//echo "IMG:<img src=\"$path_big/$rand_name.$file_ext\" />";
//exit();
//*/
//and you should be set!
//success message, redirect to main page.		

echo $begintabel2 . "<center><font color='green'>Uw bestand is succesvol opgeslagen!</font><br><br><a href='upload.php'>Nog een foto uploaden</a><br><a href='upload/uploads.php'>Foto's</a></center>" . $eindtabel;


include('footer.php');
exit();


}
}else{

//the upload form
echo $begintabel2 . "
<form action=\"$_SERVER[php_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n
<b>Kies een bestand:</b><br><br>\n<input type=\"file\" name=\"vImage\" />\n<br><br>
<input type=\"submit\" name=\"submit\" value=\"Uploaden\" />" . $eindtabel;
}

include("footer.php");?>

 

 

 

so this is the if the width is smaller than 1000px part:

 

if($width <1000){
		$imagesave = $_upload_dir . $file_name;
		move_uploaded_file($file_tmp, $imagesave);

}

 

The rest is kind of everywhere...

Hope i get some help, so i can finally introdice my file to everyone!

 

Link to comment
Share on other sites

And yet another question;

 

It sais to 'change the dorectory of the file' in the end;

move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext");

though I uncommented this. This is because the file is already in the good folder ($upload_dir). But is this then a temporary file and will it be deleted, or am I allowed to let it be like this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.