0

I have some issues with my upload.php code.

It works fine with PHP 7.4. Only Notice is: "Undefined index: submitbtn". However in PHP 8.0 it turns to a Warning: "Undefined array key "submitbtn", and the code does not execute.

Basically the code does:

  1. Upload an image (html5 input) to folder: /uploads - without refresh!
  2. Rotate the image if needed (needs to be vertical) - because some capture with mobile-phone and the photo gets horizontal aligned when uploaded for some reason.
  3. Compress the image to folder: /compresseduploads.
  4. Echo' some Javascript to exucute (a way for me to get the new /compresseduploads image path).

I really hope you can help me get it compatible with the new PHP version. Thanks :-)

(I dont think imagerotate() works with PHP 8.1 yet - therefore I am aiming for PHP 8.0.)

pssstt.. any other improvements is also welcome as I am very new to PHP.

CODE:

Index.php (input)

<form class="uploadform" method="post" enctype="multipart/form-data"
action="/wp-content/plugins/Painting_Preview/upload.php" accept="image/*" capture="camera" />
<input type="file" id="imgInp" class="span10" value="Capture photo" name="imagefile" />
<input type="submit" value="Submit" name="submitbtn" id="submitbtn">
</form>

Upload.php (compress, rotate, upload and echo js)

<?php
$file_formats = array("jpg", "jpeg", "png", "gif", "bmp"); // Set File format
$filepath = "uploads/";

if ($_POST['submitbtn']=="Submit") {
  $name = $_FILES['imagefile']['name'];
  $size = $_FILES['imagefile']['size'];

   if (strlen($name)) {
      $extension = substr($name, strrpos($name, '.')+1);
      if (in_array($extension, $file_formats)) {
          if ($size < 1500000000) {
             $imagename = md5(uniqid().time()).".".$extension;
             $tmp = $_FILES['imagefile']['tmp_name'];
             if (move_uploaded_file($tmp, $filepath . $imagename)) {

                echo 'THIS IS JUST THE JAVASCRIPT....
                <script>jQuery(document).ready(function(.....</script>';

    function compress($source, $destination, $quality) {
        $info = getimagesize($source);
        if ($info['mime'] == 'image/jpeg') {
            $image = imagecreatefromjpeg($source);

            ////FIX ROTATE///


# Get exif information
$exif = exif_read_data($source);
# Add some error handling

# Get orientation
$orientation = $exif['Orientation'];

# Manipulate image
switch ($orientation) {
    case 2:
        imageflip($image, IMG_FLIP_HORIZONTAL);
        break;
    case 3:
        $imageObject = imagerotate($image, 180, 0);
        break;
    case 4:
        imageflip($image, IMG_FLIP_VERTICAL);
        break;
    case 5:
        $imageObject = imagerotate($image, -90, 0);
        imageflip($image, IMG_FLIP_HORIZONTAL);
        break;
    case 6:
        $image = imagerotate($image, -90, 0);
        break;
    case 7:
        $image = imagerotate($imageObject, 90, 0);
        imageflip($image, IMG_FLIP_HORIZONTAL);
        break;
    case 8:
        $image = imagerotate($imageObject, 90, 0);
        break;
}
}


    elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source);
    elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source);
    imagejpeg($image, $destination, $quality);
    return $destination;
}
$src = 'uploads/'.$imagename;
$dest = 'compresseduploads/'.$imagename;
compress($src, $dest, $quality=30);
    } else {
                   echo "Error. Please try again.";
         }
      } else {
                   echo "Error - Your image exceeds 15 MB.";

      }
       } else {
           echo "Invalid file format - please use .jpg or .png.";
       }
  } else {
          echo ".....";
  }


exit();
}

?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
jharring
  • 13
  • 2
  • Button values aren't sent automatically when you submit with AJAX, only when the form is submitted normally. – Barmar Jan 12 '22 at 17:49
  • 1
    The JavaScript code needs to add the `submitbtn` parameter to the request. – Barmar Jan 12 '22 at 17:50
  • An alternative that I personally prefer is to not rely on the button itself being pressed, but the [checking if the request is a POST](https://stackoverflow.com/a/359050/231316). If one file handles multiple forms, however, this isn't usually a good choice. – Chris Haas Jan 12 '22 at 17:57
  • Yes i figured this and tried to use if(isset($_POST['upload'])) instead of if ($_POST['submitbtn']=="Submit") ... it gets rid of the "Warning: "Undefined array key "submitbtn" Error. However the the image is not getting upload and the javascript is not being printet – jharring Jan 12 '22 at 17:59
  • Ups. Sorry Chris. Guess you meant: if ($_SERVER['REQUEST_METHOD'] === 'POST') {. - Just tried this. However it now throws this Notice (and i guess warning in php 8.0): Notice: Undefined index: Orientation in /wp-content/plugins/Painting_Preview/upload.php on line 111 – jharring Jan 12 '22 at 18:02
  • Jep. In PHP 8.0 it throws: Undefined array key "Orientation" in ...content/plugins/Painting_Preview/upload.php on line 111 when using ($_SERVER['REQUEST_METHOD'] === 'POST') – jharring Jan 12 '22 at 18:06
  • It was always an error regardless of being reported as a Warning or an Error. – RiggsFolly Jan 12 '22 at 18:06
  • You always have to check that a form has been submitted before attempting to use the values that would be sent if the form was submitted. Because the first time you load a form, from a link or redirect of course the form has NOT been submitted but the PHP is still run – RiggsFolly Jan 12 '22 at 18:08
  • 1
    True.. guess i know need to post a new question "How flip image orientation on upload compatible with PHP 8.0" :-) – jharring Jan 12 '22 at 18:10
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 12 '22 at 18:19
  • Its never a good idea defining functions inside a IF, its legal, but it will likely as not catch you out, as the function wont exist if you dont enter the IF – RiggsFolly Jan 12 '22 at 18:21
  • Also, there's a programming pattern called "returning early" that I personally like, and it helps avoid deeply nested control structures, specifically `if` statements. The general idea is to check if a condition _is not_ valid, and then do something and return/exit. That way an `else` isn't needed. I'm not saying you must use this, just that it generally makes code much easier to read and debug. At the end of your code, it is almost impossible to tell what your dangling `else` statements map to, and even if they were indented properly, you'd have to scan a lot of code to figure it out. – Chris Haas Jan 12 '22 at 18:33
  • Thanks a lot. I've always been using a lot of IF and ELSEIF ... always thought it looked a bit ugly too – jharring Jan 12 '22 at 18:36

0 Answers0