-
-
Save isotrope/9ba2781228ead072c8a6 to your computer and use it in GitHub Desktop.
Force resizes and caches images without needing to add_image_size() and bloat the /upoads/ dir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Force resizes and caches images without needing to add_image_size() and bloat the /upoads/ dir | |
* | |
* Most code is completely based on Konstantin Kovshenin's image-shortcode.php | |
* https://gist.github.com/kovshenin/1984363 | |
* | |
* Expects the params similar to wp_get_attachment_image_src() | |
* http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src | |
* | |
* @uses image_make_intermediate_size | |
* | |
* @param int $attachment_id | |
* @param array $size | |
* | |
* @return array|string | |
*/ | |
function iso_get_resized_attachment_src( $attachment_id, $size = array( 150, 150 ), $cropped = true ) { | |
global $wpdb; | |
// Sanitize | |
$width = absint( $size[0] ); | |
$height = absint( $size[1] ); | |
$needs_resize = true; | |
$desired_image_size = sprintf( 'resized-%dx%d', $width, $height ); | |
$upload_dir = wp_upload_dir(); | |
$src = wp_get_attachment_url( $attachment_id ); | |
// If an attachment record was not found. | |
if ( false === $src ) { | |
return "Error: attachment not found."; | |
} | |
// Get the attachment's meta information | |
$meta = wp_get_attachment_metadata( $attachment_id ); | |
if ( ! empty( $meta['sizes'][ $desired_image_size ] ) && ! empty( $meta['sizes'][ $desired_image_size ]['file'] ) ) { | |
$src = str_replace( basename( $src ), $meta['sizes'][ $desired_image_size ]['file'], $src ); | |
$needs_resize = false; | |
} | |
// Too small to resize | |
if ( $meta['width'] < $width && $meta['height'] < $height ) { | |
if ( empty( $meta['sizes'][ $desired_image_size ] ) || empty( $meta['sizes'][ $desired_image_size ]['file'] ) ) { | |
$key = $desired_image_size; | |
$mime_type = wp_check_filetype( $src ); | |
$meta['sizes'][ $key ] = array( | |
'file' => basename( $src ), | |
'width' => $meta['width'], | |
'height' => $meta['height'], | |
'mime-type' => $mime_type['type'], | |
); | |
wp_update_attachment_metadata( $attachment_id, $meta ); | |
} | |
$needs_resize = false; | |
} | |
// If an image of such size was not found, we can create one. | |
if ( $needs_resize ) { | |
$attached_file = get_attached_file( $attachment_id ); | |
$resized = image_make_intermediate_size( $attached_file, $width, $height, $cropped ); | |
if ( ! is_wp_error( $resized ) ) { | |
// Let metadata know about our new size. | |
$key = $desired_image_size; | |
$meta['sizes'][ $key ] = $resized; | |
$src = str_replace( basename( $src ), $resized['file'], $src ); | |
wp_update_attachment_metadata( $attachment_id, $meta ); | |
// Record in backup sizes so everything's cleaned up when attachment is deleted. | |
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); | |
if ( ! is_array( $backup_sizes ) ) { | |
$backup_sizes = array(); | |
} | |
$backup_sizes[ $key ] = $resized; | |
update_post_meta( $attachment_id, '_wp_attachment_backup_sizes', $backup_sizes ); | |
} | |
} | |
return array( esc_url( $src ), $width, $height ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment