我使用 PHP 脚本上传图像,然后使用 PHP 的 getImageSize() 获取尺寸,然后根据图片方向(纵向或横向)对图像执行操作。
但是(PHP 版本 5.4.12)在 some .jpg 文件上它会按原样获取高度和宽度,而在 some(用 iPhone 拍摄)中它会交换它们,认为肖像照片实际上是风景。
它不仅发生在我的本地 Wamp 服务器上,而且也发生在远程服务器上(具有不同的 PHP 版本)。
有谁知道怎么做
1) 修复此问题或
2)找到解决问题的方法吗?
某些相机在文件本身的元数据部分中包含方向标签。这样,无论原始数据中图片的方向如何,设备本身每次都可以以正确的方向显示它。
Windows 似乎不支持读取此方向标签,而只是读取像素数据并按原样显示。
解决方案是在每个图像的基础上更改受影响图片元数据中的方向标签,或者
使用 PHP 的
exif_read_data()
函数读取方向并相应地调整图像方向,如下所示:
<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>
参考资料:
功能
getimagesize()
更改横向(水平)照片的宽度和高度。
您可以使用此代码:
<?php
$img = "test.jpg";
$exif = exif_read_data($img);
if(empty($exif['Orientation'])) {
list($width, $height, $type, $attr) = getimagesize($img);
}else{
list($height, $width, $type, $attr) = getimagesize($img);
}
?>
但在 PHP7 及以上版本已自动修复。
我使用的是 PHP 8.1.2-1ubuntu2.14。我有一张 Windows 属性正确报告的肖像图像:宽度 3648 像素,高度 5472 像素。
php $imagesize = getimagesize() 报告:$imagesize[1] 3648,$imagesize[0] 5472,$imagesize[3] 宽度=5472 高度=3548。
似乎 php getimagesize 正在反转大分辨率文件的高度和宽度。有人有解决办法吗