Skip to content

Instantly share code, notes, and snippets.

@scofennell
Last active August 29, 2015 14:00
Show Gist options
  • Save scofennell/3061e83823c334887981 to your computer and use it in GitHub Desktop.
Save scofennell/3061e83823c334887981 to your computer and use it in GitHub Desktop.
WordPress function to detect if is a photo is panoramic.
<?php
/**
* Given an attachment_id and a definition of panoramia, detect if a photo is panoramic
*
* @param int $attachment_id the attachment_id for the image
* @param mixed $min_ratio the aspect ratio that defines panoramia
* @return boolean returns true if image is panoramic, otherwise returns false
*/
function sjf_deh_is_pano( $attachment_id, $min_ratio='2' ){
//the ID for this post, of the attachment post type, make sure it's an integer
if( !is_int( $attachment_id ) ) { return false; }
/**
* the minimum ratio of width to height in order to consider an image panoramic
* make sure it's numeric (it could be an int or a float)
*/
if( !is_numeric( $min_ratio ) ) { return false; }
/**
* an array that contains information about the image
* it's important to get the full size so as not to get a wp resized version
*/
$att_src_array = wp_get_attachment_image_src( $attachment_id, 'full' );
//if something went wrong, bail
if( !is_array( $att_src_array ) ) { return false; }
//the width of the image in pixels
$width = absint( $att_src_array[1] );
//if something went wrong, bail
if( empty( $width ) ) { return false; }
//the height of the image in pixels
$height = absint( $att_src_array[2] );
//if something went wrong, bail
if( empty( $height ) ) { return false; }
//the threshold_width is the minimum width for the image in order to be considered pano
$threshold_width = $min_ratio * $height;
//if the image is wider than the threshold, it's pano
if( $width > $threshold_width ) { return true; }
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment