88 * Plugin Name: RICG Responsive Images
99 * Plugin URI: http://www.smashingmagazine.com/2015/02/24/ricg-responsive-images-for-wordpress/
1010 * Description: Bringing automatic default responsive images to wordpress
11- * Version: 2.5.1
11+ * Version: 2.5.2
1212 * Author: The RICG
1313 * Author URI: http://responsiveimages.org/
1414 * License: GPL-2.0+
@@ -54,23 +54,31 @@ function tevkori_get_picturefill() {
5454 * @since 2.2.0
5555 *
5656 * @param int $id Image attachment ID.
57- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
57+ * @param string $size Optional. Name of image size. Default value: 'medium '.
5858 * @param array $args {
5959 * Optional. Arguments to retrieve posts.
6060 *
6161 * @type array|string $sizes An array or string containing of size information.
62+ * @type int $width A single width value used in the default `sizes` string.
6263 * }
6364 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
6465 */
65- function tevkori_get_sizes ( $ id , $ size = 'thumbnail ' , $ args = null ) {
66+ function tevkori_get_sizes ( $ id , $ size = 'medium ' , $ args = null ) {
6667
67- // See which image is being returned and bail if none is found.
68- if ( ! $ img = image_downsize ( $ id , $ size ) ) {
68+ // Try to get the image width from `$args` before calling `image_downsize()`.
69+ if ( is_array ( $ args ) && ! empty ( $ args ['width ' ] ) ) {
70+ $ img_width = (int ) $ args ['width ' ];
71+ } elseif ( $ img = image_downsize ( $ id , $ size ) ) {
72+ $ img_width = $ img [1 ];
73+ }
74+
75+ // Bail early if ``$image_width` isn't set.
76+ if ( ! $ img_width ) {
6977 return false ;
7078 }
7179
72- // Get the image width.
73- $ img_width = $ img [ 1 ] . 'px ' ;
80+ // Set the image width in pixels .
81+ $ img_width = $ img_width . 'px ' ;
7482
7583 // Set up our default values.
7684 $ defaults = array (
@@ -148,15 +156,16 @@ function tevkori_get_sizes( $id, $size = 'thumbnail', $args = null ) {
148156 * @since 2.2.0
149157 *
150158 * @param int $id Image attachment ID.
151- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
159+ * @param string $size Optional. Name of image size. Default value: 'medium '.
152160 * @param array $args {
153161 * Optional. Arguments to retrieve posts.
154162 *
155163 * @type array|string $sizes An array or string containing of size information.
164+ * @type int $width A single width value used in the default `sizes` string.
156165 * }
157166 * @return string|bool A valid source size list as a 'sizes' attribute or false.
158167 */
159- function tevkori_get_sizes_string ( $ id , $ size = 'thumbnail ' , $ args = null ) {
168+ function tevkori_get_sizes_string ( $ id , $ size = 'medium ' , $ args = null ) {
160169 $ sizes = tevkori_get_sizes ( $ id , $ size , $ args );
161170
162171 return $ sizes ? 'sizes=" ' . $ sizes . '" ' : false ;
@@ -166,47 +175,51 @@ function tevkori_get_sizes_string( $id, $size = 'thumbnail', $args = null ) {
166175 * Get an array of image sources candidates for use in a 'srcset' attribute.
167176 *
168177 * @param int $id Image attachment ID.
169- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
178+ * @param string $size Optional. Name of image size. Default value: 'medium '.
170179 * @return array|bool An array of of srcset values or false.
171180 */
172- function tevkori_get_srcset_array ( $ id , $ size = 'thumbnail ' ) {
181+ function tevkori_get_srcset_array ( $ id , $ size = 'medium ' ) {
173182 $ arr = array ();
174183
175- // See which image is being returned and bail if none is found .
176- if ( ! $ img = wp_get_attachment_image_src ( $ id , $ size ) ) {
177- return false ;
178- }
184+ // Get the intermediate size .
185+ $ image = image_get_intermediate_size ( $ id , $ size );
186+ // Get the post meta.
187+ $ img_meta = wp_get_attachment_metadata ( $ id );
179188
180- // Break image data into url, width, and height.
181- list ( $ img_url , $ img_width , $ img_height ) = $ img ;
189+ // Extract the height and width from the intermediate or the full size.
190+ $ img_width = ( $ image ) ? $ image ['width ' ] : $ img_meta ['width ' ];
191+ $ img_height = ( $ image ) ? $ image ['height ' ] : $ img_meta ['height ' ];
182192
183- // If we have no width to work with, we should bail (see issue #118) .
184- if ( 0 == $ img_width ) {
193+ // Bail early if the width isn't greater that zero .
194+ if ( ! $ img_width > 0 ) {
185195 return false ;
186196 }
187197
188- // Get the image meta data and bail if none is found.
189- if ( ! is_array ( $ img_meta = wp_get_attachment_metadata ( $ id ) ) ) {
190- return false ;
198+ // Use the url from the intermediate size or build the url from the metadata.
199+ if ( ! empty ( $ image ['url ' ] ) ) {
200+ $ img_url = $ image ['url ' ];
201+ } else {
202+ $ uploads_dir = wp_upload_dir ();
203+ $ img_file = ( $ image ) ? path_join ( dirname ( $ img_meta ['file ' ] ) , $ image ['file ' ] ) : $ img_meta ['file ' ];
204+ $ img_url = $ uploads_dir ['baseurl ' ] . '/ ' . $ img_file ;
191205 }
192206
193- // Build an array with image sizes.
194207 $ img_sizes = $ img_meta ['sizes ' ];
195208
196209 // Add full size to the img_sizes array.
197210 $ img_sizes ['full ' ] = array (
198211 'width ' => $ img_meta ['width ' ],
199212 'height ' => $ img_meta ['height ' ],
200- 'file ' => basename ( $ img_meta ['file ' ] )
213+ 'file ' => wp_basename ( $ img_meta ['file ' ] )
201214 );
202215
203216 // Calculate the image aspect ratio.
204217 $ img_ratio = $ img_height / $ img_width ;
205218
206219 /*
207220 * Images that have been edited in WordPress after being uploaded will
208- * contain a unique hash. We look for that hash and use it later to filter
209- * out images that are leftovers from previous renditions .
221+ * contain a unique hash. Look for that hash and use it later to filter
222+ * out images that are leftovers from previous versions .
210223 */
211224 $ img_edited = preg_match ( '/-e[0-9]{13}/ ' , $ img_url , $ img_edit_hash );
212225
@@ -248,10 +261,10 @@ function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) {
248261 * @since 2.3.0
249262 *
250263 * @param int $id Image attachment ID.
251- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
264+ * @param string $size Optional. Name of image size. Default value: 'medium '.
252265 * @return string|bool A 'srcset' value string or false.
253266 */
254- function tevkori_get_srcset ( $ id , $ size = 'thumbnail ' ) {
267+ function tevkori_get_srcset ( $ id , $ size = 'medium ' ) {
255268 $ srcset_array = tevkori_get_srcset_array ( $ id , $ size );
256269
257270 if ( count ( $ srcset_array ) <= 1 ) {
@@ -267,10 +280,10 @@ function tevkori_get_srcset( $id, $size = 'thumbnail' ) {
267280 * @since 2.1.0
268281 *
269282 * @param int $id Image attachment ID.
270- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
283+ * @param string $size Optional. Name of image size. Default value: 'medium '.
271284 * @return string|bool A full 'srcset' string or false.
272285 */
273- function tevkori_get_srcset_string ( $ id , $ size = 'thumbnail ' ) {
286+ function tevkori_get_srcset_string ( $ id , $ size = 'medium ' ) {
274287 $ srcset_value = tevkori_get_srcset ( $ id , $ size );
275288
276289 if ( empty ( $ srcset_value ) ) {
@@ -294,43 +307,41 @@ function tevkori_filter_content_images( $content ) {
294307 $ uploads_dir = wp_upload_dir ();
295308 $ path_to_upload_dir = $ uploads_dir ['baseurl ' ];
296309
297- preg_match_all ( '|<img ([^>]+ ' . $ path_to_upload_dir . '[^>]+)[\s?][\/?]>|i ' , $ content , $ matches );
310+ // Pattern for matching all images with a `src` from the uploads directory.
311+ $ pattern = '|<img ([^>]+ ' . preg_quote ( $ path_to_upload_dir ) . '[^>]+)>|i ' ;
312+ preg_match_all ( $ pattern , $ content , $ matches );
298313
299314 $ images = $ matches [0 ];
300315 $ ids = array ();
301316
302317 foreach ( $ images as $ image ) {
303318 if ( preg_match ( '/wp-image-([0-9]+)/i ' , $ image , $ class_id ) ) {
304319 (int ) $ id = $ class_id [1 ];
305- if ( $ id ) {
320+ if ( $ id ) {
306321 $ ids [] = $ id ;
307322 }
308323 }
309324 }
310325
311326 if ( 0 < count ( $ ids ) ) {
312- /**
327+ /*
313328 * Warm object caches for use with wp_get_attachment_metadata.
314329 *
315- * To avoid making a database call for each image, WP_Query is called
316- * as a single query with the IDs of all images in the post. This warms
317- * the object cache with the meta information for all images.
318- *
319- * This loop is not used directly.
330+ * To avoid making a database call for each image, a single query
331+ * warms the object cache with the meta information for all images.
320332 **/
321- $ attachments = new WP_Query (array (
322- 'post_type ' => 'attachment ' ,
323- 'posts_per_page ' => '-1 ' ,
324- 'post_status ' => 'inherit ' ,
325- 'post__in ' => $ ids ,
326- ));
333+ _prime_post_caches ( $ ids , false , true );
327334 }
328335
329- $ content = preg_replace_callback (
330- '|<img ([^>]+ ' . $ path_to_upload_dir . '[^>]+)[\s?][\/?]>|i ' ,
331- '_tevkori_filter_content_images_callback ' ,
332- $ content
333- );
336+ foreach ( $ matches [0 ] as $ k => $ image ) {
337+ $ match = array ( $ image , $ matches [1 ][$ k ] );
338+ $ needle = $ image ;
339+ $ replacement = _tevkori_filter_content_images_callback ( $ match );
340+ if ( false === $ replacement ) {
341+ continue ;
342+ }
343+ $ content = str_replace ( $ image , $ replacement , $ content );
344+ }
334345
335346 return $ content ;
336347}
@@ -348,7 +359,6 @@ function _tevkori_filter_content_images_callback( $image ) {
348359 }
349360
350361 list ( $ image_html , $ atts ) = $ image ;
351- $ id = $ size = false ;
352362
353363 // Bail early if a 'srcset' attribute already exists.
354364 if ( false !== strpos ( $ atts , 'srcset= ' ) ) {
@@ -366,31 +376,27 @@ function _tevkori_filter_content_images_callback( $image ) {
366376 }
367377
368378 // Grab ID and size info from core classes.
369- if ( preg_match ( '/wp-image-([0-9]+)/i ' , $ atts , $ class_id ) ) {
370- (int ) $ id = $ class_id [1 ];
371- }
372- if ( preg_match ( '/size-([^\s|"]+)/i ' , $ atts , $ class_size ) ) {
373- $ size = $ class_size [1 ];
374- }
379+ $ id = preg_match ( '/wp-image-([0-9]+)/i ' , $ atts , $ class_id ) ? (int ) $ class_id [1 ] : false ;
380+ $ size = preg_match ( '/size-([^\s|"]+)/i ' , $ atts , $ class_size ) ? $ class_size [1 ] : false ;
381+ $ width = preg_match ( '/ width="([0-9]+)"/ ' , $ atts , $ atts_width ) ? (int ) $ atts_width [1 ] : false ;
382+ $ height = preg_match ( '/ height="([0-9]+)"/ ' , $ atts , $ atts_height ) ? (int ) $ atts_height [1 ] : false ;
375383
376384 if ( $ id && false === $ size ) {
377- if ( preg_match ( '/ width="([0-9]+)"/ ' , $ atts , $ width ) && preg_match ( '/ height="([0-9]+)"/ ' , $ atts , $ height ) ) {
378- $ size = array (
379- (int ) $ width [1 ],
380- (int ) $ height [1 ]
381- );
382- }
385+ $ size = array (
386+ $ width ,
387+ $ height
388+ );
383389 }
384390
385391 /*
386392 * If attempts to parse the size value failed, attempt to use the image
387- * metadata to match the ` src` angainst the available sizes for an attachment.
393+ * metadata to match the ' src' angainst the available sizes for an attachment.
388394 */
389395 if ( ! $ size && ! empty ( $ id ) && $ meta = wp_get_attachment_metadata ( $ id ) ) {
390396
391397 preg_match ( '/src="([^"]+)"/ ' , $ atts , $ url );
392398
393- // Sanity check the ` src` value and bail early it doesn't exist.
399+ // Sanity check the ' src' value and bail early it doesn't exist.
394400 if ( ! $ url [1 ] ) {
395401 return $ image_html ;
396402 }
@@ -416,7 +422,18 @@ function _tevkori_filter_content_images_callback( $image ) {
416422
417423 // If we have an ID and size, try for 'srcset' and 'sizes' and update the markup.
418424 if ( $ id && $ size && $ srcset = tevkori_get_srcset_string ( $ id , $ size ) ) {
419- $ sizes = tevkori_get_sizes_string ( $ id , $ size );
425+
426+ // Pass height and width to `tevkori_get_sizes_string()`.
427+ $ args = array (
428+ 'width ' => $ width ,
429+ 'height ' => $ height ,
430+ );
431+
432+ $ sizes = tevkori_get_sizes_string ( $ id , $ size , $ args );
433+
434+ // Strip trailing slashes and whitespaces from the `$atts` string.
435+ $ atts = trim ( rtrim ( $ atts , '/ ' ) );
436+
420437 $ image_html = "<img " . $ atts . " " . $ srcset . " " . $ sizes . " /> " ;
421438 };
422439
0 commit comments