Jump to content

Resizing an image if it's landscape PHP


melissal

Recommended Posts

Hello,

 

I'm a little hopeless with PHP, still learning!

 

I want to display an image differently if it's uploaded as a landscape image to if it's in uploaded in portrait mode - basically I want to be able to detect which has been uploaded by the width. Can I get a rough idea of how I would achieve this?

Edited by melissal
Link to comment
Share on other sites

A way to tell if an image is landscape or not is.. usually images that are landscape have their width larger than the height. You can check to see if that is the case and if so, it's a landscape. To be more precise, I would see what the difference is between the width and height. So if an image is 800 width and 400 height, since the width is obviously larger than the height, it's a landscape image but check the difference which is 400 because in some cases, you can have an image where the width is larger than the height by only a small amount like 500 width and 300 height, 200 difference which this can be considered a portrait image really.

 

So check to see if the width is larger than the height and also see what the difference is and based on your preference of what you want to be considered a landscape, consider that image 'landscape' then. So another example, user uploads an image of a width of 900 and height 500, obviously this is a landscape most likely but say you have 300 of what to check difference from and if the difference is larger than 300 which it is in this case (400), it's landscape, if it's less than 300, you can consider it a portrait. It's all in your preference as to what you want to consider a landscape or not.

 

I would though first resize all the uploaded images if they are larger than a specified amount for the site because these days from digital cameras, phone cameras, etc, images can be very large in width and height with dimensions like: 3434 by 2213 or something of the like. Good size image dimensions these days for website displaying if larger than the following dimensions is about 700 or 800 px width. So if the uploaded image width is larger than 800, I would resize the image to 800 px. (The height is automatically calculated to preserve aspect ratio).

 

Hope this was of some help.

Link to comment
Share on other sites

You can use the getimagesize function to get information about the size:http://php.net/manual/en/function.getimagesize.phpSome images will also be saved with a certain orientation defined but get displayed differently, in that case you can use this function to figure out what the orientation should be and rotate the image:http://php.net/manual/en/imagick.getimageorientation.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...