Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created August 9, 2018 12:04
Show Gist options
  • Select an option

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

Select an option

Save morgyface/82c5279e3d12bbef21c7d1fb11bcf015 to your computer and use it in GitHub Desktop.
WordPress | Image size test function
<?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;
}
}
@morgyface
Copy link
Author

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.

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