Laravel Image Intervention avoid rotation
Asked Answered
A

4

10

I'm uploading an iPhone image - taken by iPhone camera in vertical - with the dimensions of 2448x3264 and because this dimensions are so high (?) when I create a thumb of 600x360 it automatically rotates to horizontal.

What have I tried without any success:

  • Change the thumb dimensions
  • Use the fit function
  • Use the resize function
  • Use the crop function
  • Use the upsize and aspectRatio methods
  • Set only the height and use null on width
  • Set only the width and use null on height

The thumb must have a maximum of height of 360 and I'm ok if the width is not 600.

$imageResize = Image::make($originalFile);
$imageResize->fit(600, 360, function ($constraint)
{
    $constraint->upsize();
});
$imageResize->save($thumbPath);

My goal is to have:

  • Thumbnails in vertical if the original photo is vertical
  • Thumbnails in horizontal if the original photo is horizontal

How can I achieve this?

Affiant answered 10/6, 2019 at 14:1 Comment(4)
Upon saving, is the original file in its original rotation? So if you manually went to the location, the file is in the correct rotation at it's default dimensions? I found some information here that may suggest Laravel is rotating it upon uploadingCrossarm
@swonder yes, the original image is saved with the default rotation/dimensions.Affiant
Could you take a look at this github issue as it seems to run inline with your question. It could be due to the fit() function.Crossarm
@swonder the orientate method solved, would you like to set up as an answer so I can accept?Affiant
C
19

As spoke before, the image is being saved in its correct orientation and at the point of resizing you are running the fit() function on which I was able to find some information on this issue running along side that which suggests you need to use orientate() with fit.

An example here:

$imageResize = Image::make($originalFile);
$imageResize->orientate()
->fit(600, 360, function ($constraint) {
    $constraint->upsize();
})
->save($thumbPath);

I'm glad this helped.

Crossarm answered 10/6, 2019 at 14:15 Comment(0)
D
8

According to this github issue you may need to run orientate() before fit():

$imageResize = Image::make($originalFile)
    ->orientate()
    ->fit(600, 360, function ($constraint) {
        $constraint->upsize();
    })
    ->save($thumbPath);
Defaulter answered 10/6, 2019 at 14:13 Comment(0)
T
2
$img = Image::make($originalFile);

$img->orientate();
$img->resize(1024, null, function($constraint){
    $constraint->upsize();
    $constraint->aspectRatio();
});
$img->save();
Turfman answered 13/6, 2021 at 12:27 Comment(1)
While your code snippet may answer the question, providing additional context regarding why and/or how your code snippet works improves its long-term value.Langdon
S
0

The solutions above will crop the image. To prevent that, you can slightly change code:

$imageResize = Image::make($originalFile);
$imageResize->orientate();
$imageResize->resize(600, null, function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
})->save($thumbPath);
Spermous answered 30/8, 2023 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.