I have an AJAX form that submits 4 images and a PHP form handler. Acceptable formats are .gif, .png, .jpg or .jpeg. Before submitting the form, I check the image file type using javascript.
Now, in PHP, I have this code:
while($x <= 4) {
$target_dir = "files/";
$target_file = $target_dir . basename($_FILES["fileUpload".$x]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["fileUpload".$x]["tmp_name"], $target_file)) {
$filename = date("d-m-Y---H-i-s---").rand(10000,99999);
$oldfile = $target_file;
$newfile = $target_dir . $filename . "." . $imageFileType;
rename($oldfile, $newfile);
$file[$x] = $filename . "." . $imageFileType;
$fileCounter++;
}
$x++;
}
Some people upload large image files, and I want to automatically resize them to the maximum width / height without cropping the image. How to do it using PHP?
source
share