加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 我服务的图像不正确,它们都显示为旋转90度

发布时间:2020-12-13 16:10:20 所属栏目:PHP教程 来源:网络整理
导读:我曾经发现这段代码可以将服务器中的图像提供给客户端: $filename = $_GET["filename"];if ($filename == null || strlen($filename) 1){ return null;}$fp = fopen($filename,'rb');// send the right headersheader("Content-Type: image/jpeg");header("
我曾经发现这段代码可以将服务器中的图像提供给客户端:

$filename = $_GET["filename"];
if ($filename == null || strlen($filename) < 1){
    return null;
}

$fp = fopen($filename,'rb');

// send the right headers
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($filename));

// dump the picture and stop the script
fpassthru($fp);
exit;

当我通过浏览器运行这个php文件时(比如在浏览器的地址栏中调用此脚本),肖像图像显示肖像.
但是当我在HTML文件中运行它时(我动态设置了img元素的src)所有肖像图像都显示为横向(如旋转90度).

我应该在响应(-headers)中包含图像是横向还是纵向的内容?

这是我在html中加载图像的方式:

document.getElementById('next').src = "image.php?filename=" + data;

这是从我的html页面调用并且图像显示正确时请求的样子:

enter image description here

这是不正确的版本

enter image description here

我可以看到标题不同,但这有什么不同? (除了我知道如何在设置图像源时设置标题)

我还注意到的一件事是,在两种情况下,当我右键单击并保存图像时,文件名是image.jfi,我认为这是一个奇怪的扩展?

解决方法

在Exif中设置了方向.图片没有旋转phisicaly.
图像查看器可以使用它,但标签中的浏览器不会旋转它.

您可以通过imagemagick –auto-orient http://imagemagick.org/Usage/photos/#orient在服务器上旋转图片

你也可以“飞行”旋转它.只需通过exif_read_data()获取Exif信息,如果在’Orientation’中有3(180deg),6(90CW)或8(-90CCW),则旋转它

// dump the picture and stop the script
$source = imagecreatefromjpeg($filename);
$exif = exif_read_data($filename);
if (isset($exif['Orientation'])) {
   switch($exif['Orientation']) {
        case 3: // 180 degree
            $rotate=imagerotate($source,180,0);
            break;
        case 6: // 90 CW
            $rotate=imagerotate($source,-90,0);
            break;
        case 8: // 90 CCW
            $rotate=imagerotate($source,90,0);
            break;
        default:
            $rotate=imagerotate($source,0);
            break;
    }
    imagejpeg($rotate);
    imagedestroy($source);
    imagedestroy($rotate);
} else {
    imagejpeg($source);
    imagedestroy($source);
}

但当然最好准备一次所有图片.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读