I am creating a script for an ajax image upload using PHP with Laravel 5.4. Now I have an issue here is after image upload to a server. When an image is shown in a browser, an image is coming rotated with 90 or some degrees.

Images are coming in 180 rotation or 90 degree rotation if an image is uploaded from iPhone or iPad Device.

How should I do a rotation of an image with relevant degrees?

If you have any relevant rotation examples available for in PHP, please share

Bhaskar Monitor Asked on February 17, 2018 in Programming.
Add Comment
  • 1 Answer(s)

    As you are doing image upload and getting the issue for image rotate that I can feel is you are trying to upload an image from REST API.
    Image rotate issue is coming in php in below cases: 

    • Html crop image is not used in front end ( In case of web)
    • Image orientation is not checked ( Applicable if image cropping is not used)

    Here I will tell you about how to check image orientation, rotating images and save it.

    
    
    public function imageOrientaionUpload(Request $request) {
        $publicPath = public_path('uploads_directory/');
        if($request->file('imgFile') == null) {
            echo 'No File Exist';
        }
        $file = $request->file('imgFile');
        // generating unique name for uploaded file
        $uniq = uniqid().time();
        $uploadFile = $uniq.'.'.$file->getClientOriginalExtension();
        // getting file path
        $filePath = $file->getPathName();
        // image rotation code in php starts here
        $exif = @exif_read_data($filePath);
        if(isset($exif['Orientation'])) {
            $orientation = $exif['Orientation'];
            if (isset($orientation) && $orientation != 1){
                switch ($orientation) {
                    case 3:
                       // 180 degree image rotation
                        $deg = 180;
                        break;
                    case 6:
                       // 270 degree image rotation
                        $deg = 270;
                        break;
                    case 8:
                       // 90 degree image rotation
                        $deg = 90;
                        break;
                    }
                if ($deg) {
                    // If uploaded image is php
                    if ($file->getClientOriginalExtension() == "png") {
                        $img_new = imagecreatefrompng($filePath);
                        $img_new = imagerotate($img_new, $deg, 0);
                        // Save rotated image
                        imagepng($img_new,$filePath);
                        } else {
                            $img_new = imagecreatefromjpeg($filePath);
                            $img_new = imagerotate($img_new, $deg, 0);
                            // Save rotated image
                            imagejpeg($img_new,$filePath,80);
                        }
                    }
                }
            }
        // image rotation code in php ends here
        // image compression start
        $destinationPath = $publicPath.$uploadFile;
        // checking file extenstion in php
        $getFileExtn = strtolower($file->getClientOriginalExtension());
        $imageExtnArr = ['jpeg','jpg','png','bmp'];
        if(!in_array($getFileExtn, $imageExtnArr)){
            echo 'Image File format not supported';
        }
        // save roatate image
        $img->save($destinationPath,70);
        echo 'failed to upload image';
    }
    Bhaskar Monitor Answered on February 17, 2018.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.