• Resolved smartyp

    (@smartyp)


    I’ve set CRP to use the Thumbnail size of 300×300. The files for these thumbnails do exist.

    If I set the Thumbnail width/height to 300/300, and Thumbnail size attributes to ‘Use HMTL’ then I get output that just uses the full size image with height and width set to 300.

    If I remove the sizes and attributes then I get a srcset output (which does include the 300 image) but the web browser just downloads the full size image anyway.

    How do I get the smaller thumbnails to be used?

    P.S. I’ve tried turning ‘Hard crop thumbnails’ on and off but that didn’t help (the ‘300×300’ images are scaled not cropped, so typically 300 wide and not 300 height, but that shouldn’t make any difference right?).

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Ajay

    (@ajay)

    Please post your url for me to check this. Also what are your settings under thumbnail.

    Thread Starter smartyp

    (@smartyp)

    I can’t share the url, but let me know what details/source code would be useful.

    Settings are:
    Location of the post thumbnail: Display thumbnails inline with posts, after title
    Thumbnail size: medium (300×300)
    Thumbnail width: 300 (or blank as above)
    Thumbnail height: 300 (or blank as above)
    Hard crop thumbnails: off (tried on)
    Generate thumbnail sizes: off
    Thumbnail size attributes: Use HTML (tried CSS and none)
    Get first image: on

    Thread Starter smartyp

    (@smartyp)

    OK, I dug into the code a bit.

    [1] In e.g. crp_get_the_post_thumbnail, wp_get_attachment_image_src isn’t returning the correct thumbnail url. Not sure if this is a WP bug or intended behaviour, but if you pass the named thumbnail in (“medium” in this case) it works, but if you pass an explicit size (300×300 – as CRP is doing) it doesn’t. Both return the correct thumbnail image sizes (in the test case I was looking at, 300×164) but the source url returned is the full size image if you pass 300×300 and the correct thumbnail url if you pass “medium”.

    [2] Not 100% sure on this one yet – but this site is using a CDN for images. And in crp_get_attachment_id_from_url there is a check for attachment_url against baseurl – but in this case attachment_url = cdn.example.com which doesn’t match the baseurl of www.example.com. I think that causes some problems, but the full size image output is then also being served from the site itself and not the CDN.

    Hope that helps ??

    • This reply was modified 3 years, 8 months ago by smartyp.
    Plugin Author Ajay

    (@ajay)

    @smartyp

    I think you’ve hit the nail on the head on the wp_get_attachment_image_src. This calls image_downsize and then image_get_intermediate_size. The last one tries to find the correct size when an array is passed – which is the case here.

    I think at this stage is where it can’t find the correct image size it returns full – I think it’s because it can’t find the correct size of the same aspect ratio.

    What is the thumbnail size that you have selected in the Thumbnail settings page? Assume this is medium?

    Would you be OK making some edits to the media.php file – I’m thinking there might be a case of passing the size like medium which is selected in the settings page ($args[‘thumb_size’]) instead of the width and height.

    But if crp_thumbnail is selected and we don’t get a thumbnail to recheck with the the image array.

    Plugin Author Ajay

    (@ajay)

    On the CDN question, I’m actually not sure what the solution would be. I don’t know if there is a separate function you’ll need to replace the image of your site to the image of the cdn

    Thread Starter smartyp

    (@smartyp)

    I hard coded all the wp_get_attachment_image_src calls to use ‘medium’ instead of the width/height array, and that does fix the 1st problem. So the correct size thumbnails get output where featured images have been used (just not using the CDN).

    Re: CDN. I guess one solution might be to have a user option to set a base CDN url, which is then used a) for the check to see if an image is a media library image (i.e. image url is CDN url + ‘/wp-content/uploads’), and b) to also set all the image output URL’s.

    Plugin Author Ajay

    (@ajay)

    Are all the images in your site also on the CDN. Sorry basic question but I’ve never needed to use a CDN explicitly, I just use cloudflare’s free plan.

    https://github.com/WebberZone/contextual-related-posts/blob/master/includes/media.php#L358

    This allows you to set the attributes and in theory you could change the src attribute value to replace your site with the CDN URL?

    Thread Starter smartyp

    (@smartyp)

    Yes, all internal images do use the CDN.

    Ah, lots of useful filters, thanks ??

    So I’ve now used the crp_get_image_attributes filter to switch the output back to the CDN url. So posts using featured images now work as expected.

    For ‘use first image in the post’ (no featured image set) I also had to use the crp_thumb_url filter to switch the url from ‘www.’ to ‘cdn.’ so they would be treated as local urls. But I then hit a problem with wp_calculate_image_srcset (in crp_get_image_html) – which doesn’t return anything as there is no 300 x 300 image. So the full size image still gets output for these. It looks like wp_calculate_image_srcset only works on an explicit pixel size array too rather than any option to use named sizes!

    Plugin Author Ajay

    (@ajay)

    On the wp_calculate_image_srcset I think there are code changes that will be needed as you rightly said that the image can’t be found.

    The issue is that it can only find an image of the same proportion which won’t apply if only one of the criteria is met.

    I’ve been pondering this for quite a bit of time, but why are you not passing the correct image size in the settings page? Alternatively, have you considered enabling the cropping of the image size and generates the correct thumbnails?

    Thread Starter smartyp

    (@smartyp)

    I was trying to avoid creating even more thumbnail images by using the existing ones ??

    ‘Medium’ is one of the built-in WordPress sizes (in Settings/Media) – by default it’s a max 300px wide and max 300px height. But it doesn’t crop to exact size, it keeps ratio – so it’s rarely ever actually 300 x 300.

    Plugin Author Ajay

    (@ajay)

    I figured ??

    I’ll look into this for the next version to see how I can pick the correct size vs the the array of images if crp_thumbnail isn’t selected.

    I can’t promise this will definitely work, but I’ll likely also put a filter to pass the size parameter for wp_calculate_image_srcset so at least you can replace the image array with the size.

    Thread Starter smartyp

    (@smartyp)

    Thanks ??

    Plugin Author Ajay

    (@ajay)

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Thumbnail using full size image’ is closed to new replies.