ExactDN for Plugin and Theme Developers
If you are wanting to make your plugin work better with ExactDN, it helps to understand how it works. So here is a list of things that ExactDN does:
- It looks for height/width attributes on any img elements that it finds, and will resize an image to those dimensions.
- It checks the $content_width global variable, which is normally set by the theme, and scales images to that width.
- It will also look at the attachment ID and size names that might be inserted as classes or other attributes and makes sure the image is not too large.
- Now we get to the good stuff, where ExactDN filters the image_downsize() function to produce an image that is exactly the size you need. I'll show some practical tips of how this works in a minute.
- And lastly, it filters the built-in srcset functions for WordPress to fill in any gaps that might exist for mobile devices and varying screen sizes. It uses multiples of .2x, .4x, .6x, .8x, 2x, and 3x by default, but you can override that and add your own if you like.
You've likely never used or seen the image_downsize() function directly, but it is used by these functions: get_image_tag(), wp_get_attachment_image_src(), wp_get_attachment_thumb_url(), wp_get_attachment_image_url(). If you visit the developer reference for any of these functions, you'll see that they are, in turn, used by other core functions, many of which have a size parameter available.
The size parameter is where the magic happens, but you have to use it correctly to get the best results, because it can be given either the name of an existing size, or a desired height/width combination. If you just pass along a size name, like 'thumbnail', or 'full', you'll get what you asked for. But if you pass along the exact dimensions you need, like array( 800, 600 ), then you'll get that exact image size from ExactDN.
Similarly, the wp_get_attachment_image_srcset() function mentioned in #5 above has a size parameter that works exactly the same. But in this case, ExactDN has some wiggle room. So if you pass along 'medium', it will give you that size + the above mentioned multiples, and any existing sizes that are smaller than the requested size.
One last note, if you use aq_resize() in your code, you need to be careful to check the return value, because it's highly likely that ExactDN will already have altered the url before it gets to aq_resize(), and then aq_resize() will give you a big fat nothing in response. Apparently, it's an intended design, so you need to check to see that aq_resize() has returned a valid url, and use the original url if it doesn't. Of course, now that you know about ExactDN, you can get the same effect without placing any strain on the web server and with increased performance.
If you think of anything I've missed, or have additional questions, just use the contact form and I'll be happy to help you out.