Skip to content

Commit

Permalink
Merge pull request #122 from ResponsiveImagesCG/dev
Browse files Browse the repository at this point in the history
Version 2.3.1
  • Loading branch information
tevko committed May 27, 2015
2 parents 724c8d3 + 664d6e2 commit ea5e1e6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,17 @@ We use a hook because if you attempt to dequeue a script before it's enqueued, w

##Version

2.3.0
2.3.1

##Changelog

- First char no longer stripped from file name if there's no slash
- Adding test for when uploads directory not organized by date
- Don't calculate a srcset when the image data returns no width
- Add test for image_downsize returning 0 as a width

**2.3.0**

- Improved performance of `get_srcset_array`
- Added advanced image compression option (available by adding hook to functions.php)
- Duplicate entires now filtered out from srcset array
Expand Down
48 changes: 48 additions & 0 deletions tests/test-suite.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,32 @@ function test_tevkori_get_srcset_array() {
$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' );

// Disable date organized uploads
update_option( 'uploads_use_yearmonth_folders', 0 );

// make an image
$id = $this->_test_img();
$sizes = tevkori_get_srcset_array( $id, 'medium' );

$image = wp_get_attachment_metadata( $id );
$filename_base = substr( $image['file'], 0, strrpos($image['file'], '.png') );

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

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

// Leave the uploads option the way you found it.
update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders );
}

function test_tevkori_get_srcset_array_thumb() {
// make an image
$id = $this->_test_img();
Expand All @@ -171,6 +197,28 @@ function test_tevkori_get_srcset_array_false() { // make an image
$this->assertFalse( $sizes );
}

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' ) );

// Make our attachement.
$id = $this->_test_img();
$srcset = tevkori_get_srcset_array( $id, 'medium' );

// The srcset should be false
$this->assertFalse( $srcset );

// Remove filter.
remove_filter( 'image_downsize', 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 );
}

function test_tevkori_get_srcset_string() {
// make an image
$id = $this->_test_img();
Expand Down
12 changes: 10 additions & 2 deletions wp-tevko-responsive-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: RICG Responsive Images
* Plugin URI: http://www.smashingmagazine.com/2015/02/24/ricg-responsive-images-for-wordpress/
* Description: Bringing automatic default responsive images to wordpress
* Version: 2.3.0
* Version: 2.3.1
* Author: The RICG
* Author URI: http://responsiveimages.org/
* License: GPL-2.0+
Expand Down Expand Up @@ -153,6 +153,11 @@ function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) {
// Break image data into url, width, and height.
list( $img_url, $img_width, $img_height ) = $img;

// If we have no width to work with, we should bail (see issue #118).
if ( 0 == $img_width ) {
return false;
}

// Get the image meta data.
$img_meta = wp_get_attachment_metadata( $id );

Expand All @@ -163,8 +168,11 @@ function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) {
$img_sizes['full'] = array(
'width' => $img_meta['width'],
'height' => $img_meta['height'],
'file' => substr( $img_meta['file'], strrpos( $img_meta['file'], '/' ) + 1 )
'file' => $img_meta['file']
);
if ( strrpos( $img_meta['file'], '/' ) !== false ) {
$img_sizes['full']['file'] = substr( $img_meta['file'], strrpos( $img_meta['file'], '/' ) + 1 );
}

// Get the image base url.
$img_base_url = substr( $img_url, 0, strrpos( $img_url, '/' ) + 1 );
Expand Down

0 comments on commit ea5e1e6

Please sign in to comment.