Hi, I have an image upload functions, it basicly works, but it does not resizes! It just uploads the original image, while I want it to upload the resized image, and delete the big one.
Heres my code:
session_start();
$username = $_SESSION['username'];
include("config.php");
$uploaddir = "../../upload/i/"; //Upload directory: needs write premissions
function generate_code($length = 10)
{
if ($length <= 0)
{
return false;
}
$code = "";
$chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
srand((double)microtime() * 1000000);
for ($i = 0; $i < $length; $i++)
{
$code = $code . substr($chars, rand() % strlen($chars), 1);
}
return $code;
}
$code = generate_code(20);
if (!is_dir($uploaddir)) {
die ("Upload directory does not exists.");
}
if (!is_writable($uploaddir)) {
die ("Upload directory is not writable.");
}
if(isset($_GET["upload"])) {
if ($_POST['cmdupload'])
{
$ip = trim($_SERVER['REMOTE_ADDR']);
if (isset($_FILES['file']))
{
if($_FILES['file']['type'] == "image/jpeg" || $_FILES['file']['type'] == "image/pjpeg" || $_FILES['file']['type'] == "image/png" || $_FILES['file']['type'] == "image/gif")
{
if ($_FILES['file']['error'] != 0)
{
switch ($_FILES['file']['error'])
{
case 1:
header("Location: ../editpicture.php?e=1");
exit;
break;
case 2:
header("Location: ../editpicture.php?e=2");
exit;
break;
case 3:
header("Location: ../editpicture.php?e=3");
exit;
break;
case 4:
header("Location: ../editpicture.php?e=4");
exit;
break;
case 6:
header("Location: ../editpicture.php?e=6");
exit;
break;
case 7:
header("Location: ../editpicture.php?e=7");
exit;
break;
case 8:
header("Location: ../editpicture.php?e=8");
exit;
break;
}
} else {
if (!file_exists($uploaddir . $_FILES["file"]["name"]))
{
// Proceed with file upload
if (is_uploaded_file($_FILES['file']['tmp_name']))
{
$file_tmp = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
if($_FILES['file']['type'] == "image/pjpeg" || $_FILES['file']['type'] == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($_FILES['file']['type'] == "image/x-png" || $_FILES['file']['type'] == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($_FILES['file']['type'] == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);
if($width < 700 || $height < 500) {
//calculate the image ratio
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
} else {
$newheight = $height;
$newwidth = $width;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
//the resizing is going on here!
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//finally, save the image
$rand_name = $code.$file_name;
if($_FILES['file']['type'] == "image/pjpeg" || $_FILES['file']['type'] == "image/jpeg"){
ImageJpeg ($resized_img,"$uploaddir/$rand_name");
}elseif($_FILES['file']['type'] == "image/x-png" || $_FILES['file']['type'] == "image/png"){
ImagePNG ($resized_img,"$uploaddir/$rand_name");
}elseif($_FILES['file']['type'] == "image/gif"){
ImageGIF ($resized_img,"$uploaddir/$rand_name");
}
ImageDestroy ($new_img);
if (move_uploaded_file($resized_img,"$uploaddir/$rand_name"))
{
ImageDestroy ($resized_img);
// uploaded file was moved and renamed succesfuly. Display a message.
header("Location: ../member.php?loginid=$username");
$link = mysql_connect($yourhost, $youruser, $yourpass);
mysql_select_db( $yourdb ) or die ( "Couldnt open $db: ".mysql_error() );
$fullupldir = "http://www.upload.website.com/i/";
$profpicture = $fullupldir . $code . $_FILES['file']['name'];
$query = "UPDATE login SET profpicture='$profpicture' WHERE username='$username'";
$result = mysql_query($query);
} else { echo "Go **** yourself!"; }
} else {
//File was NOT uploaded to the temp dir
switch ($_FILES['file']['error'])
{
case 1:
header("Location: ../editpicture.php?e=1");
break;
case 2:
header("Location: ../editpicture.php?e=2");
break;
case 3:
header("Location: ../editpicture.php?e=3");
break;
case 4:
header("Location: ../editpicture.php?e=4");
break;
case 6:
header("Location: ../editpicture.php?e=6");
break;
case 7:
header("Location: ../editpicture.php?e=7");
break;
case 8:
header("Location: ../editpicture.php?e=8");
break;
}
}
} else {
header("Location: ../editpicture.php?e=10");
unset($_FILES['file']['tmp_name']);
}
}
} else {
header("Location: ../editpicture.php?e=13");
unset($_FILES['file']['tmp_name']); }
} else {
// user did not select a file to upload
header("Location: ../editpicture.php?e=11");
}
} else {
// upload button was not pressed
header("Location: ../editpicture.php");
}
}
I tryed various changes, it just does not upload the resized image :S
Oh, yes, sorry for the ‘error message’ if it does not moved the resized image… but this is what happens when things dont work…