Created
May 8, 2018 10:12
-
-
Save morgyface/b16ef21aaa45562d1d56cf4975ce20a5 to your computer and use it in GitHub Desktop.
PHP | Image orientation with svg support and tolerance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function get_orientation($image_file, $tolerance = '1.5') { | |
| // First let us determine what kind of file we are dealing with | |
| $path_parts = pathinfo($image_file); | |
| $extension = $path_parts['extension']; | |
| if ( $extension == 'svg' ) { | |
| // Vector svg | |
| $xmlget = simplexml_load_file($image_file); | |
| $xmlattributes = $xmlget->attributes(); | |
| $width = (string) $xmlattributes->width; | |
| $width = trim($width, "px"); // remove px after dimension | |
| $height = (string) $xmlattributes->height; | |
| $height = trim($height, "px"); | |
| } else { | |
| // Bitmap image | |
| list($width, $height) = getimagesize($image_file); | |
| } | |
| // Now we have a width and height let us determine the orientation | |
| if ( $width > $height ) { | |
| // Landscape | |
| if ( $width / $height >= $tolerance ) { | |
| // If the ratio is more than 1.5 it is indeed a proper landscape image | |
| $orientation = 'landscape'; | |
| } else { | |
| // if the ratio is less than 1.5 it is practically square | |
| $orientation = 'square'; | |
| } | |
| } elseif ( $height > $width ) { | |
| // Portrait | |
| if ( $height / $width >= $tolerance ) { | |
| $orientation = 'portrait'; | |
| } else { | |
| $orientation = 'square'; | |
| } | |
| } else { | |
| // The width and height must be the same | |
| $orientation = 'square'; | |
| } | |
| return $orientation; | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Image orientation
A client site had a list of customer logos in an SVG format, some quite obviously landscape or portrait and some pretty much square, I created this function so I could apply an orientation class to each image and then set the width or height accordingly.
For example;
.landscape { width: 200px; height: auto; }or.portrait { height: 100px; width: auto; }WordPress usage
Add the function to your theme's
functions.phpfile.Use
get_orientation($image_file)in your template file.Example:
Tolerance
Simply, this is how relaxed the function is about the image being classed as square.
I added this flexibility as I had one image that was 100 pixels wide by 95 pixels tall. Strictly speaking it's a landscape image, though visually it's pretty much square. In this instance the ratio was 1.05. I decided, visually, the ratio should be around 1.5. However this can be altered to suit, simply pass in a second variable like so:
Bitmap support
The function is geared toward svg images since they can be scaled without pixelisation. However, it will return a class for bitmap images too.
Mime
As you can tell I used
pathinfoto determine file extension as opposed to mime. This gave me the desired result. I tried various mime identification php functions without consistent success.