I've been having a very difficult time trying to get images rotated automatically via PHP when I upload them. Currently, they are displaying sideways when viewed in the browser.
I've been searching for hours, and have found numerous tips and examples, but I don't quite know how to implement them.
I've also tried using the code from a commenter on the PHP manual, with no luck.
This is the code I'm referencing:
<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>
Here is the page I am working with right now. It seems to function okay, but images come out sideways. I stripped out the code from my numerous failed attempts at getting the rotation to work.
<?php
include 'includes/democonnect.php';
$cnum=$_POST['cnum'];
$amount1=$_POST['amount1'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadReceipt"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["uploadReceipt"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
$filename = 'receipt'.time() . basename($_FILES["uploadReceipt"]["name"]);
// Check file size
if ($_FILES["uploadReceipt"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "bmp" ) {
echo "Sorry, only JPG, JPEG, PNG, GIF, and BMP files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadReceipt"]["tmp_name"], $target_dir.$filename)) {
echo "The file ". $filename. " has been uploaded.";
$query = "INSERT INTO tblReceiptUpload
(cnum,pointer1,amount1)
VALUES(?,?,?)";
$params1 = array($cnum,$filename,$amount1);
$result = sqlsrv_query($conn,$query,$params1);
sqlsrv_close($conn);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Any help would be immensely appreciated!