Site logo

[Support request] How to serve ‘Medium’ size images in Mobile only?

Home Forums Support [Support request] How to serve ‘Medium’ size images in Mobile only?

Home Forums Support How to serve ‘Medium’ size images in Mobile only?

  • This topic has 18 replies, 3 voices, and was last updated 3 years ago by David.
Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #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?

    #2563887
    David
    Staff
    Customer Support

    Hi there,

    WordPress provides the wp_calculate_image_sizes filter hook, that you can use with a PHP snippet to rewrite the img HTML sizes attribute, 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/

    #2563945
    victor

    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?

    #2564130
    David
    Staff
    Customer Support

    Where did you add the code ?

    #2564152
    victor

    I used the code snippets plugin, setting was ‘Run Snippet Everywhere’.

    #2564660
    David
    Staff
    Customer Support

    Are you using any optimization or caching plugins on the site?

    #2564791
    victor

    Using Smush and Converter for Media. Deactivated post and played around with the above snippet, no dice!

    #2564845
    David
    Staff
    Customer Support

    Is 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.

    #2564867
    victor

    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.

    #2565170
    Ying
    Staff
    Customer Support

    Hi 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.

    #2565181
    victor

    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.

    #2565285
    Ying
    Staff
    Customer Support

    The snippet is working, the image size attribute has been changed:
    https://www.screencast.com/t/oqQ1OFNgzFJT

    Can you try modifying (max-width: 420px) 300px to (max-width: 420px) 150px of the code to test?

    #2565788
    victor

    Modified, I am seeing the change in inspect as your picture shows but no visual change in UI.

    #2566288
    David
    Staff
    Customer Support

    Change your image sizes to Medium Large, the code i provided simply prompts the browser to load the smaller images when viewed on smaller screen.

    #2566486
    victor

    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 others

    3) Any other recommendations for optimizing in this way and keeping images looking as detailed as possible?

Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.