Support

Account

Home Forums General Issues Fallback image size when using wp_get_attachment_image_src

Solved

Fallback image size when using wp_get_attachment_image_src

  • Hi there,

    I am using ACF to pull images using ‘wp_get_attachment_image_src’ as such:

    <?php $image = wp_get_attachment_image_src(get_field('image'), 'large'); ?>
    <img src="<?php echo $image[0]; ?>" alt="" />

    Whatever image size I enter (here it is ‘large’, but it could be a custom image size), if the image that has been uploaded was smaller than the set size, that size will not have been created and WordPress will use the full, original, uncompressed image as a fallback.

    This is very undesirable, as the image will often have no compression and be a very large filesize unnecessarily.

    What are the workarounds for this? Can I make WordPress create a full pixel-size, yet jpeg compressed image and make it use that as the fallback?

    Thanks in advance for your help!

  • Hi @lechatlisse

    If the ‘large’ size doesn’t exist, then the uploaded image must be smaller than ‘large’. Therefore, the image returned (full size) will not be a “very large filesize unnecessarily”.

    This aside, why not use image fiedl setting of ‘image object’ for the return type.
    This way, you can look in the ‘sizes’ array at all the URLs and sizes to make sure you get the correct fallback data.

    Thanks
    E

  • Wow, ok, I hadn’t even realised what Image Object was/did.

    I’ll look into it, thanks again!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Fallback image size when using wp_get_attachment_image_src’ is closed to new replies.