Image - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Image.php

  1. <?php
  2. $error = "";
  3. $success = "";
  4. $outputFilePath = "";
  5.  
  6. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  7.     $target_dir = "uploads/";
  8.     $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  9.     $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
  10.     $newFileType = $_POST['fileType'];
  11.  
  12.     // Check if file is an actual image
  13.     $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  14.     if ($check === false) {
  15.         $error = "File is not an image.";
  16.     }
  17.  
  18.     // Allow certain file formats
  19.     $allowedFormats = ["jpg", "jpeg", "png", "gif"];
  20.     if (!in_array($imageFileType, $allowedFormats)) {
  21.         $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  22.     }
  23.  
  24.     if (empty($error)) {
  25.         // Check if file already exists
  26.         if (!file_exists($target_dir)) {
  27.             mkdir($target_dir, 0777, true);
  28.         }
  29.  
  30.         if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  31.             $outputFilePath = $target_dir . basename($_FILES["fileToUpload"]["name"], ".$imageFileType") . ".$newFileType";
  32.             
  33.             switch ($newFileType) {
  34.                 case 'jpg':
  35.                 case 'jpeg':
  36.                     $image = imagecreatefromstring(file_get_contents($target_file));
  37.                     imagejpeg($image, $outputFilePath);
  38.                     break;
  39.                 case 'png':
  40.                     $image = imagecreatefromstring(file_get_contents($target_file));
  41.                     imagepng($image, $outputFilePath);
  42.                     break;
  43.                 case 'gif':
  44.                     $image = imagecreatefromstring(file_get_contents($target_file));
  45.                     imagegif($image, $outputFilePath);
  46.                     break;
  47.                 default:
  48.                     $error = "Conversion to the selected file format is not supported.";
  49.             }
  50.             if (empty($error)) {
  51.                 $success = "The file " . htmlspecialchars(basename($_FILES["fileToUpload"]["name"])) . " has been converted to " . strtoupper($newFileType) . ".";
  52.             }
  53.         } else {
  54.             $error = "Sorry, there was an error uploading your file.";
  55.         }
  56.     }
  57. }
  58. ?>
  59.  
  60. <!DOCTYPE html>
  61. <html lang="en">
  62. <head>
  63.     <meta charset="UTF-8">
  64.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  65.     <title>Image Converter Tool</title>
  66.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
  67.     <style>
  68.         body {
  69.             background-color: #f8f9fa;
  70.             color: #343a40;
  71.         }
  72.         .btn-submit, .btn-back {
  73.             background-color: #007bff;
  74.             color: white;
  75.         }
  76.         .alert-danger {
  77.             color: red;
  78.         }
  79.     </style>
  80. </head>
  81. <body>
  82.     <div class="container mt-5">
  83.         <h1 class="text-center">Image Converter Tool</h1>
  84.         <p class="text-center">Upload an image to convert it to a different format.</p>
  85.         <?php if (!empty($error)): ?>
  86.             <div class="alert alert-danger">
  87.                 <?php echo htmlspecialchars($error); ?>
  88.             </div>
  89.         <?php endif; ?>
  90.         <?php if (!empty($success)): ?>
  91.             <div class="alert alert-success">
  92.                 <?php echo htmlspecialchars($success); ?>
  93.             </div>
  94.             <div class="text-center">
  95.                 <a href="<?php echo $outputFilePath; ?>" class="btn btn-primary" download>Download Converted Image</a>
  96.             </div>
  97.         <?php endif; ?>
  98.         <form action="index.php" method="POST" enctype="multipart/form-data">
  99.             <div class="form-group">
  100.                 <label for="fileToUpload">Select image to upload:</label>
  101.                 <input type="file" class="form-control" id="fileToUpload" name="fileToUpload" required>
  102.             </div>
  103.             <div class="form-group">
  104.                 <label for="fileType">Convert to:</label>
  105.                 <select class="form-control" id="fileType" name="fileType">
  106.                     <option value="jpg">JPG</option>
  107.                     <option value="png">PNG</option>
  108.                     <option value="gif">GIF</option>
  109.                 </select>
  110.             </div>
  111.             <button type="submit" class="btn btn-submit btn-block">Convert</button>
  112.         </form>
  113.     </div>
  114.  
  115.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  116.     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  117.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  118. </body>
  119. </html>
  120.  
File Description
  • Image
  • PHP Code
  • 01 Jul-2024
  • 4.63 Kb
You can Share it: