Created
August 9, 2018 12:04
-
-
Save morgyface/82c5279e3d12bbef21c7d1fb11bcf015 to your computer and use it in GitHub Desktop.
WordPress | Image size test function
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 | |
| // Works with thumbnail, medium, large and full | |
| function has_thumbnail_size($size, $post_id) { | |
| if ( has_post_thumbnail($post_id) ) { | |
| $image_id = get_post_thumbnail_id($post_id); | |
| $image_attributes = wp_get_attachment_image_src($image_id, 'full'); | |
| $image_width = $image_attributes[1]; | |
| $image_height = $image_attributes[2]; | |
| $media_width = $size . '_size_w'; | |
| $media_width = get_option($media_width); | |
| $media_height = $size . '_size_h'; | |
| $media_height = get_option($media_height); | |
| if ( ($image_width > $media_width) && ($image_height > $media_height) ) { | |
| return true; | |
| } else { | |
| return null; | |
| } | |
| } else { | |
| // No thumbnail attached to post | |
| return null; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Image size test
The principle behind this is; the function grabs the original full size image dimensions, then it grabs the requested image size dimensions (as set on settings>media), then it makes sure the original size is bigger than the requested size. The idea being, if the original is bigger; it has been able to generate the smaller size.
It's not fool proof, if the image sizes have changed since the original upload, I guess the principle fails.