Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel is rotating the image when uploaded

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.

enter image description here

Then, the photo is rotated on the server and becomes like this. I display the image in the HTML tag. enter image description here

So, what is wrong. How can I stop the photo from being rotated?

like image 313
Wai Yan Hein Avatar asked May 10 '18 16:05

Wai Yan Hein


People also ask

Why is my photo Rotating when I upload it?

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.

How do you fix a rotated image?

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.

What does rotating photos mean?

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.

What command rotates images?

rotate left: Shift + Ctrl + R or [ rotate right: Ctrl + R or ]


2 Answers

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.

like image 165
Romantic Dev Avatar answered Sep 24 '22 08:09

Romantic Dev


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
like image 33
Waqleh Avatar answered Sep 24 '22 08:09

Waqleh