WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 5 months ago

#37549 reviewing enhancement

Add optional parameter to wp_generate_attachment_metadata for image dimensions.

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

Description

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

<?php
function wp_generate_attachment_metadata( $attachment_id, $file ) {

became something like

<?php
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 3 years ago.
Patch that introduces optional parameter.
Screen Shot 2016-08-02 at 18.36.24.png (30.2 KB) - added by sterlo 3 years 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.


3 years ago

#2 @sterlo
3 years ago

  • Component changed from Upload to Media

@sterlo
3 years ago

Patch that introduces optional parameter.

@sterlo
3 years ago

Example of what implemented patch can produce for developers.

#3 @sterlo
3 years 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:

https://gist.github.com/grok/e257ea85a492ab267fd1f0b28fdafe03

#4 @joemcgill
3 years 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
3 years ago

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

#6 in reply to: ↑ 5 @joemcgill
3 years 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
3 years 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.