PHP read_exif_data and Adjust Orientation
The documentation for imagerotate refers to a different type for the first parameter than you use:
An image resource, returned by one of the image creation functions,
such as imagecreatetruecolor().
Here is a small example for using this function:
function resample($jpgFile, $thumbFile, $width, $orientation) {
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($jpgFile);
$height = (int) (($width / $width_orig) * $height_orig);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($jpgFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Fix Orientation
switch($orientation) {
case 3:
$image_p = imagerotate($image_p, 180, 0);
break;
case 6:
$image_p = imagerotate($image_p, 90, 0);
break;
case 8:
$image_p = imagerotate($image_p, -90, 0);
break;
}
// Output
imagejpeg($image_p, $thumbFile, 90);
}
PHP Upload with EXIF Orientation based rotation
Try this. We're taking the file, and if it's a jpeg then we rotate it. If not, we don't. We take the $image
variable that is created and generate a jpeg in the location you wanted.
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;
$info = $check;
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($_FILES["uploadReceipt"]["tmp_name"]);
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($_FILES["uploadReceipt"]["tmp_name"]);
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($_FILES["uploadReceipt"]["tmp_name"]);
else exit;//Do whatever you want here.
if($info['mime'] == 'image/jpeg') {
$exif = exif_read_data($_FILES["uploadReceipt"]["tmp_name"]);
if(isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
}
}
if(isset($orientation)) {
switch($orientation) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
}
//////////
// $image is your new, rotated file.
//////////
} 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 || !isset($image)) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (imagejpeg($image, $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.";
}
}
Related Topics
Check If PHP-Page Is Accessed from an iOS Device
Php: Multiple SQL Queries in One MySQL_Query Statement
Uploading in Codeigniter - the Filetype You Are Attempting to Upload Is Not Allowed
Using MySQLi from Another Class in PHP
How to Alias a Table in Laravel Eloquent Queries (Or Using Query Builder)
How to Sort an Array of Utf-8 Strings
Utf-8 Safe Equivalent of Ord or Charcodeat() in PHP
Get Last Insert Id After a Prepared Insert with Pdo
How to Use Objects from Other Namespaces and How to Import Namespaces in PHP
How to Install/Enable the PHP Phar Extension
How to Chain Method on a Newly Created Object
Can You Put PHP Inside PHP with Echo
Jquery Ajax Settimeout After Form Succes Not Redirecting
Php7.1 JSON_Encode() Float Issue