简体   繁体   English

我提供的图像不正确吗,它们都显示为旋转90度

[英]Am I serving my images incorrect, they are all shown rotated 90 degrees

I've once found this code to serve images from my server to the client: 我曾经发现以下代码可以将服务器中的图像提供给客户端:

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

When I run this php file via the browser (like call this script in the addressbar of the browser), a portrait image shows portrait. 当我通过浏览器运行此php文件时(例如在浏览器的地址栏中调用此脚本),将显示一个纵向图像。 But when I run this in an HTML file (I set the src of an img element dynamically) all portrait images are shown landscape (like rotated 90 degrees). 但是,当我在HTML文件中运行此文件时(我动态设置了img元素的src ),所有肖像图像都显示为横向(例如旋转90度)。

Should I include something in the response(-headers) that the image is landscape or portrait? 我是否应该在响应(标题)中包含图像为横向或纵向的内容?

This is the way I load the image in the html: 这是我在html中加载图像的方式:

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

This is what the request looks like when it's called from my html page and when the image shows correct: 当从我的html页面调用请求并且图像显示正确时,请求的外观如下:

在此处输入图片说明

And this is the incorrect version 这是不正确的版本 在此处输入图片说明

I can see the headers differ, but does that make a difference? 我可以看到标题不同,但这有区别吗? (besides I would know how to set the headers when I set the image source) (此外,在设置图像源时,我会知道如何设置标题)

One thing I also noticed was that in both cases, when I rightclick and save the image, the filename is image.jfi which I think is a weird extension? 我还注意到的一件事是,在两种情况下,当我右键单击并保存图像时,文件名都是image.jfi ,我认为这是一个奇怪的扩展名吗?

Orientation was setted in Exif. 方向已在Exif中设置。 Picture wasn't rotated phisicaly. 图片没有旋转。 Image-viewer can to work with it but the browser in tag doesn't rotate it. 图像查看器可以使用它,但是标记中的浏览器不会旋转它。

Your can rotate pictures on server by imagemagick --auto-orient http://imagemagick.org/Usage/photos/#orient 您可以通过imagemagick --auto-orient http://imagemagick.org/Usage/photos/#orient在服务器上旋转图片

You can also rotate it "on fly". 您也可以“动态”旋转它。 Just get Exif info by exif_read_data() and rotate it if it has 3(180degree), 6(90CW), or 8(-90CCW) in 'Orientation' 只需通过exif_read_data()获取Exif信息,然后在“方向”中旋转3(180degree),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,0);
            break;
    }
    imagejpeg($rotate);
    imagedestroy($source);
    imagedestroy($rotate);
} else {
    imagejpeg($source);
    imagedestroy($source);
}

But of course better to prepare all pictures once. 但是当然最好一次准备所有图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM