How do you Upload, Resize, and Save files with PHP?

myscranton

New member
I have this code I wrote in PHP which allows a user to upload an image. It then scales it so it is 250px wide. It for the most part works fine. It uploads the picture and if you use imagejpeg() it will show it at reduced size on the screen.

BUT, it saves it at its normal size. I want it to be saved at the same size it is displayed on the screen. What is wrong?

if (isset($_FILES["file"])){
$imagename = $_FILES["file"]["name"];
$src = $_FILES["file"]["tmp_name"];
$destination = ($_SERVER['DOCUMENT_ROOT'] )."/uploads/images/".$imagename;
move_uploaded_file($src, $destination);

$file = ($_SERVER['DOCUMENT_ROOT'] )."/uploads/images/".$_FILES["file"]
["name"];

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

$new_width = "250";
$new_height = ($height/$width)*$new_width;

$new_file = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($file);

imagecopyresampled($new_file, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagejpeg($new_file, ($_SERVER['DOCUMENT_ROOT'] )."/uploads/images/".$_FILES["file"]
["name"]);
}
 
Back
Top