I have a brilliant idea and since WordPress already takes care of some of the work, I just need to find a good method to make this work.
I am working on a project that needs to be responsive to all devices, whether a desktop PC or mobile gadget. Thus, I want the images to also be responsive, meaning that mobile devices shouldn’t load 50KB+ images.
For each page or post, I can add a Featured Image using Post Thumbnails which, at full-size, the image is about 950×250 at ~60KB. If I load the website on an iPhone/Android, I wouldn’t want the ~60KB image to load, but instead would like the small thumbnail to load in its place.
The default method for responsive images is to make the width of the image 100% of the parent container, thus it will resize automatically if the parent container is also resized. Not the best method for larger images.
I thought about trying out Filament Group’s responsive image script, but I tried it and it didn’t work right. One way this could be accomplished is through user-agent detection, but I’d rather not do this method either since user-agents can be spoofed.
Here’s another method for resizing images on the fly, but this seems to be duplicating what WordPress has already done.
If there is a way of doing this with the Media Gallery images that WordPress has be default, with all the resized thumbnails already created, that would be preferable.
Step 1:
Define two custom image sizes, e.g.:
Step 2:
Implement your chosen means to determine client. There are several ways, and which method you use is outside the scope of this question. But, assuming you have a method that works for you, output the result to some variable, such as
$mobile_device = true;
Step 3:
In your template files, output the image conditionally, based on client.
Note: you could repeat the
if/else
(or do aswitch
) for multiple form factors (i.e. screen sizes). Just add multiple custom image sizes, and conditionally test for each screen size you want to support.The best way is to use a fluid grid to build the WordPress theme, and remove the width and height values of featured images through a function for proportional scaling. A tutorial on Making WordPress images responsive:
This is not a bad method and is actually the industry standard, a good WURFL has a very high accuracy rating, the ones I have used and tested always returned solid results and independent testing ( above what I would bother doing) seem to have them in the 98% +range. Who cares if some lame bot is spoofing a user-agent, they aren’t there for a good reason anyhow.
For step 2 I think either method comes down to which is actually faster, a PHP backed WURFL or CSS media query’s.
First you have to define âbestâ. My definition would be:
Renders the image with the designerâs intended effect on any device or screen
Renders the image with equal quality as the original
Consumes the absolute minimum amount of system and human resources (i.e. bandwidth, CPU, designer/programmer time)
Here are the approaches I have seen so far:
Pros: Requires next to no effort to implement, cross browser compatible and supported by older browsers.
Cons: Often downloading more data than required and then spending CPU cycles on the client scaling it down (slow). You may end up with very poor quality images depending on the browserâs scaling algorithm. No possibility for artistic direction and canât adapt image for retina type displays.
Pros: Faster download on mobile devices. Can handle retina type displays. Improved image quality since images where hopefully processed using some high quality method. Artistic direction becomes possible.
Cons: Someone has to spend time processing, cropping and managing multiple versions of the same image. More coding: you now have to spell out each and every version of the image in some fashion and create media queries for all desired layouts. Repeat the above for each and every image you serve. Will only work for browsers supporting CSS3 Media Queries or the new tags.
Pros: Designer doesn’t have to spend any time processing images and managing multiple versions. The most optimally sized image is sent every time. Can handle retina type displays and dynamically adjust for artistic direction (although with some extra effort â have to know where to focus). No special or extra markup required (caveat below). Cross browser compatible and will work for older browsers.
Cons: Have to capture and transmit information about clientâs browser and screen properties. First time an image loads may be slower than in any other approach since the image has to be processed (is typically cached for later requests).