I'm creating a mobile app that allows people to upload photos, but I have a problem resizing the images with the Image Manipulator Class you can find here https://gist.github.com/philBrown/880506.
My problem is that when I take a photo by keeping my phone in portrait mode, the image is being rotated 90 degrees to the right. When I take a photo in landscape mode, everything goes fine.
Here's my cropping PHP code:
$manipulator = new ImageManipulator($_FILES['Filedata']['tmp_name']);
$width = $manipulator->getWidth();
$height = $manipulator->getHeight();
$centreX = round($width / 2);
$centreY = round($height / 2);
// our dimensions will be 600x450
$x1 = $centreX - 300; // 600 / 2
$y1 = $centreY - 225; // 450 / 2
$x2 = $centreX + 300; // 600 / 2
$y2 = $centreY + 225; // 450 / 2
// center cropping to 600x450
$newImage = $manipulator->crop($x1, $y1, $x2, $y2);
// rotate 90 degrees to the right
$imageResource = $newImage->getResource();
$angle = '90'; // in degrees
$fileName = "../file_upload_img/43/" . $random_file_name . $_FILES["Filedata"]["name"];
$rotatedImage = imagerotate($imageResource, $angle);
imagejpeg($rotatedImage, $fileName, 95);
// saving file to uploads folder
$manipulator->save("../file_upload_img/43/" . $random_file_name . $_FILES["Filedata"]["name"]);
How can I make sure that photos taken in portrait mode don't get rotated when cropped?
EDIT*
The upload.php code:
require_once(__DIR__ . '/../includes/ImageManipulator.php');
require_once(__DIR__ . '/../init/db/conn.php');
ob_start();
echo "<pre>";
print_r($_FILES);
print_r($_GET);
print_r($_POST);
echo "</pre>";
print_r("file type: " . $_FILES["Filedata"]["type"]);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["Filedata"]["name"]);
$extension = end($temp);
if ((($_FILES["Filedata"]["type"] == "image/gif")
|| ($_FILES["Filedata"]["type"] == "image/jpeg")
|| ($_FILES["Filedata"]["type"] == "image/jpg")
|| ($_FILES["Filedata"]["type"] == "image/pjpeg")
|| ($_FILES["Filedata"]["type"] == "image/x-png")
|| ($_FILES["Filedata"]["type"] == "image/png"))
&& ($_FILES["Filedata"]["size"] < 9999999999)
&& in_array($extension, $allowedExts)) {
if ($_FILES["Filedata"]["error"] > 0) {
echo "Return Code: " . $_FILES["Filedata"]["error"] . "<br>";
}
else {
echo "Upload: " . $_FILES["Filedata"]["name"] . "<br>";
echo "Type: " . $_FILES["Filedata"]["type"] . "<br>";
echo "Size: " . ($_FILES["Filedata"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["Filedata"]["tmp_name"] . "<br>";
if (file_exists("../file_upload_img/" . $_FILES["Filedata"]["name"])) {
echo $_FILES["Filedata"]["name"] . " already exists. ";
}
else {
$random_file_name = sha1(uniqid($_FILES["Filedata"]["name"]));
/*
* ROTATE, CROP AND RESIZE
*/
// 4:3 Ratio version
$manipulator = new ImageManipulator($_FILES['Filedata']['tmp_name']);
// Resize image to 600px wide and 450px high
$manipulator = $manipulator->resample(600, 450);
// saving file to uploads folder
$manipulator->save("../file_upload_img/43/" . $random_file_name . $_FILES["Filedata"]["name"]);
$width = $manipulator->getWidth();
$height = $manipulator->getHeight();
$centreX = round($width / 2);
$centreY = round($height / 2);
// our dimensions will be 600x450
$x1 = $centreX - 300; // 600 / 2
$y1 = $centreY - 225; // 450 / 2
$x2 = $centreX + 300; // 600 / 2
$y2 = $centreY + 225; // 450 / 2
// center cropping to 200x130
$newImage = $manipulator->crop($x1, $y1, $x2, $y2);
// rotate 90 degrees to the right
$imageResource = $newImage->getResource();
$angle = '90'; // in degrees
$fileName = "../file_upload_img/43/" . $random_file_name . $_FILES["Filedata"]["name"];
$rotatedImage = imagerotate($imageResource, $angle);
imagejpeg($rotatedImage, $fileName, 95);
// saving file to uploads folder
$manipulator->save("../file_upload_img/43/" . $random_file_name . $_FILES["Filedata"]["name"]);
// 640:120 Ratio version
$manipulator_640 = new ImageManipulator($_FILES['Filedata']['tmp_name']);
// Resize image to 640px wide and 120px high
$manipulator_640 = $manipulator_640->resample(640, 120);
// saving file to uploads folder
$manipulator_640->save("../file_upload_img/header/" . $random_file_name . $_FILES["Filedata"]["name"]);
$width_640 = $manipulator_640->getWidth();
$height_640 = $manipulator_640->getHeight();
$centreX_640 = round($width_640 / 2);
$centreY_640 = round($height_640 / 2);
// our dimensions will be 640x120
$x1_640 = $centreX_640 - 320; // 640 / 2
$y1_640 = $centreY_640 - 60; // 120 / 2
$x2_640 = $centreX_640 + 320; // 640 / 2
$y2_640 = $centreY_640 + 60; // 120 / 2
// center cropping to 640x120
$newImage_640 = $manipulator_640->crop($x1_640, $y1_640, $x2_640, $y2_640);
// saving file to uploads folder
$manipulator_640->save("../file_upload_img/header/" . $random_file_name . $_FILES["Filedata"]["name"]);
/*
*****************
*/
}
}
}
else {
echo "Invalid file";
}
$data=ob_get_clean();
EDIT 2
I've have added the code from user @matewka to my PHP. I've edited my PHP with the rotating code included. The code doesn't rotate the images at all.
Fix the image orientation using exif_read_data
function orientation($path){
$image = imagecreatefromjpeg($path);
$exif = exif_read_data($path);
$orientation = $exif['COMPUTED']['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;
}
imagejpeg($image, $path);
}
There may be 4 possible orientations to be detected [Here]
Regarding your recent comment:
I just realized that the phone itself has already rotated the image after I took it. How can I rotate recently taken images by my phone so they look normal?
to rotate an image in PHP you can use imagerotate function. So, right after you cropped the image and saved it to the $newImage
variable, proceed with those operations:
$imageResource = $newImage->getResource();
$angle = '90'; // in degrees
$fileName = "../file_upload_img/43/" . $random_file_name . $_FILES["Filedata"]["name"];
$rotatedImage = imagerotate($imageResource, $angle);
imagejpeg($rotatedImage, $fileName, 95);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With