I do not serve my images correctly, they are all shown rotated 90 degrees

I once found this code to serve images from my server to a 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 through the browser (for example, call this script in the address bar of the browser), the portrait image shows the portrait. But when I run this in an HTML file (I set the element srcelement imgdynamically), all portrait images are displayed as landscape (for example, rotated 90 degrees).

Should I include something in the answer (-headers) that the image is a landscape or a portrait?

So I load the image in html:

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

This is what the request looks like when called from my html page and when the image is displayed correctly:

enter image description here

And this is the wrong version enter image description here

, , ? ( , , , )

, , , , , image.jfi, , , ?

+4
1

​​ Exif. . Image-viewer , .

imagemagick --auto-orient http://imagemagick.org/Usage/photos/#orient

" ". Exif exif_read_data() , 3 (180 ), 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);
}

, , .

+1

Source: https://habr.com/ru/post/1694392/


All Articles