Add the ability to filter wp_get_attachment_image()

Description

i'm working on a plugin that lets users define any image to serve as the thumbnail for any PDF document. I'm storing the ID of the image as post_meta for the PDF attachment. Instead of showing the default icon with wp_get_attachment_image() I'm quite close to being able to do this by filtering image_downsize.

the trouble is that the image_downsize filter it run after

if ( !wp_attachment_is_image($id) )
return false;

so the PDF image fails the test and I never have a chance to run my filter.

i looked at the wp_mime_type_icon filter and it almost works. you can send back a custom URL to the wp_mime_type_icon filter, but the URL gets truncated down to simply the filename by wp_basename($src) in the wp_get_attachment_image_src() function.

which is missing the sub-directories. i thought about trying to send an empty path to the icon_dir filter and sending the full path to the wp_mime_type_filter, but you can't do that either b/c of that string '/' in the way.

wow a duplicate from 4 years ago! i almost have to applaud that. i would have to dig out the code i was working on to see if this would work for what I was attempting, but it definitely looks like an elegant solution. 1 line of code and shouldn't have any backcompat issues. i hope it makes it into core.

i'd agree that a wp_get_attachment_image_src filter should be inside the wp_get_attachment_image_src function, but that function is currently structured such that if you are getting the attachment_image for a non-image, you pretty much fail right away b/c if you don't get an image back from image_downsize(), you get kicked to looking for an icon. and there's no way to return an image from an attachment's postmeta, b/c image_downsize() immediately returns false if the attachment itself isn't an image.

i like your solution too though. it should be there, but for my use case, it is a little late and would be redundant. but a filter for wp_get_attachment_image_src definitely gives the most control. just a matter of where it should be. it is definitely trickier in the wp_get_attachment_image_src function b/c there are places where a value could return

Added a new patch for media.php (media.php.2.patch​) that places the wp_get_attachment_image_src filter in the wp_get_attachment_image_src() function. Multiple returns are removed in favor of a single array which is then filtered, and if the result is still empty() returns false.

I think this makes more sense as the wp_get_attachment_image_src() could be called directly, and I would expect the results to be filtered.

Would love to see this implemented ASAP. I am now working on a new project that includes video uploads and would like to be able to display a video thumbnail using native functions. Obviously, this can't be done until we work this out. It is a rather unfortunate situation to have to remove the default icon row from the list table completely, and replace it with my own in order to display thumbnails.

If it was me I would close the ticket since that function isn't for that kind functionality. We are talking about attachments and by default they don't have any relationship with something else. I would change some of the logic a different way. Since 3.5 we have Imagick support and hopefully in 3.6 Gmagick support and with that you can create an image from a PDF.

So I guess 20205.patch makes the most sense since that would generate fallback when WordPress can't handle it.

I still feel that filtering the wp_get_attachment_image_src() function is a better choice, as it is used by wp_get_attachment_image(), otherwise you will get different results if you have only filtered the latter but also need access to the image URI.

The problem what I have with filtering wp_get_attachment_image_src() is the array output. I guess we should rewrite the function so you can manipulate the $src. I will give it a try now.

What's wrong with passing the whole array into the filter, as I did in the patch I just posted? If you change the image source, the dimensions are more than likely different as well and should be changed just the same.

The problem with that is that people will break stuff faster. I was thinking if we can make if safer and easier. They can pass the url and then call getimagesize() on it so you can retrieve the width/height.

I also not sure if we should apply the filter on the result of image_downsize().

Sorry, but if people "break" something, that's their own problem. There is no warranty, expressed or implied, when you utilize the available hooks that WordPress provides.

I was thinking if we can make if safer and easier. They can pass the url and then call getimagesize() on it so you can retrieve the width/height.

The problem with this approach, is that some servers do not allow calling getimagesize on a remote image. What if somebody wants to use a remote image, such as a YouTube thumb, and they already know the dimensions. They can then pass the known dimensions into the filter.

I also not sure if we should apply the filter on the result of image_downsize().

I would expect the filter to be applied to whatever result the function produces.

attachment:20205.diff​ cleans up this function. Let's wrap the image_downsize() call in a wp_attachment_is_image() check, this makes more logical sense, even if that check is already in that function. Don't short-circuit there, instead carry the returned value (similar to @jfarthing74's approach) through the function to be passed into the filter at the end of the function.

This also actually fixes a separate bug: you couldn't previously get the icon if the attachment is an image, it returned early with the image_downsize() call.

On a sidenote, it's convoluted that we offer an 'icon' parameter for this function. I would like to see this deprecated, and a separate function created, perhaps wp_get_attachment_icon_src().