[wp-hackers] Always finding more and more cleverness in WP...

Otto otto at ottodestruct.com
Tue Aug 19 18:57:55 GMT 2008


I was making an image.php template for a site, so naturally I'm using
wp_get_attachment_image to get the IMG link for the page. However, I
needed to have it constrain the image size to a certain size.

Looking at the definition for it, I see this:
function wp_get_attachment_image($attachment_id, $size='thumbnail',
$icon = false)

Dang. I didn't want to use one of the predefined sizes in the
Settings->Miscellaneous screen, so I started sifting through the code.
Lo and behold, I find this buried down in image_get_intermediate_size:

function image_get_intermediate_size($post_id, $size='thumbnail') {
...
	// get the best one for a specified set of dimensions
	if ( is_array($size) && !empty($imagedata['sizes']) ) {

Huh? The $size can be an array? Neat!

Reading the code, it actually takes an array of max sizes you want on
each dimension, finds the next biggest one, then eventually returns
the image tag with the sizes you specify (proportionally scaled
correctly, of course). This lets the browser do the grunt work of
scaling the image down to fit. Okay, so browser scaling sucks, it's
still semi-okay at scaling down than up. And newer browsers (FF3) use
good routines to do this anyway.

In short:
<?php echo wp_get_attachment_image( $post->ID, array(600,600) ); ?>
Gives me an IMG tag that has a max size of 600 on one side, using the
next biggest scaled image it has as per the parameters on the
Settings->Miscellaneous screen.

I just found that highly clever and thought everybody should know about it. :)

-Otto


More information about the wp-hackers mailing list