1

I need to resize images in php using GD to a fixed size but maintain the orientation (portrait/landscape), these are:

portrait: 375 x 500px landscape: 500 x 375px

Everything I have tried always ignores the orientation and makes them all landscape.

Can anyone help?

designvoid
  • 431
  • 1
  • 6
  • 19

3 Answers3

2

Check the width and height of the incoming image. If the width is wider than the height, make your target size 500 x 375, otherwise make it 375 x 500. Then run the resize code with those target dimensions.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
  • And since you already have an gd image resource you can use imagesx() and imagesy() to get the width and height. – VolkerK Mar 31 '10 at 14:09
1

Here is a method from an image class I use that calculates the scaled dimensions of an image. It works by fitting an image into a square box.

You can set $box = 500 and then pass the $x and $y of the image you are trying to resize and it will always return the correct resized dimensions maintaining the aspect ratio.

public static function fit_box($box = 200, $x = 100, $y = 100)
{
  $scale = min($box / $x, $box / $y, 1);
  return array(round($x * $scale, 0), round($y * $scale, 0));
}
Giles Smith
  • 166
  • 7
  • Hm, the question is not about scaling but how to determine whether an image has portrait (height>=width) or landscape (width>=height) orientation. – VolkerK Mar 31 '10 at 14:51
  • I assumed that he wanted to maintain the aspect ratio as well as the orientation whilst resizing the images, which is what my function will do. The function will always keep the larger dimension at 500px whilst matching the shorter dimension without needing to know the orientation. – Giles Smith Mar 31 '10 at 15:18
0

This question was answered correctly here: How to detect shot angle of photo, and auto rotate for website display like desktop apps do on viewing?

The short answer is, you need to use the exif_read_data() function to get the "Orientation" attribute of a photo, and then you can use that value to determine whether you need to rotate your photo. The answer above contains sample PHP code that shows you how to do that.

Community
  • 1
  • 1
Justin Watt
  • 740
  • 1
  • 10
  • 17