14

Is there a standard way to get all the registered sizes of the featured images? One can register different sizes with add_image_size(). What I need, is to get all the sizes of a featured image of a post. Something like this:

$sizes = get_post_feature_image_sizes($postid);

...which will return an array of objects like this (here in JSON format):

[
{width: 200, height: 300, url: 'http://.........../wp-content/uploads/2015/10/file-200x300.jpg'},
{width: 300, height: 400, url: 'http://.........../wp-content/uploads/2015/10/file.jpg'},
]

Is there anything like this, or I will have to scan all the upload folder file names with regex?

1
  • The featured image is just the ID of an attachment, all the standard attachment APIs should work on it
    – Tom J Nowell
    Commented Oct 25, 2015 at 14:23

3 Answers 3

16

I don't remember a function that will do precisely that, but it is easily achieved with API overall:

  1. Retrieve attachment ID with get_post_thumbnail_id()
  2. Retrieve available sizes with get_intermediate_image_sizes()
  3. For each size use wp_get_attachment_image_src(), which gives precisely data you need (URL and dimensions).
3
  • Thanks, that helped a lot! :) This is my first time developing with WP and the API and the terminology is a bit frightening. :)
    – NoOne
    Commented Oct 26, 2015 at 14:40
  • is there any way to retrieve the current post's generated featured image size, width and height. For example if the the featured image set is of size 540 x 400, although it should be of 550 x 550. How to retrieve the current post's featured image size to store $width = 540 & $height = 400 in the current example.
    – Nimesh
    Commented May 6, 2018 at 22:51
  • @Nimesh wp_get_attachment_metadata($attachment_id) will get you what you need.
    – jave.web
    Commented Sep 28, 2018 at 18:00
9

After you get it's ID ( get_post_thumbnail_id($YOUR_POST_ID) ) you need to know the uploads base url and the attachment meta data.

Uploads base url can be found in an array returned by wp_upload_dir().

Attachment meta data can be retrieved by wp_get_attachment_metadata().

Here is a handy function I wrote that prepares the url data for any image attachment, not just featured.

function prepareImageData( $attachment_id ){
  $uploads_baseurl = wp_upload_dir()['baseurl'];

  $prepared = [];
  $data = wp_get_attachment_metadata($attachment_id);
  $prepared = [
    'mime_type' => get_post_mime_type($attachment_id),
    'url' => $uploads_baseurl.'/'.$data['file'],
    'sizes' => [],
  ];

  foreach( $data['sizes'] as $size => $sizeInfo ){
    $prepared['sizes'][$size] = [
      'url' => $uploads_baseurl.'/'.$sizeInfo['file'],
    ];
  }

  return $prepared;
}
1
  • 1
    excellent! You might also lik to add $dir = pathinfo( $data['file'] )['dirname']; to use as: 'url' => $uploads_baseurl.'/'.$dir.'/'.$sizeInfo['file'], as they might miss the year/month directories Commented Feb 11, 2021 at 14:51
1

To get image according to size you can use wordpres pre-defined function that is the_post_thumbnail( $size, $attr ).

you can use predefined media sizes.

the_post_thumbnail();                 

the_post_thumbnail( 'thumbnail' );       // Thumbnail (default 150px x 150px max)
the_post_thumbnail( 'medium' );          // Medium resolution (default 300px x 300px max)
the_post_thumbnail( 'large' );           // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' );            // Full resolution (original size uploaded)

the_post_thumbnail( array(100, 100) );

This might help you.

1
  • The point of the question is that the OP was looking to find the names of all available image sizes including any additional sizes created by the theme &/or plugins. Commented Aug 7, 2017 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.