WordPress makes it easy to generate images in various sizes through the add_image_size  function. There are a couple of limitations however:

  • Image have to be regenerated after creating or modifying an image size. There are plugins like Regenerate Thumbnails that make this easy, but for a large number if images it can take a long time.
  • Image generation is all-or-nothing. If you add an image size and then regenerate your images you will have every image in every size.

My solution to this was to write a function that temporarily creates an image size, regenerates thumbnails for that image only and the removes the image size so future images won’t be created in that size.

This function is “lazy” because it does not create images until they are needed. Calling that function will generate a new image, only if one does not already exist. In other words, the first call creates the new image size, subsequent calls fetch the existing size.

Gist.