Skip to content

Commit

Permalink
Avoid calling wp_get_attachment_image_src() in srcset functions.
Browse files Browse the repository at this point in the history
Improve the performance of `tevkori_get_srcset_array()` by removing the call to
`wp_get_attachment_image_src()` and use `image_get_intermediate_size()` in combination
with data we already get in the attachment post_meta to produce a srcset value. This
also gets around edge cases where the height and width attributes were being filtered
in `image_downsize()`. Updates tests.
  • Loading branch information
joemcgill committed Oct 2, 2015
1 parent a96d0fc commit c3057fb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
29 changes: 25 additions & 4 deletions tests/test-suite.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ function test_tevkori_get_srcset_array() {
$this->assertSame( $expected, $sizes );
}

function test_tevkori_get_srcset_array_random_size_name() {
// make an image
$id = $this->_test_img();
$sizes = tevkori_get_srcset_array( $id, 'foo' );

$year_month = date('Y/m');
$image = wp_get_attachment_metadata( $id );

$expected = array(
$image['sizes']['medium']['width'] => 'http://example.org/wp-content/uploads/' . $year_month = date('Y/m') . '/'
. $image['sizes']['medium']['file'] . ' ' . $image['sizes']['medium']['width'] . 'w',
$image['sizes']['large']['width'] => 'http://example.org/wp-content/uploads/' . $year_month = date('Y/m') . '/'
. $image['sizes']['large']['file'] . ' ' . $image['sizes']['large']['width'] . 'w',
$image['width'] => 'http://example.org/wp-content/uploads/' . $image['file'] . ' ' . $image['width'] .'w'
);

$this->assertSame( $expected, $sizes );
}

function test_tevkori_get_srcset_array_no_date_upoads() {
// Save the current setting for uploads folders
$uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' );
Expand Down Expand Up @@ -280,7 +299,7 @@ function test_tevkori_get_srcset_array_false() {

function test_tevkori_get_srcset_array_no_width() {
// Filter image_downsize() output.
add_filter( 'image_downsize', array( $this, '_test_tevkori_get_srcset_array_no_width_filter' ) );
add_filter( 'wp_generate_attachment_metadata', array( $this, '_test_tevkori_get_srcset_array_no_width_filter' ) );

// Make our attachement.
$id = $this->_test_img();
Expand All @@ -290,14 +309,16 @@ function test_tevkori_get_srcset_array_no_width() {
$this->assertFalse( $srcset );

// Remove filter.
remove_filter( 'image_downsize', array( $this, '_test_tevkori_get_srcset_array_no_width_filter' ) );
remove_filter( 'wp_generate_attachment_metadata', array( $this, '_test_tevkori_get_srcset_array_no_width_filter' ) );
}

/**
* Helper funtion to filter image_downsize and return zero values for width and height.
*/
public function _test_tevkori_get_srcset_array_no_width_filter() {
return array( 'http://example.org/foo.jpg', 0, 0, false );
public function _test_tevkori_get_srcset_array_no_width_filter( $meta ) {
$meta['sizes']['medium']['width'] = 0;
$meta['sizes']['medium']['height'] = 0;
return $meta;
}

function test_tevkori_get_srcset_string() {
Expand Down
34 changes: 19 additions & 15 deletions wp-tevko-responsive-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,41 +179,45 @@ function tevkori_get_sizes_string( $id, $size = 'thumbnail', $args = null ) {
function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) {
$arr = array();

// See which image is being returned and bail if none is found.
if ( ! $img = wp_get_attachment_image_src( $id, $size ) ) {
return false;
}
// Get the intermediate size.
$image = image_get_intermediate_size( $id, $size );
// Get the post meta.
$img_meta = wp_get_attachment_metadata( $id );

// Break image data into url, width, and height.
list( $img_url, $img_width, $img_height ) = $img;
// Extract the height and width from the intermediate or the full size.
$img_width = ( $image ) ? $image['width'] : $img_meta['width'];
$img_height = ( $image ) ? $image['height'] : $img_meta['height'];

// If we have no width to work with, we should bail (see issue #118).
if ( 0 == $img_width ) {
// Bail early if the width isn't greater that zero.
if ( ! $img_width > 0 ) {
return false;
}

// Get the image meta data and bail if none is found.
if ( ! is_array( $img_meta = wp_get_attachment_metadata( $id ) ) ) {
return false;
// Use the url from the intermediate size or build the url from the metadata.
if ( ! empty( $image['url'] ) ) {
$img_url = $image['url'];
} else {
$uploads_dir = wp_upload_dir();
$img_file = ( $image ) ? path_join( dirname( $img_meta['file'] ) , $image['file'] ) : $img_meta['file'];
$img_url = $uploads_dir['baseurl'] . '/' . $img_file;
}

// Build an array with image sizes.
$img_sizes = $img_meta['sizes'];

// Add full size to the img_sizes array.
$img_sizes['full'] = array(
'width' => $img_meta['width'],
'height' => $img_meta['height'],
'file' => basename( $img_meta['file'] )
'file' => wp_basename( $img_meta['file'] )
);

// Calculate the image aspect ratio.
$img_ratio = $img_height / $img_width;

/*
* Images that have been edited in WordPress after being uploaded will
* contain a unique hash. We look for that hash and use it later to filter
* out images that are leftovers from previous renditions.
* contain a unique hash. Look for that hash and use it later to filter
* out images that are leftovers from previous versions.
*/
$img_edited = preg_match( '/-e[0-9]{13}/', $img_url, $img_edit_hash );

Expand Down

0 comments on commit c3057fb

Please sign in to comment.