I have developed a laravel web application which has a function of accepting images uploaded by users and then display it. I had encountered a problem while testing as photos uploaded using mobile phones were rotating 90 degrees in anti clock wise direction I used image intervention to solve that issue . But as i am showing a preview of the uploaded image to the users using javascript the images are rotated 90 degrees but when i save the image it becomes proper. My javascript code is
function imagePreview(input,elm) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$(elm).css("background-image","url('"+e.target.result+"')");
}
reader.readAsDataURL(input.files[0]);
}
}
$("#settings_img").on("change",function(){
imagePreview(this,"#settings_img_elm");
});
can anyone please help me to properly orient the preview image by editing the code above so that the orientation of the image changes when needed.
Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the “up” orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata.
Syntax: transform: rotate(90deg);
How about just rotating the image with CSS?
reader.onload = function (e) {
$(elm).css({
"background-image":"url('"+e.target.result+"')",
"transform": "rotate(90deg)"
});
}
Edit: Well, after a brief dive into the world of EXIF data, I think I might have a solution for you. I borrowed Ali's getOrientation() function, as @Nick suggested, and then I just corrected the orientation with CSS as follows:
function correctOrientation(element, orientation){
switch (orientation) {
case 2: $(element).css("transform", "scaleX(-1)");
break;
case 3: $(element).css("transform", "rotate(180deg)");
break;
case 4: $(element).css("transform", "rotate(180deg) scaleX(-1)");
break;
case 5: $(element).css("transform", "rotate(-90deg) scaleX(-1)");
break;
case 6: $(element).css("transform", "rotate(90deg)");
break;
case 7: $(element).css("transform", "rotate(90deg) scaleX(-1)");
break;
case 8: $(element).css("transform", "rotate(-90deg)");
break;
default: break;
}
}
fiddle
And if you need example files to test it, get them here.
Edit: Placing the uploaded image in an <img>
instead of the div's background-image:
https://jsfiddle.net/ynzvtLe2/2/
This should work. Here I get the original orientation of the image file and then build out a canvas with appropriate rotation correction. We then set the correctly rotated image as preview.
function imagePreview(input,elm) {
// image file
var file = input.files[0];
if (!file.type.match('image/jpeg.*')) {
// image is not a jpeg, do something?
}
var reader = new FileReader();
reader.onload = function(e) {
var exif = piexif.load(e.target.result);
var image = new Image();
image.onload = function () {
//get original orientation of the uploaded image
var orientation = exif["0th"][piexif.ImageIFD.Orientation];
// Build a temperory canvas to manipulate image to required orientation
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
var x = 0;
var y = 0;
ctx.save();
if (orientation == 2) {
x = -canvas.width;
ctx.scale(-1, 1);
} else if (orientation == 3) {
x = -canvas.width;
y = -canvas.height;
ctx.scale(-1, -1);
} else if (orientation == 4) {
y = -canvas.height;
ctx.scale(1, -1);
} else if (orientation == 5) {
canvas.width = image.height;
canvas.height = image.width;
ctx.translate(canvas.width, canvas.height / canvas.width);
ctx.rotate(Math.PI / 2);
y = -canvas.width;
ctx.scale(1, -1);
} else if (orientation == 6) {
canvas.width = image.height;
canvas.height = image.width;
ctx.translate(canvas.width, canvas.height / canvas.width);
ctx.rotate(Math.PI / 2);
} else if (orientation == 7) {
canvas.width = image.height;
canvas.height = image.width;
ctx.translate(canvas.width, canvas.height / canvas.width);
ctx.rotate(Math.PI / 2);
x = -canvas.height;
ctx.scale(-1, 1);
} else if (orientation == 8) {
canvas.width = image.height;
canvas.height = image.width;
ctx.translate(canvas.width, canvas.height / canvas.width);
ctx.rotate(Math.PI / 2);
x = -canvas.height;
y = -canvas.width;
ctx.scale(-1, -1);
}
ctx.drawImage(image, x, y);
ctx.restore();
var dataURL = canvas.toDataURL("image/jpeg", 1.0);
// set the preview image to your element
$(elm).css("background-image","url('"+dataURL+"')");
};
image.src = e.target.result;
};
reader.readAsDataURL(file);
}
$("#settings_img").on("change",function(){
imagePreview(this,"#settings_img_elm");
});
Make sure to include this library first: https://github.com/hMatoba/piexifjs
I got help from the documentation: https://readthedocs.org/projects/piexif/downloads/pdf/latest/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With