0

I create a thumbnail of an image create by user's phone with php on sql server, but the thumb result rotate by 90°.

this is the code:

function generateThumbnail($imgk, $width, $height, $quality)
{
    if (is_file($imgk)) {
        $imagick = new Imagick(realpath($imgk));
        $imagick->setImageFormat('jpeg');
        $imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
        $imagick->setImageCompressionQuality($quality);
        $imagick->thumbnailImage($width, $height, false, false);
        $filename_no_ext = reset(explode('.', $imgk));
        if (file_put_contents($filename_no_ext . '_thumb' . '.jpg', $imagick) === false) {
            throw new Exception("Could not put contents.");
        }
        return true;
    }
    else {
        throw new Exception("No valid image provided with {$imgk}.");
    }
}

the result are like this (in most of image):

Original image

Thumbnail

Maybe the exif data is the problem but I don't have idea for solution.

steguozzo
  • 55
  • 9

1 Answers1

1
    $orientation = $imagick->getImageOrientation();
    switch ($orientation) {
        case 8:
            $imagick->rotateimage("#000", -90);
            break;
        case 3:
            $imagick->rotateimage("#000", 180);
            break;
        case 6:
            $imagick->rotateimage("#000", 90);
            break;
    }

The files save the information about device rotation in EXIF. This can be obtained from getImageOrientation function. This code help to rotate images according to device rotation.

Rolling
  • 53
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 11:16