我希望我的用户能够上传照片。然后,我想要显示压缩图像以加载更快的图像并在单击压缩图像时显示全尺寸图像。 为此,我想我必须上传两张不同的照片。这是我上传未压缩的文件的方式:
if(!$_FILES['fileToUpload']['error'] == UPLOAD_ERR_NO_FILE){
$target_dir = "uploads/";
$name = md5(session_id().microtime()) . basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir . $name;//basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "<br />Image détectée - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "<br />Le fichier n'est pas une image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<br />Le fichier existe déjà, renommez le et réessayez.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "<br />L'image est trop lourde, compressez-la ou réduisez sa taille.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "<br />Seul les formats JPG, JPEG, PNG & GIF sont autorisés.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<br />Le fichier n'a pas été importé.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<br />L'image a été importée.";
$req = $bdd->prepare('INSERT INTO spots(nom, description, img1, latitude, longitude, pseudoCreateur, idCreateur) VALUES(?, ?, ?, ?, ?, ?, ?)');
$req->execute(array($_POST['nom'], $description, $name, $_POST['latitude'], $_POST['longitude'], $_SESSION['pseudo'], $_SESSION['id']));
$req = $bdd->prepare('SELECT points FROM users WHERE id = ?');
$req->execute(array($_SESSION['id']));
$points = $req->fetch();
$bdd->exec('UPDATE users SET points = ' . (string)($points[0] + 3) .' WHERE id = '. $_SESSION['id']);
echo "<br /><br />SPOT PUBLIÉ !";
} else {
echo "<br />Sorry, there was an error uploading your file.";
}
}
如何上传压缩图像?