Please note: The following code was written specifically for WordPress version 2.8.6. It may apply to other versions (ie: 2.8.x), but is untested on versions other than 2.8.6.
I've been spending some time recently redesigning my WordPress powered blog (again!) in an attempt to evolve it into something more attractive, mature, and accessible. It’s an ongoing process which will likely never end, but at least I can say I'm learning in the process. I'm most definitely not a WordPress expert.
One of the things I dislike about the way WordPress works is how it names thumbnails of images which have been uploaded to the Media Library. Thankfully, by default WP stores the uploaded image under its original filename, however when automatically resizing and/or cropping the image to the Large, Medium, and Thumbnail sizes, it appends a suffix to the filename. By default, the suffix is "-{width}x{height}" (eg: MyUploadedImage-75x100.jpg).
I suppose this could be useful, as it’s an easy way to figure out what the size of each image is, however WP has built-in functions which put metadata for images in variables that are much easier to use directly, rather than parsing a filename string to determine the dimensions of the image. Rather, I find it much more useful to provide a more easily "human recognizable" indication of image size.
For my purposes, I wanted images to be named "MyImage-large.jpg", "MyImage-medium.jpg", and "MyImage-thumbnail.jpg" for Large, Medium, and Thumbnail images respectively, so I set out to find a way to do so. After a long session of Googling and digging through WP support websites, I did not find any solutions, which is surprising considering the number of people that use the software.
Knowing I'd have to come up with my own solution, I went digging through the WordPress code, courtesy of Joost de Valk. After some searching, I found the image_resize function, and the $suffix variable in the /wp-includes/media.php file. $suffix is normally defined like so:
if ( !$suffix )
$suffix = "{$dst_w}x{$dst_h}";
By modifying the second line (the statement portion of the if construct), I could easily change the suffix to whatever I wanted. Unfortunately, though, the image_resize function doesn't hold a variable for which intermediate size it is creating, so I'd have to determine that.
In my installation, I've got automatic resizing set up a custom way, so that images are first resized to a maximum width (max height being zero), and then cropped (if necessary) to a maximum height. Due to this, I always know what my maximum width will be. Generally, this is the same for most WP installations, the max width and height being set in the Settings -> Media screen. So, to figure out which suffix to append to the image, all I have to do is determine the target size of the resulting image based on the $max_w variable (which is passed as an argument to image_resize:
if ( !$suffix )
{
switch ($max_w) {
case (intval(get_option('large_size_w'))):
$suffix = 'large';
break;
case (intval(get_option('medium_size_w'))):
$suffix = 'medium';
break;
case (intval(get_option('thumbnail_size_w'))):
$suffix = 'thumbnail';
break;
default:
$suffix = 'other';
}
}
Here I'm using a switch construct to determine which case matches $max_w. In each case, I'm testing against a different user-setting for image sizes (the ones set in the WP Media options screen), which are retrieved using get_option( … ) and converted to integers with intval( … ). When a match is made, $suffix is then set to the appropriate value, and the break statement then exits the switch. Note that I'm also providing a default case, which is a sort of fall-back, in case any of the cases don't match. In theory, this shouldn't ever be used, but is helpful for troubleshooting, should I ever see a random "MyImage-other.jpg" file appear in my uploads directory.
I now have properly resized intermediary and thumbnail sized images with definite filenames for each of my uploads. I no longer have to search the uploads directory to figure out a filename; I can simply expect that it will be one of -large, -medium, or -thumbnail.
The one unfortunate downside of this modification is that it is a hack to the core WP files, as there are no hooks into image_resize which would allow me to create a plugin to do the same, meaning that it will have to be performed after every upgrade to the WP code.
One Response to “Custom image suffixes in WordPress”
Sufixo de imagens no WordPress…
Gostei muito das últimas atualizações do Worpress porque adicionaram funcionalidades que eu obtinha apenas por meio de plugins. O gerenciamento de imagens, por exemplo, faço agora pela Biblioteca de Mídia. O sistema oferece ferramentas básicas de ediçã…