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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 我服务的图像不正确,它们都显示为旋转90度脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我曾经发现这段代码可以将服务器中的图像提供给客户端:

$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;

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

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

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

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

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

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

这是不正确的版本

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

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

我还注意到的一件事是,在两种情况下,当我右键单击并保存图像时,文件名是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);
}

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 我服务的图像不正确,它们都显示为旋转90度全部内容,希望文章能够帮你解决php – 我服务的图像不正确,它们都显示为旋转90度所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。