WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 4 months ago

#34817 new enhancement

Add a new image size detection function

Reported by: simonrcodrington Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.3.1
Component: Media Keywords:
Focuses: Cc:
PR Number:

Description

Hi there,

Currently if you need to see if a custom image size exists for an image you need to call get_image_sizes and pass in your image id. This returns either the the applicable information for that image size or false.

I think a better approach would be to have a function such as images_size_exists() which just detects if that size exists for a given image id and returns true or false.

When using wp_get_attachment_image_src for example, you pass in your desired size, e.g 'large'. If the size you passed in doesn't exist for the image it will choose an applicable similar image such as the 'full' size. If there was a function like images_size_exists() it would let you conditionally target based on if sizes exist.

I'm pretty keen on potentially adding this as a feature if it's not being worked on by anyone else?

Thoughts guys?

Change History (3)

#1 @joemcgill
4 years ago

Hi @simonrcodrington,

I believe image_get_intermediate_size() might already provide what you're suggesting. If you pass in an attachment ID and size name that doesn't exist, the function will return false.

Alternately, this sounds similar to the issue addressed in #33905. In that case, the solution was to check for a (previously undocumented) fourth parameter on the array returned from wp_get_attachment_image_src to make sure the image being returned was an intermediate size instead of the fallback 'full' size.

Would either of these options work for your use case?

#2 @simonrcodrington
4 years ago

Hey there @joemcgill

Thanks for the reply.

I think using image_get_intermediate_size() and checking for false would be the easiest way to do what I want (I just thought it might make more sense to have it's own function).

The link you sent through looks pretty interesting as it seems you can use wp_get_attachment_image_src() to detect if a passed back image is actually not the full size (as this was actually another reason why I wanted to double check the existence of set sizes).

I think that one is due for release in WP 4.4?

Simon

#3 @joemcgill
4 years ago

The fourth parameter in wp_get_attachment_image_sr() is already in WordPress, but it was just undocumented until 4.4.

Note: See TracTickets for help on using tickets.