Make WordPress Core

Opened 23 months ago

Last modified 21 months ago

#37549 reviewing enhancement

Add optional parameter to wp_generate_attachment_metadata for image dimensions.

Reported by: sterlo Owned by: joemcgill
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.6
Component: Media Keywords: has-patch
Focuses: Cc:


My plugin Scalable Vector Graphics received a support request, SVG in image gallery is not working -- which caused me to do some digging in core.

I realized the reason the Theme wasn't doing what it intended to do is because it is reyling on wp_get_attachment_metadata() -- which lead me to realize that SVG files -- although being uploaded and working for the most part, are missing the meta information normally generated for images such as JPG files.

The function that does that is wp_generate_attachment_metadata.

The reason it is not doing that is because file_is_displayable_image is returning false, which I can override if I want with the filter -- BUT -- the function also depends on getimagesize -- which I cannot navigate around and does not work with SVG files.

It would be cool if

function wp_generate_attachment_metadata( $attachment_id, $file ) {

became something like

function wp_generate_attachment_metadata( $attachment_id, $file, $dimensions = false ) {

SVG files are displayable -- and file_is_displayable_image is a well meaning function but it's not really determining if a file is displayable. It's determining if an image will work with getimagesize.

The Ask: Let's allow developers to pass dimensions to wp_generate_attachment_metadata so that we allow for future file types that are web friendly to exist within Wordpress, with the support of plugins, and that useful information about those uploaded files is generated in a controllable and consistent manner.

Right now, the recourse I have as a plugin developer is to copy wp_generate_attachment_metadata and generate my meta data within the plugin. Which I isn't a problem, but I feel this request nudges image.php into a direction it's going to need to go sooner or later anyway.

Attachments (2)

image.php.patch (1.3 KB) - added by sterlo 23 months ago.
Patch that introduces optional parameter.
Screen Shot 2016-08-02 at 18.36.24.png (30.2 KB) - added by sterlo 23 months ago.
Example of what implemented patch can produce for developers.

Download all attachments as: .zip

Change History (9)

This ticket was mentioned in Slack in #core-images by sterlo. View the logs.

23 months ago

#2 @sterlo
23 months ago

  • Component changed from Upload to Media

23 months ago

Patch that introduces optional parameter.

23 months ago

Example of what implemented patch can produce for developers.

#3 @sterlo
23 months ago

  • Keywords has-patch added

Here is an example of how this could be leveraged by Plugin/Theme developers if the patch were to be implemented:


#4 @joemcgill
23 months ago

  • Owner set to joemcgill
  • Status changed from new to reviewing

Hi @sterlo,

Thanks for the detailed description and initial patch. I think you might be able to accomplish what you need to with a filter on the output of wp_generate_attachment_metadata(). In reality, you don't want to jump into the first branch of that if statement, because the only metadata you need is the height, width, and file—not the additional intermediate sizes.

Could you get the attachment_id from the filter, see if the attachment is an SVG, and then generate those three pieces of data (most of which you demonstrated in your gist)? This would also avoid the getimagesize() issues you run into in file_is_displayable_image() as well.

#5 follow-up: @sterlo
23 months ago

@joemcgill I will take a stab at it and report back.

#6 in reply to: ↑ 5 @joemcgill
22 months ago

Replying to sterlo:

@joemcgill I will take a stab at it and report back.

Hi @sterlo any update on whether the approach mentioned above would get you what you need here?

#7 @sterlo
21 months ago

I will be jumping back in here this weekend! Thanks for following up and being cool with the delay :-)

Note: See TracTickets for help on using tickets.