- <?php
- $error = "";
- $success = "";
- $outputFilePath = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
- $newFileType = $_POST['fileType'];
- // Check if file is an actual image
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if ($check === false) {
- $error = "File is not an image.";
- }
- // Allow certain file formats
- $allowedFormats = ["jpg", "jpeg", "png", "gif"];
- if (!in_array($imageFileType, $allowedFormats)) {
- $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- }
- if (empty($error)) {
- // Check if file already exists
- if (!file_exists($target_dir)) {
- mkdir($target_dir, 0777, true);
- }
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- $outputFilePath = $target_dir . basename($_FILES["fileToUpload"]["name"], ".$imageFileType") . ".$newFileType";
- switch ($newFileType) {
- case 'jpg':
- case 'jpeg':
- $image = imagecreatefromstring(file_get_contents($target_file));
- imagejpeg($image, $outputFilePath);
- break;
- case 'png':
- $image = imagecreatefromstring(file_get_contents($target_file));
- imagepng($image, $outputFilePath);
- break;
- case 'gif':
- $image = imagecreatefromstring(file_get_contents($target_file));
- imagegif($image, $outputFilePath);
- break;
- default:
- $error = "Conversion to the selected file format is not supported.";
- }
- if (empty($error)) {
- $success = "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been converted to " . strtoupper($newFileType) . ".";
- }
- } else {
- $error = "Sorry, there was an error uploading your file.";
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Image Converter Tool</title>
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
- <style>
- body {
- background-color: #f8f9fa;
- color: #343a40;
- }
- .btn-submit, .btn-back {
- background-color: #007bff;
- color: white;
- }
- .alert-danger {
- color: red;
- }
- </style>
- </head>
- <body>
- <div class="container mt-5">
- <h1 class="text-center">Image Converter Tool</h1>
- <p class="text-center">Upload an image to convert it to a different format.</p>
- <?php if (!empty($error)): ?>
- <div class="alert alert-danger">
- <?php echo htmlspecialchars($error); ?>
- </div>
- <?php endif; ?>
- <?php if (!empty($success)): ?>
- <div class="alert alert-success">
- <?php echo htmlspecialchars($success); ?>
- </div>
- <div class="text-center">
- <a href="<?php echo $outputFilePath; ?>" class="btn btn-primary" download>Download Converted Image</a>
- </div>
- <?php endif; ?>
- <form action="index.php" method="POST" enctype="multipart/form-data">
- <div class="form-group">
- <label for="fileToUpload">Select image to upload:</label>
- <input type="file" class="form-control" id="fileToUpload" name="fileToUpload" required>
- </div>
- <div class="form-group">
- <label for="fileType">Convert to:</label>
- <select class="form-control" id="fileType" name="fileType">
- <option value="jpg">JPG</option>
- <option value="png">PNG</option>
- <option value="gif">GIF</option>
- </select>
- </div>
- <button type="submit" class="btn btn-submit btn-block">Convert</button>
- </form>
- </div>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
- </body>
- </html>
[text] Image
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: