如何调整图像的方向以显示正确的图像预览?
How to adjust the orientation of a image in order to show proper preview of the image?
我开发了一个laravel web 应用程序,它具有接受用户上传的图像然后显示它的功能。我在测试时遇到了一个问题,用手机上传的照片逆时针旋转了90度,我用图像干预解决了这个问题。但是当我使用 javascript 向用户展示上传图像的预览时,图像旋转了 90 度,但是当我保存图像时它变得正确。
我的 javascript 密码是
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");
});
任何人都可以通过编辑上面的代码帮助我正确定位预览图像,以便在需要时更改图像的方向。
用 CSS 旋转图像怎么样?
reader.onload = function (e) {
$(elm).css({
"background-image":"url('"+e.target.result+"')",
"transform": "rotate(90deg)"
});
}
编辑: 好吧,在短暂地 dive 进入 EXIF 数据的世界之后,我想我可能有适合您的解决方案。按照@Nick的建议,我借用了Ali的getOrientation()函数,然后我用CSS纠正了方向如下:
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;
}
}
如果您需要示例文件来测试它,get them here。
编辑: 将上传的图片放在 <img>
而不是 div 的背景图片中:
https://jsfiddle.net/ynzvtLe2/2/
这应该有效。在这里,我获得了图像文件的原始方向,然后构建了一个 canvas 并进行了适当的旋转校正。然后我们将正确旋转的图像设置为预览。
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");
});
确保首先包含这个库: https://github.com/hMatoba/piexifjs
我从文档中得到了帮助: https://readthedocs.org/projects/piexif/downloads/pdf/latest/
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");
});
任何人都可以通过编辑上面的代码帮助我正确定位预览图像,以便在需要时更改图像的方向。
用 CSS 旋转图像怎么样?
reader.onload = function (e) {
$(elm).css({
"background-image":"url('"+e.target.result+"')",
"transform": "rotate(90deg)"
});
}
编辑: 好吧,在短暂地 dive 进入 EXIF 数据的世界之后,我想我可能有适合您的解决方案。按照@Nick的建议,我借用了Ali的getOrientation()函数,然后我用CSS纠正了方向如下:
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;
}
}
如果您需要示例文件来测试它,get them here。
编辑: 将上传的图片放在 <img>
而不是 div 的背景图片中:
https://jsfiddle.net/ynzvtLe2/2/
这应该有效。在这里,我获得了图像文件的原始方向,然后构建了一个 canvas 并进行了适当的旋转校正。然后我们将正确旋转的图像设置为预览。
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");
});
确保首先包含这个库: https://github.com/hMatoba/piexifjs
我从文档中得到了帮助: https://readthedocs.org/projects/piexif/downloads/pdf/latest/