php-gallery/upload.php

94 lines
2.9 KiB
PHP
Raw Normal View History

2022-07-21 14:53:04 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>UwU</title>
<link rel="stylesheet" href="css/master.css">
<link href="https://fonts.googleapis.com/css2?family=Rubik" rel="stylesheet">
</head>
<body>
2022-07-23 07:44:43 +00:00
<?php include("ui/header.php"); ?>
2022-07-21 14:53:04 +00:00
<div class="upload-root">
2022-07-23 09:37:12 +00:00
<h2 class="space-bottom">Upload image</h2>
2022-07-21 19:41:01 +00:00
<form class="flex-down between" method="POST" action="upload.php" enctype="multipart/form-data">
2022-07-23 09:37:12 +00:00
<input class="btn alert-default space-bottom" type="file" name="image" placeholder="select image UwU">
<input class="btn alert-default space-bottom" type="text" name="alt" placeholder="Description/Alt for image">
2022-07-21 18:42:03 +00:00
<button class="btn alert-default" type="submit" name="upload">Upload Image</button>
2022-07-21 14:53:04 +00:00
</form>
<?php
if ($_GET["r"] == "success") {
2022-07-23 07:44:43 +00:00
// Image uploaded
2022-07-22 00:21:48 +00:00
echo "<p class='alert alert-high space-top'>Your Image uploaded successfully!</p>";
2022-07-23 07:44:43 +00:00
} elseif ($_GET["r"] == "fail") {
// Upload failed
2022-07-22 00:21:48 +00:00
echo "<p class='alert alert-low space-top'>F, Upload failed</p>";
2022-07-23 07:44:43 +00:00
} elseif ($_GET["r"] == "nofile") {
// No file was present
2022-07-22 00:21:48 +00:00
echo "<p class='alert alert-default space-top'>No file lol</p>";
2022-07-23 07:44:43 +00:00
} else {
2022-07-21 22:14:56 +00:00
// echo "<p class='alert alert-default'>Select an image to upload</p>";
2022-07-21 14:53:04 +00:00
}
?>
</div>
<?php
2022-07-23 07:44:43 +00:00
include_once("ui/conn.php");
2022-07-21 14:53:04 +00:00
if (isset($_POST['upload'])) {
// Get image name
$get_image_name = $_FILES['image']['name'];
2022-07-21 19:41:01 +00:00
// Get alt text
2022-07-23 07:44:43 +00:00
if (empty($_POST['alt'])) {
2022-07-21 19:41:01 +00:00
$get_alt_text = "No description provided";
2022-07-23 07:44:43 +00:00
} else {
$get_alt_text = $_POST['alt'];
2022-07-21 19:41:01 +00:00
}
2022-07-21 14:53:04 +00:00
// If image present, continue
2022-07-23 07:44:43 +00:00
if (!empty($get_image_name)) {
2022-07-21 14:53:04 +00:00
// Set file path for image upload
$image_basename = basename($get_image_name);
$image_path = "images/".$image_basename;
2022-07-21 19:41:01 +00:00
$sql = "INSERT INTO swag_table (imagename, alt) VALUES ('$get_image_name','$get_alt_text')";
2022-07-21 14:53:04 +00:00
// Uploading image to Table
mysqli_query($conn, $sql);
// Checking if image uploaded
if (move_uploaded_file($_FILES['image']['tmp_name'], $image_path)) {
// Make thumbnail
$image_thumbnail = new Imagick($image_path);
2022-07-22 17:09:50 +00:00
// Get image format
$image_format = $image_thumbnail->getImageFormat();
// If image is gif
if ($image_format == 'GIF') {
$image_thumbnail = $image_thumbnail->coalesceImages();
}
2022-07-23 07:44:43 +00:00
// Resize image
2022-07-22 17:34:22 +00:00
$image_thumbnail->resizeImage(300,null,null,1,null);
2022-07-22 17:09:50 +00:00
// Save image
$image_thumbnail->writeImage("images/thumbnails/".$image_basename);
2022-07-21 14:53:04 +00:00
header("Location:upload.php?r=success");
2022-07-23 07:44:43 +00:00
} else {
// Could not move images to folder
2022-07-21 14:53:04 +00:00
header("Location:upload.php?r=fail");
}
2022-07-23 07:44:43 +00:00
} else {
2022-07-21 14:53:04 +00:00
// No image present
header("Location:upload.php?r=nofile");
}
}
?>
2022-07-23 07:44:43 +00:00
<?php include("ui/footer.php"); ?>
2022-07-21 14:53:04 +00:00
</body>
</html>