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:
- Upload an image (html5 input) to folder: /uploads - without refresh!
- 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.
- Compress the image to folder: /compresseduploads.
- 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();
}
?>