Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resizing images.

Status
Not open for further replies.

Chelsea7

Programmer
Aug 25, 2008
69
US
Hi I have a question about resizing images and uploading them to a server. The code below works. However, it also copies the original file to the root directory. I only want the thumb nails files uploaded in a directory call 'upload', which it already does. But the original file is going to the root directory, after the user clicks the submit button on the form.

<?php

if(isset($_POST['submit']))

{


$path_thumbs = "/upload/";

$img_thumb_width = 400; //

$extlimit = "yes";

$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

$file_type = $_FILES['vImage']['type'];

$file_name = $_FILES['vImage']['name'];

$file_size = $_FILES['vImage']['size'];

$file_tmp = $_FILES['vImage']['tmp_name'];

if(!is_uploaded_file($file_tmp)){

echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

exit(); //exit the script and don't process the rest of it!

}

$ext = strrchr($file_name,'.');

$ext = strtolower($ext);

if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {

echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

exit();

}

$getExt = explode ('.', $file_name);

$file_ext = $getExt[count($getExt)-1];

//create a random file name

$rand_name = md5(time());

$rand_name= rand(0,999999999);

//the new width variable

$ThumbWidth = $img_thumb_width;



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

// CREATE THE THUMBNAIL //

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



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);

}

list($width, $height) = getimagesize($file_tmp);


$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("Error: Please make sure you have GD library ver 2+");

}



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



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

ImageDestroy ($resized_img);

ImageDestroy ($new_img);


}



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



$msg = urlencode("$title was uploaded! <a href=\"Resize.php\">Upload More?</a>");

header("Location: Resize.php?msg=$msg");

exit();





}else{


if(isset($_GET['msg']))

{

//but decode it first!

echo "<p>".urldecode($_GET['msg'])."</p>";

}


echo "

<form action=\"$_SERVER[PHP_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n

<p>File:<input type=\"file\" name=\"vImage\" /></p>\n

<p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>";

}



?>

Any assistance would be appreciated.

Chelsea
 
To delete a file using PHP see unlink

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
uploaded files should normally be stored in your tmp system directory where file perms should permit the php process to delete files itself on unload. i do not think it a good idea for php (or any external process) to have write access to your system root. check your php.ini and change the setting accordingly. remember to restart the web server afterwards.
 
I got it work. Yes, php process to delete the files itself. I have it pointing to the wrong directory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top