Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

srcset missing for some cropped sizes #217

Closed
solange opened this issue Oct 21, 2015 · 8 comments
Closed

srcset missing for some cropped sizes #217

solange opened this issue Oct 21, 2015 · 8 comments

Comments

@solange
Copy link

solange commented Oct 21, 2015

Hello,
I'm using this in a template:
< img src="'.$image[url].'" '.tevkori_get_srcset_string( $image['ID'], "img_920x522"). '>
where the image size is set to cropped 'true', and it works very well for most images.

However, I have an image where the original size is 720x960, and the image name at my default size as per above is myimage-720x522.jpg in which case tevkori_get_srcset_string returns nothing, I guess because it can't find the exact size/image name. What's the work around for those situations so I can get a full srcset? Thanks you

@tevko
Copy link
Member

tevko commented Oct 21, 2015

Hey Solange, the name parameter in the srcset function should be a string keyword for the image. WordPress includes 4 by default, thumbnail, medium, large, and full. If you have explicitly set another image size, you would do so by giving it a name, similar to the default WordPress sizes. In your current case, you may want to leave the size parameter blank, as it passes in medium by default.

@solange
Copy link
Author

solange commented Oct 21, 2015

Thanks Tevko for replying so promptly, I have set the new image size like this:
add_image_size('th_921x522', 921, 522, true);
And I'm bringing it into the template like this:
$img_size = 'th_921x522';

'< img  src="'.$image[url].'" '.tevkori_get_srcset_string( $image['ID'], $img_size) . ' [description, alt etc] > ';

where the exact image size exists, ie it was cropped at both width / height, then I get this in the html:

< img src="http://nta.local/wp-content/uploads/2015/08/banner4.jpg" srcset="http://nta.local/wp-content/uploads/2015/08/banner4-921x522.jpg 921w, http://nta.local/wp-content/uploads/2015/08/banner4-676x384.jpg 676w, http://nta.local/wp-content/uploads/2015/08/banner4-160x90.jpg 160w" sizes="(max-width: 921px) 100vw, 921px" description="" title="" alt="" >

But where 'image-921x522.jpg' does not exist because it was cropped in one direction only, ie image-720x522.jpg as per original comment, than i get this in the html:

< img src="http://nta.local/wp-content/uploads/2015/09/BlueStalactite.jpg" sizes="(max-width: 720px) 100vw, 720px" description="" title="" alt="" > 

However if I use a non-cropped size (eg, "large") then it works fine.

@tevko
Copy link
Member

tevko commented Oct 21, 2015

@solange this behavior makes sense, because we filter out sizes that don't have the same aspect ratio as the original image

@joemcgill
Copy link
Member

It's interesting that the sizes attribute is showing up but the srcset attribute is not. @solange what version of the plugin are you running?

@solange
Copy link
Author

solange commented Oct 21, 2015

version 2.5.2
So the work around would be to use a non-cropped image size in those cases where I really want the srcset, correct?

@joemcgill
Copy link
Member

Correct. For a srcset to be useful, you need multiple image sources with identical height/width ratios.

@solange
Copy link
Author

solange commented Oct 21, 2015

Ok many thanks joemcgill & tevko. Cheers

@solange solange closed this as completed Oct 21, 2015
@jaspermdegroot
Copy link
Member

It's interesting that the sizes attribute is showing up but the srcset attribute is not. @solange what version of the plugin are you running?

We fixed that in the WordPress core patch by first checking if we have both srcset and sizes before adding any of the two attributes. Those changes will be applied to the plugin (version 2.6) as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants