Update: WordPress – Dealing with lots of standard image sizes

Jul 31, 2012

This is an update to the blog post and gist I wrote about getting image source filenames from wordpress when all you have is the thumbnail version. It turns out the whole thing is a bit more complicated, especially if your client/editor is uploading files that already have dimensions in the filenames. Not to mention an odd little WordPress “feature” where you sometimes don’t get the files in the sizes you expect.

I wrote a post a little while ago with my clever little regular expression I was using to switch between different thumbnail sizes in WordPress. The idea being that sometimes you have several different thumbnail sizes set-up in your functions.php using add_image_size and you have one of the images at a certain size but you actually need a different size. All my regex did was switch the dimensions in the filename and et voila we have the new image. Except I came a cross a number of pitfalls with this approach which I will explain below.

Sometimes your files already have dimensions in them and this means the regex has to be much more complex. Also, we have to watch out for the case where we think we have the wrong size but in fact have the correct size all along and the original regex made a mess of that! Finally, I discovered that WordPress doesn’t always give you files of the correct size.

Really? Well it seems so. Usually WordPress behaves and gives you the correct sized files based on the settings you specify in add_image_filename but I seem to be seeing a problem whereby the dimensions are very close and WordPress gives you the original size instead of the one your requested. This all works if you use the WordPress way of doing things everywhere but using my code you end up with a new filename that doesn’t match any file.. usually by a matter of a few pixels. I’m not sure if this is a rounding problem, or perhaps WordPress is actually being clever and when the dimensions are very close it guesses (usually correctly) that it won’t matter and it is better to save the processing time and skip the resize. Either way it caused me problems, so here is some code with a little explanation as to why/what I am doing.

function jimcode_get_thumnail($existing_filename, $width, $height) {
  // do a clever regex replace
  $filename =  preg_replace('/(-\d+x\d+)?\.(\w{3,4})$/', '-'.$width.'x'.$height.'.\\2',$existing_filename);
  if ($filename == $existing_filename) {
    // we didn't change anything - so check if we matched the same dimensions
    if (preg_match('/-\d+x\d+\.(\w{3,4})$/', $existing_filename) == 0) {
      $filename =  preg_replace('/\.(\w{3,4})$/', '-'.$width.'x'.$height.'.\\1',$existing_filename);        
  // check if that actually exists and if not just return without any size
  // (optional)
  // get the url without the domain
  $path = str_replace(get_option('home'), "", $filename);
  $path = ABSPATH.$path;
  $path = str_replace('/', DIRECTORY_SEPARATOR, $path);
  if (file_exists($path)) {
    return $filename;
  } else {
    return preg_replace('/(-\d+x\d+)?\.(\w{3,4})$/', '.\\2',$existing_filename);;
  return $filename;