Hi, I have been using this tutorial and it works great.
But there is one issue: when I upload the pictures from my laptop, any portrait picture becomes landscape. Landscape is landscape.
On the other hand, when I use the computer, it prints correctly in the screen.
The images are uploaded correctly if I look at the height px and width px. (portrait will have larger height px at DB whether I use laptop or computer). But it will print in screen as landscape both on computer and laptop when I uploaded the same portrait picture from the laptop (I a talking about the same pic). When uploaded the same portrait pic from computer, it will show portrait on both computer and laptop...
Do you have any suggestion?
I am about to have a rotate feature so this may be fixed. The problem is I am new to PHP...
Any help will be greatly appreciated.
Thank you.

Dani AI

Generated

This is almost always the EXIF “Orientation” flag rather than the pixel dimensions. Camera/phone firmware often writes an Orientation tag instead of physically rotating the pixel data; some viewers/browsers honor that tag and some do not, so the exact same file can look correct on one machine and sideways on another (this is what was getting at and what ’s width/height checks will show — the pixel dims can be correct while the viewer still ignores EXIF). See the browser/CSS note on image orientation. ()

Server-side fix (recommended): read the uploaded file’s EXIF Orientation and normalize the image at upload time so the saved file pixels are upright and the EXIF tag is set to “top-left” (or stripped). In PHP you can read EXIF with exif_read_data() and then rotate/flip using GD (imagerotate() and imageflip()) or with Imagick. Example (GD, JPEG-only):

<?php
function fix_jpeg_orientation($file) {
    if (!function_exists('exif_read_data')) return false;
    $exif = @exif_read_data($file);
    if (empty($exif['Orientation'])) return false;
    $img = imagecreatefromjpeg($file);
    switch ($exif['Orientation']) {
        case 2: if (function_exists('imageflip')) imageflip($img, IMG_FLIP_HORIZONTAL); break;
        case 3: $img = imagerotate($img, 180, 0); break;
        case 4: if (function_exists('imageflip')) imageflip($img, IMG_FLIP_VERTICAL); break;
        case 5: if (function_exists('imageflip')) imageflip($img, IMG_FLIP_HORIZONTAL); $img = imagerotate($img, 270, 0); break;
        case 6: $img = imagerotate($img, 270, 0); break;
        case 7: if (function_exists('imageflip')) imageflip($img, IMG_FLIP_HORIZONTAL); $img = imagerotate($img, 90, 0); break;
        case 8: $img = imagerotate($img, 90, 0); break;
    }
    imagejpeg($img, $file, 90);
    imagedestroy($img);
    return true;
}
?>

Reading and saving EXIF is done with PHP’s EXIF functions. For the GD transforms above see the exif_read_data() and imagerotate() docs; imageflip() handles mirrored cases. (php.net)

If you use Imagick you can call the built-in rotate/flip methods or (in newer releases) autoOrient(); otherwise read the orientation, call rotateImage() / flip methods, then call setImageOrientation(Imagick::ORIENTATION_TOPLEFT) before writing the file so downstream viewers don’t try to re-orient. (pecl.php.net)

Notes: rotate at upload time (not in the browser) for consistent display; rotating/re-saving JPEGs recompresses (use a lossless tool like jpegtran if you need bit-for-bit lossless rotation). If you only need a quick client-side fix, modern browsers support image-orientation: from-image (but support/behavior varies), so server-side normalization is the most robust solution. ()

Recommended Answers

All 3 Replies

see the example

It has to do with the image orientation stored in the EXIF data.
camera's should add this data to the jpg but that is not standard.
Also windows xp ignores and removes this data.
the link gave but now unbroken:
http://php.net/manual/en/imagick.getimageorientation.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.