- This topic has 18 replies, 3 voices, and was last updated 3 years ago by
David.
-
AuthorPosts
-
March 10, 2023 at 7:57 am #2563118
victor
Had images served as ‘Medium-large’ but on Mobile, long load times. Switched to ‘Medium’, site loads quickly!
However, I want the image on my ‘Feature Post’ on desktop to be ‘Medium-large’ or something bigger than ‘Medium’.
So,
Is there a way to serve ‘Medium’ size images in mobile and serve ‘Medium-large’ on desktop? Is there another work around?
March 11, 2023 at 5:12 am #2563887David
StaffCustomer SupportHi there,
WordPress provides the
wp_calculate_image_sizesfilter hook, that you can use with a PHP snippet to rewrite theimgHTMLsizesattribute, which is a kind of prompt for the browser to choose a specific size image.Try adding this snippet:
function db_modify_srcset_sizes($sizes, $size) { $sizes = '(max-width: 420px) 300px, (min-width: 421px) 768px, (min-width: 769px) 1024px, 100vw'; return $sizes; } add_filter('wp_calculate_image_sizes', 'db_modify_srcset_sizes', 10 , 2);It has 3 breakpoints set, with the 300px image set for mobiles.
This explains how to add PHP: https://docs.generatepress.com/article/adding-php/
March 11, 2023 at 6:27 am #2563945victor
Thanks David.
I implemented it but I’m not sure how to use it still. I modified it using different values and haven’t seen any difference, am I supposed to? I’ve been looking through the inspect elements tab on the images — not fully clear on what’s going on. It seems like the image is sized through width and height dimensions?
March 11, 2023 at 8:07 am #2564130David
StaffCustomer SupportWhere did you add the code ?
March 11, 2023 at 8:23 am #2564152victor
I used the code snippets plugin, setting was ‘Run Snippet Everywhere’.
March 12, 2023 at 2:35 am #2564660David
StaffCustomer SupportAre you using any optimization or caching plugins on the site?
March 12, 2023 at 6:02 am #2564791victor
Using Smush and Converter for Media. Deactivated post and played around with the above snippet, no dice!
March 12, 2023 at 7:07 am #2564845David
StaffCustomer SupportIs there any way i can see it with those plugins out the way and after any caches are cleared?
Note: those plugins may rewrite image HTML, if thats the case it may not be possible to make this work.
March 12, 2023 at 7:32 am #2564867victor
Sure! I’ll deactivate them for the next or two / when I hear back from you!
I attached some additional ‘Private information’ which has a screenshot of my plugins.
March 12, 2023 at 11:56 am #2565170Ying
StaffCustomer SupportHi Victor,
I don’t see David’s code running on your site, can you make sure the PHP snippet is activated?
And can you try disabling all your other plugins for now to eliminate conflicts?
If it’s a live site with traffic, I would recommend creating a staging site to do the testing.
March 12, 2023 at 12:13 pm #2565181victor
Just activated the script. The plugins currently activated have no effect on pages directly unless I’m mistaken but I can disable them if you want me too.
March 12, 2023 at 4:45 pm #2565285Ying
StaffCustomer SupportThe snippet is working, the image size attribute has been changed:
https://www.screencast.com/t/oqQ1OFNgzFJTCan you try modifying
(max-width: 420px) 300pxto(max-width: 420px) 150pxof the code to test?March 13, 2023 at 4:42 am #2565788victor
Modified, I am seeing the change in inspect as your picture shows but no visual change in UI.
March 13, 2023 at 10:11 am #2566288David
StaffCustomer SupportChange your image sizes to Medium Large, the code i provided simply prompts the browser to load the smaller images when viewed on smaller screen.
March 13, 2023 at 1:49 pm #2566486victor
Oh gosh, I’m an idiot, thank you! I have a few questions!
1) The code snippet above “(min-width: 421px) 768px” effects large screens even though there is “(min-width: 769px)”, is this intended?
2) I’m assuming there is an upper limit, i.e., if I put “100000 pixels”, it’ll just render the image in max detail?
Edit:
Seems this depends on a lot of factors according to your response to others3) Any other recommendations for optimizing in this way and keeping images looking as detailed as possible?
-
AuthorPosts
- You must be logged in to reply to this topic.