Хорошо, у меня есть этот код для загрузки фотографий на мою страницу:
$filename = uniqid() . "_" . time(); // 5dab1961e93a7_1571494241
$extension = pathinfo( $_FILES["post_image"]["name"], PATHINFO_EXTENSION ); // jpg
$basename = $filename . '.' . $extension; // 5dab1961e93a7_1571494241.jpg
$target_dir = "../postimages/";
$target_file = $target_dir . $basename;
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$file_name = $_FILES['post_image']['tmp_name'];
$maxDim = 1080;
if($_FILES['post_image']['tmp_name']=='') {
$image = "noimage.png";
} else {$check = getimagesize($_FILES["post_image"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["post_image"]["size"] > 5000000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
list($width, $height, $type, $attr) = getimagesize( $file_name );
if ( $width > $maxDim || $height > $maxDim ) {
$target_filename = $file_name;
$ratio = $width/$height;
if( $ratio > 1) {
$new_width = $maxDim;
$new_height = $maxDim/$ratio;
} else {
$new_width = $maxDim*$ratio;
$new_height = $maxDim;
}
$src = imagecreatefromstring( file_get_contents( $file_name ) );
$dst = imagecreatetruecolor( $new_width, $new_height );
imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagedestroy( $src );
imagepng( $dst, $target_filename ); // adjust format as needed
imagedestroy( $dst );
move_uploaded_file($_FILES["post_image"]["tmp_name"], $target_file);
} else {
move_uploaded_file($_FILES["post_image"]["tmp_name"], $target_file);
}
}
}
Этот код идеально подходит для загрузки одной фотографии в одну папку, но я хотел бы загрузить одну копию фотографии, но с меньшие размеры для папки thumbnail . Кто-нибудь знает, как я это сделаю?