1

I am facing problem with the resize,Image orientation get changed on resize (rotated left to right).

Original Image size: 2320px × 3088px,

Resize: 279px x 349px

Resize code:

        $imageFactory = $this->_imageFactory->create();
        $imageFactory->open($absolutePath);
        $imageFactory->quality(100);
        $imageFactory->constrainOnly('true');
        $imageFactory->keepAspectRatio('true');
        $imageFactory->keepFrame('true');
        $imageFactory->keepTransparency('true');
        $imageFactory->backgroundColor([255, 255, 255]);
        $imageFactory->resize($width, $height);
        $imageFactory->save($imageResized);
    

thanks

0

Hope this will help someone, I have created a function which will change the orientation of image and save in the same directory.

$absolutePath = $this->_mediaDirectory->getAbsolutePath() . $image;
$this->__image_orientate($absolutePath);

public function __image_orientate($source, $quality = 90, $destination = null)

{

    if ($destination === null) {
        $destination = $source;
        
    }
    
        $info = getimagesize($source);
    
        if ($info['mime'] === 'image/jpeg') {
            $exif = exif_read_data($source);
            if (!empty($exif['Orientation']) && in_array($exif['Orientation'], [2, 3, 4, 5, 6, 7, 8])) {
                $image = imagecreatefromjpeg($source);
                if (in_array($exif['Orientation'], [3, 4])) {
                    $image = imagerotate($image, 180, 0);
                }
                if (in_array($exif['Orientation'], [5, 6])) {
                    $image = imagerotate($image, -90, 0);
                }
                if (in_array($exif['Orientation'], [7, 8])) {
                    $image = imagerotate($image, 90, 0);
                }
                if (in_array($exif['Orientation'], [2, 5, 7, 4])) {
                    imageflip($image, IMG_FLIP_HORIZONTAL);
                }
                imagejpeg($image, $destination, $quality);
            }
        }
     return true;
 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.