8

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?

Linesofcode
  • 5,327
  • 13
  • 62
  • 116
  • 1
    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 uploading](https://stackoverflow.com/questions/50277697/laravel-is-rotating-the-image-when-uploaded) –  Jun 10 '19 at 14:04
  • 1
    @swonder yes, the original image is saved with the default rotation/dimensions. – Linesofcode Jun 10 '19 at 14:09
  • 1
    Could you take a look at [this github issue](https://github.com/Intervention/image/issues/461) as it seems to run inline with your question. It could be due to the `fit()` function. –  Jun 10 '19 at 14:12
  • 1
    @swonder the `orientate` method solved, would you like to set up as an answer so I can accept? – Linesofcode Jun 10 '19 at 14:12

4 Answers4

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.

7

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);
Rwd
  • 34,180
  • 6
  • 64
  • 78
1
$img = Image::make($originalFile);

$img->orientate();
$img->resize(1024, null, function($constraint){
    $constraint->upsize();
    $constraint->aspectRatio();
});
$img->save();
Joundill
  • 6,828
  • 12
  • 36
  • 50
Basem Olimy
  • 767
  • 7
  • 10
  • 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. – Sven Eberth Jun 13 '21 at 13:20
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);
Yannick Beuchat
  • 241
  • 2
  • 6