Last active
December 10, 2015 22:18
-
-
Save SleeplessByte/4501498 to your computer and use it in GitHub Desktop.
Missing function to get the defined thumbnail size metadata and a check to see if an attachment's thumbnail is actually of that defined size.
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 | |
/** | |
* Gets the thumbnail resize sizes. | |
* | |
* These are the sizes defined by the media options page or custom added sizes, rather than the actual | |
* thumbnail image sizes. Falls back to thumbnail if size does not exist. Put your size parameter first | |
* through the 'post_thumbnail_size' filter if you want WordPress get_post_thumbnail behaviour. | |
* | |
* @size either string with thumbnail name size or array with dimensions | |
* @returns array with width, height and crop parameters | |
*/ | |
public function get_thumbnail_base_size( $size ) { | |
global $_wp_additional_image_sizes; | |
if ( is_array( $size ) ) | |
return array( 'width' => intval( $size[0] ), 'height' => intval( $size[1] ), 'crop' => false ); | |
// Custom size | |
if ( isset( $_wp_additional_image_sizes[ $size ] ) ) | |
return $_wp_additional_image_sizes[ $size ]; | |
// Rename size | |
if ( in_array( $size, array( 'post-thumbnail', 'thumb' ) ) ) | |
$size = 'thumbnail'; | |
// Get from media settings | |
$width = get_option( $size.'_size_w' ); | |
$height = get_option( $size.'_size_h' ); | |
$crop = get_option( $size.'_crop' ); | |
// If couldn't get this | |
if ( empty( $width ) && $size != 'thumbnail' ) | |
// Get default | |
return get_thumbnail_base_size( 'thumbnail' ); | |
// Return sizes | |
return array( 'width' => intval( $width ), 'height' => intval( $height ), 'crop' => !empty( $crop ) ); | |
} | |
/** | |
* Checks if a attachment's thumbnail is of the defined size | |
* | |
* @attachment_id attachement to check | |
* @size size to check for | |
* @returns true if correct size, false if non existant or too small | |
*/ | |
protected function is_thumbnail_of_size( $attachment_id, $size ) { | |
$meta_size = get_thumbnail_base_size( $size ); | |
$image_attributes = wp_get_attachment_image_src( $attachment_id, $size ); | |
return isset($image_attributes) && ( | |
// When cropping, sizes needs to exactly match | |
($meta_size['crop'] && ( $image_attributes[1] == $meta_size['width'] && $image_attributes[2] == $meta_size['height'] ) ) || | |
// When not cropping, one of the sizes needs to exactly match | |
(!$meta_size['crop'] && ( $image_attributes[1] == $meta_size['width'] || $image_attributes[2] == $meta_size['height'] ) ) | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment