I am developing a web application that involves the feature of uploading image to server. I am using Laravel. The problem is when I upload a photo, Laravel is rotating the image.
This is my code to upload image file.
$request->file('image_file')->store('images');
Just one line of code for uploading the image.
I uploaded this image.
Then, the photo is rotated on the server and becomes like this. I display the image in the HTML tag.
So, what is wrong. How can I stop the photo from being rotated?
Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.
Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it. That process reconstructs the image along the requested dimensions.
When referring to an image or image editor, rotate is a feature that lets you turn an image in a clockwise or counterclockwise direction. For example, many editors allow you to rotate images 90, 180, or 270.
rotate left: Shift + Ctrl + R or [ rotate right: Ctrl + R or ]
This happen when you capture the image with Mobile camera.
you can see the image data using exif_read_data()
But if you want to store it in an orignal way you can use intervention/image
package.
and use orientate() to change it. Here is an example
$img = \Image::make($request->file('image_file')->getRealpath());
$img->orientate();
But if you dont want to use the Package
you can try
$exif = exif_read_data($request->file('image_file'));
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;
}
}
Hope this helps.
I had this issue when converting HTML to PDF using Snappy PDF/Image Wrapper for Laravel
wkhtmltopdf
and the below solution is what worked for me:
/**
* @param \Illuminate\Http\UploadedFile $filename
*/
function correctImageOrientation($filename)
{
if (function_exists('exif_read_data')) {
$exif = exif_read_data($filename);
if ($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if ($orientation != 1) {
$img = imagecreatefromjpeg($filename);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img, $deg, 0);
}
// then rewrite the rotated image back to the disk as $filename
imagejpeg($img, $filename->getPath() . DIRECTORY_SEPARATOR . $filename->getFilename(), 100);
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
}
$fileInputs = $request->file();
/** @var UploadedFile $file */
$file = $fileInputs['value'];
correctImageOrientation($file);
// then save the image
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