Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rotate image if needed with javascript

Tags:

javascript

I have a preview spot to show a preview of the image before uploading, the problem is when you choose an image from your phone it appears sideways. How can I rotate the image if it needs to be rotated?

my javascript to show the preview:

<script type="text/javascript">

 function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah')
                        .attr('src', e.target.result)

                };

                reader.readAsDataURL(input.files[0]);
            }
        }

</script>

and the html

<img src="images/Your_Picture_Here.png" alt="" title="" class="prod_image" id = "blah"/>
like image 817
BluGeni Avatar asked Feb 17 '14 20:02

BluGeni


People also ask

How do you rotate an image in JavaScript?

When you need to rotate images using JavaScript, you need to add the transform: rotate(x) CSS style to the image element you want to rotate. To rotate the image, you can select the element using document. querySelector('#img') and then append the . style.

How do you rotate an object in JavaScript?

Introduction to JavaScript rotate() canvas APIThe rotate() method allows you to rotate a drawing object on the canvas. The rotate() method accepts a rotation angle in radians. If the angle is positive, the rotation is clockwise. In case the angle is negative, the rotation is counterclockwise.

How do I rotate an image in HTML?

Syntax: transform: rotate(90deg);

How do I rotate an image in base64?

To rotate a base 64 image by X degrees and return new base64 image with JavaScript, we can put the image in a canvas. Then we do the rotation in the canvas and return the image. to add an img element. to define the rotateBase64Image function to rotate the base64data image by 45 degrees.


1 Answers

I would suggest you to take a look at the github project called JavaScript-Load-Image. Every thing is there to help you out with your orientation problem. There is an online demo that you can see here.

You could use code as follow:

document.getElementById('file-input').onchange = function (e) {
    var loadingImage = loadImage(
        e.target.files[0],
        function (img) {
            document.getElementById("blah").src = img.toDataURL();
        },
        {orientation: 1}
    );
    if (!loadingImage) {
        // Alternative code ...
    }
};

Please note the option orientation: 1 to make your image well rotated.

There is 8 valid values in EXIF for the orientation, see below the letter F for the 8 different value:

1       2       3       4       5           6           7           8

000000  000000      00  00      0000000000  00                  00  0000000000
00          00      00  00      00  00      00  00          00  00      00  00
0000      0000    0000  0000    00          0000000000  0000000000          00
00          00      00  00
00          00  000000  000000
like image 174
service-paradis Avatar answered Nov 07 '22 23:11

service-paradis