从 iPhone 等移动设备上传图片时,图片通常会横向显示,除非直接在 Chrome 中访问图片。
从我在这里读到的内容来看,这与 Chrome 等浏览器会忽略的图像 exif 方向数据有关。
修复此类问题的解决方案是什么?
有一些额外的代码可以根据方向数据多次旋转它并重新保存吗?
const upload = multer({ storage: multer.diskStorage({
destination: function (req, file, cb) {
cb(null, avatar_path);
},
filename: function (req, file, cb) {
var ext = require('path').extname(file.originalname);
ext = ext.length>1 ? ext : "." + require('mime').extension(file.mimetype);
require('crypto').pseudoRandomBytes(16, function (err, raw) {
cb(null, (err ? undefined : raw.toString('hex') ) + ext);
});
}
})});
app.post('/upload', upload.single('avatar'), function(req,res) {
.....
});
最佳答案
我认为情况正好相反 - 图像在 EXIF 数据(拍摄时由 iPhone 的传感器存储)中携带有关其方向的信息,Chrome 会读取它并相应地进行调整。
您需要找到并解释这些数据,您的算法也将能够正确地旋转它们。参见示例 iOS Image Orientation has Strange Behavior了解更多信息。
关于javascript - 由于 exif 数据,从 iPhone 上传的图像显示在侧面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408977/