Skip to content

Commit

Permalink
Use the image url to get the ID and size
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspermdegroot committed Sep 15, 2015
1 parent 3208c96 commit 82ca815
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions wp-tevko-responsive-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,46 +331,39 @@ function tevkori_filter_content_images_callback( $matches ) {
$sizes = $srcset = '';

// Check if srcset attribute is not already present.
if ( ! preg_match( '/srcset="([^"]+)"/i', $atts ) ) {
if ( false !== strpos( 'srcset="', $atts ) ) {

// Grab the image ID from the core class.
preg_match( '/wp-image-([0-9]+)/i', $atts, $id );
// Get the url of the original image.
preg_match( '/src="(.+?)(\-([0-9]+)x([0-9]+))?(\.[a-zA-Z]{3,4})"/i', $atts, $url_matches );

$url = $url_matches[1] . $url_matches[5];

// Get the image ID.
$id = attachment_url_to_postid( $url );

if ( $id ) {
$id = (int) $id[1];

// Grab the size name from the core class.
preg_match( '/size-([^\s|"]+)\s|"/i', $atts, $size );

// If a class with size name is present, use it.
if ( $size ) {
$size = $size[1];

// Otherwise create an array with the values from the width and height attributes.
} else {
preg_match( '/width="([0-9]+)"/', $atts, $width );
preg_match( '/height="([0-9]+)"/', $atts, $height );

// Use the width and height from the image url.
if ( $url_matches[3] && $url_matches[4] ) {
$size = array(
(int) $width[1],
(int) $height[1]
(int) $url_matches[3],
(int) $url_matches[4]
);
} else {
$size = 'full';
}

if ( $size ) {

// Get the srcset string.
$srcset_string = tevkori_get_srcset_string( $id, $size );
// Get the srcset string.
$srcset_string = tevkori_get_srcset_string( $id, $size );

if ( $srcset_string ) {
$srcset = ' ' . $srcset_string;
if ( $srcset_string ) {
$srcset = ' ' . $srcset_string;

// Get the sizes string.
$sizes_string = tevkori_get_sizes_string( $id, $size );
// Get the sizes string.
$sizes_string = tevkori_get_sizes_string( $id, $size );

if ( $sizes_string && ! preg_match( '/sizes="([^"]+)"/i', $atts ) ) {
$sizes = ' ' . $sizes_string;
}
if ( $sizes_string && ! preg_match( '/sizes="([^"]+)"/i', $atts ) ) {
$sizes = ' ' . $sizes_string;
}
}
}
Expand Down

0 comments on commit 82ca815

Please sign in to comment.