Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created May 8, 2018 10:12
Show Gist options
  • Select an option

  • Save morgyface/b16ef21aaa45562d1d56cf4975ce20a5 to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/b16ef21aaa45562d1d56cf4975ce20a5 to your computer and use it in GitHub Desktop.
PHP | Image orientation with svg support and tolerance
<?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;
}
?>
@morgyface
Copy link
Author

morgyface commented May 8, 2018

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.php file.

Use get_orientation($image_file) in your template file.

Example:

$image_file = get_sub_field('logo');
$orientation = get_orientation($image_file);
echo '<img src="' . $image_file . '" class="' .  $orientation . '" />;

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:

$orientation = get_orientation($image_file, '2.1');

// or make it strict like this...

$orientation = get_orientation($image_file, '1');

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 pathinfo to determine file extension as opposed to mime. This gave me the desired result. I tried various mime identification php functions without consistent success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment