Image rotated after upload php

I have a issiue when uploading a image taken using iphone. I am trying to use PHP and the imgrotate function to automaticly decide if the image needs to be rotated to the correct possition, before uploading it to the server.

My html code:

<form class="form-horizontal" method="post" enctype="multipart/form-data">  
     <div class="form-group">
        <div class="col-md-9">
            <div class="input-group">
                <span class="input-group-btn">
                    <span class="btn btn-default btn-file">
                        Choose img<input type="file" name="file" id="imgInp">
                    </span>
                </span>

            </div>
        </div>
    </div>
    <button type="submit">Send</button>
</form>

The PHP code im using: Also return a error: Warning: imagerotate() expects parameter 1 to be resource, string given in.

Anyone have a working code for this scenario?

<?php

$filename = $_FILES['file']['name'];
$exif = exif_read_data($_FILES['file']['tmp_name']);


 if (!empty($exif['Orientation'])) {
        switch ($exif['Orientation']) {
            case 3:
                $image = imagerotate($filename, -180, 0);
                break;
            case 6:
                $image = imagerotate($filename, 90, 0);
                break;
            case 8:
                $image = imagerotate($filename, -90, 0);
                break;
        } 
    }

    imagejpeg($image, $filename, 90);

?>

September 5, 2020 Category : PHP

If you want to rotate image on upload 90 degrees or 180 degrees in php then this tutorial will help you. we will use imagecreatefrompng(), imagerotate() and imagepng() for rotate png image and save to server. same imagecreatefromjpeg(), imagerotate() and imagejpeg() for jpeg image.

we will simple use imagecreatefrompng(), imagerotate() and imagepng() gd function of php for rotate and save image.

you can see bellow example, i will put one dummy pdf image with "Df7731542705507.png" and write code of rotate, than another image will save with "myUpdateImage.png" name. i make same for jpeg image.

Image rotated after upload php

Let's check both example, make sure add one dummy image for testing..

Example for PNG:

<?php

$fileName = "Df7731542705507.png";

$degrees = 90;

$source = imagecreatefrompng($fileName);

$rotate = imagerotate($source, $degrees, 0);

imagepng($rotate, "myUpdateImage.png");

print_r('Image saved successfully.');

?>

Example for JPEG:

<?php

$fileName = "Df7731542705507.jpeg";

$degrees = 90;

$source = imagecreatefromjpeg($fileName);

$rotate = imagerotate($source, $degrees, 0);

imagejpeg($rotate, "myUpdateImage.jpeg");

print_r('Image saved successfully.');

?>

I hope it can help you....

Image rotated after upload php

Photo by Mike Fox on Unsplash. [Modified]

I recently experienced an issue on a web application I was working on where users who took photos directly from their mobile devices had it rotated 90° once it was uploaded and put in an <img> tag.

It turns out the reason for this is the EXIF (exchangeable image file format) information for orientation that is set on the images by the phone’s digital camera. The camera alters the EXIF Orientation Tag to indicate the orientation of the captured scene, which may cause the orientation to show differently in an <img> tag on your web app, depending on if the browser obeys the orientation rule or not.

Solution 1 — Reading EXIF data

One way to fix the rotation issue in PHP is to manually read the EXIF data and rotate the image accordingly. Here’s a great function created by Wes on Stack Overflow that does just this:

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, 95);
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
}

The function above uses the PHP in-built exif_read_data() function to read the Orientation EXIF data from the image, and uses imagerotate and imagejpeg to recreate the image with the correct orientation.

To use the function:

move_uploaded_file($uploadedFile, $destinationFilename);
correctImageOrientation($destinationFilename);

Solution 2— Intervention to the rescue

Another fix to the rotation issue is to use this PHP Intervention package. From their docs:

Intervention Image is an open source PHP image handling and manipulation library. It provides an easier and expressive way to create, edit, and compose images.

From the installation guide for Intervention, you can install it with Composer using this command:

php composer.phar require intervention/image

Here’s an example of how to use the orientate() method to automatically read the EXIF data and perform a rotation on the image to display the image correctly in a Laravel application:

$file = $request->file('image');
$image = \Image::make($file);
// perform orientation using intervention
$image->orientate();
$imageName = "image_name." . $file->getClientOriginalExtension();
$destinationPath = public_path("/uploads");
// save image
$image->save($destinationPath . $imageName);

Conclusion

EXIF data contains a bunch of useful information and it works great for determining the proper orientation of images before saving them.

Do you have any other tips for managing this orientation issue for uploads? Or do you use EXIF data for any other interesting thing? Let’s discuss in the comments.

Why do pictures turn sideways when I upload them?

Photos taken on smartphones, tablets and some cameras can look great on your device but appear upside down or sideways when uploaded to a post or page because the device stores the image's orientation in the EXIF metadata and not all software is able to read the metadata.

How to upload rotated image in PHP?

we will simple use imagecreatefrompng(), imagerotate() and imagepng() gd function of php for rotate and save image. you can see bellow example, i will put one dummy pdf image with "Df7731542705507. png" and write code of rotate, than another image will save with "myUpdateImage. png" name.

How do I rotate a picture and save it?

Two buttons with arrow will appear at the bottom. Select either Rotate the image 90 degrees to the left or Rotate the image 90 degrees to the right. If you want to keep the picture rotated in this way, click Save. ... Rotate a picture..

How do I rotate a image in Photoshop?

With your image open in Photoshop, go to Image > Image Rotation. 2. Select from the image rotation options — 90 degrees clockwise, 90 degrees counterclockwise, or 180 degrees.