图片在上传时自动旋转
我正在尝试上传 base64 编码图像并在解码后保存.图片正在上传和保存,我可以使用 URL 和所有内容访问它..但是图片逆时针旋转了 90 度,我不知道为什么!!
I'm trying to upload a base64 encoded image and save after decoding it. Image is getting uploaded and saved, and I can access it using a URL and everything..but the image gets rotated by 90 degrees anti-clockwise and I have no idea WHY!!
我得到编码数据的地方很好,因为把它放在 <img/>
工作正常!
The place where I get the encoded data is fine, as putting it in <img />
works fine!
function saveImageData($base64Data) {
$base64_decoded = base64_decode($base64Data);
$im = imagecreatefromstring($base64_decoded);
if ($im !== false) {
$imagepath = '/public/uploads/' . time() . '.jpg';
imagejpeg($im, $imagepath);
chmod($imagepath, 0755);
imagedestroy($im);
} else {
return false;
}
return $imagepath;
}
我没有使用任何旋转功能,但它仍然在旋转.我可以使用像 imagerotate 这样的 PHP GD 函数,但不想因为黑色背景等原因.
I'm not using any rotation functions, but still its getting rotated. I can use a PHP GD function like imagerotate, but don't want to for reasons like black backgrounds etc.
如果你能帮上忙……你是最棒的人!!
If you can help..you r the awesomest person!!
推荐答案
我的猜测是您上传的图像实际上是旋转的,但正在被纠正,因为它的 EXIF 部分包含旋转数据.(例如,相机在拍照时可能会感知纵向,并将该信息保存在 EXIF 数据中;某些查看软件会感知旋转数据,并在查看照片时会自动旋转照片.)在这种情况下,图像可能只是出现为您旋转,具体取决于您用于查看它的内容.
My guess would be that the image you're uploading is actually rotated, but is being corrected because it contains rotation data in its EXIF section. (For example, a camera may be aware of portrait orientation when taking a photo, and save that information in the EXIF data; some viewing software is aware of the rotation data and will auto-rotate the photo when viewing it.) In which case, the image might just be appearing to be rotated for you, depending on what you're using to view it.
您是否使用同一软件查看上传前"和上传后"图像?例如,如果您使用相同的网络浏览器查看它们会发生什么?
Are you viewing the "before upload" and "after upload" images using the same software? What happens if you look at them both using the same web browser, say?
如果您尝试不同的图片,最好是来自不同来源的图片,会发生什么情况?你有任何软件可以让你查看图像的 EXIF 数据吗?寻找方向"值;1"以外的任何内容都表示为图像设置了旋转(请参阅 此页面 以获得体面的描述.)
What happens if you try a different image, preferably from a different source? Do you have any software that will allow you to view the image's EXIF data? Look for the "Orientation" value; anything other than "1" means there's a rotation set for the image (see this page for a decent description.)
因此,总而言之,我会说 JPEG 文件中的底层图像处于错误"方向,并且 EXIF 数据包含用于纠正显示旋转的信息.如果源是,例如,刚刚玩过我的 iPhone,这很可能会以横向存储其底层图像数据,但如果图像是实际拍摄的,则设置 EXIF 数据(因此应该显示) 作为肖像.
So, in summary, I'd say that that the underlying image in the JPEG file is at the "wrong" orientation, and the EXIF data contains information to correct that rotation for display. This is very likely if the source is, for example, an iPhone, which having just played with mine, seems to store its underlying image data in landscape orientation, but set the EXIF data if the image was actually taken (and should therefore be displayed) as portrait.
修复它的最佳方法几乎肯定是在上传后检查文件上的 EXIF 数据,使用 PHP EXIF 函数 并根据需要旋转图像以纠正方向,然后再保存您自己的副本.
Best way to fix it is almost certainly to examine the EXIF data on the file after upload, using the PHP EXIF functions and rotate the image as necessary to correct the orientation before saving your own copy.
相关文章