7

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"/>

2 Answers 2

11

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
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this and it almost works! But I can get the image to go into my id the image is put at the bottom of the body. When i do document.getElementById("blah").appendChild(img); nothing happens at all..
@BluGeni Try document.getElementById("blah").src = img; instead of document.body.appendChild(img);
I get [object%20HTMLCanvasElement] 404 (Not Found) with that
Ok, i just reread the doc for this feature. It seems to return a canvas while using the option orientation. So this should work: document.getElementById("blah").src = img.toDataURL();. I'm sorry that i cannot test it out myself.
@morgul after reorienting the image, how can I replace it in the input field so that the changed image gets submitted in the form?
|
-2

A great way to do it is: let the browser do it. You use CSS / HTML5 instead of Javascript.

.image_rotated_by_90 {
    transform: rotate(90deg) translateY(-100%);
    -webkit-transform: rotate(90deg) translateY(-100%);
    -ms-transform: rotate(90deg) translateY(-100%);
}

You can append the class image_rotated_by_90 dynamically to your rotated images in js. Apply it by doing

$('#my_image').addClass('image_rotated_by_90');

8 Comments

# is not a class, it's an id. You want .image_rotated_by_90.
@aoeu so i added the code to my css do i need to reference it ? or how to I make it work?
@BluGeni I added a snippet
@aoeu ok this worked! but it moved my image over to the right, because of the rotate?
This answer doesnt work because when I upload a pic not from a phone it still rotates it. I dont want to always rotate. I need to determine if it needs rotated.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.