Skip to content

Instantly share code, notes, and snippets.

@wkw
Created August 7, 2014 16:18
Show Gist options
  • Save wkw/9c5f795c8187665b68cc to your computer and use it in GitHub Desktop.
Save wkw/9c5f795c8187665b68cc to your computer and use it in GitHub Desktop.
Add this to your WordPress functions.php if you are using many custom image sizes (add_image_size(...)) to prevent the creation of all the sizes upon image upload. This will only generate a custom size the first time it is requested.
/* ======================================================================================
//! -- On Demand Image Sizing --
Experimental code for generating custom image sizes on demand.
http://wordpress.stackexchange.com/a/124790
From the author...
"When an image is then requested in a particular size,
which is not yet generated, it will be created only that once."
====================================================================================== */
add_filter('image_downsize', 'ml_media_downsize', 10, 3);
function ml_media_downsize($out, $id, $size) {
$dims = null;
$crop = false;
if( is_array($size) ){
// don't handle these for now. but if you want on-the-fly sizes, comment out
// the return statement
return false;
$dims = $size;
// create custom image size name consisting of widthXheight
$size = sprintf("%dx%d", $size[0],$size[1]);
}
// If image size exists let WP serve it like normally
$imagedata = wp_get_attachment_metadata($id);
if (is_array($imagedata) && isset($imagedata['sizes'][$size]))
return false;
// Check that the requested size exists, or abort
if( $dims === null ){
global $_wp_additional_image_sizes;
if (!isset($_wp_additional_image_sizes[$size]))
return false;
}
// Make the new thumb
if( $dims ){
// we got passed custom width/height as array...
if (!$resized = image_make_intermediate_size(
get_attached_file($id),
$dims[0],
$dims[1],
$crop
)){
return false;
}
}else{
if (!$resized = image_make_intermediate_size(
get_attached_file($id),
$_wp_additional_image_sizes[$size]['width'],
$_wp_additional_image_sizes[$size]['height'],
$_wp_additional_image_sizes[$size]['crop']
)){
return false;
}
}
// Save image meta, or WP can't see that the thumb exists now
$imagedata['sizes'][$size] = $resized;
wp_update_attachment_metadata($id, $imagedata);
// Return the array for displaying the resized image
$att_url = wp_get_attachment_url($id);
return array(dirname($att_url) . '/' . $resized['file'], $resized['width'], $resized['height'], true);
}
add_filter('intermediate_image_sizes_advanced', 'ml_media_prevent_resize_on_upload');
function ml_media_prevent_resize_on_upload($sizes) {
// Removing these defaults might cause problems, so we don't, though you could
// probably set them to all be the same dimensions and rely on custom image sizes
// for everything else
return array(
'thumbnail' => $sizes['thumbnail'],
'medium' => $sizes['medium'],
'large' => $sizes['large']
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment