Support

Account

Home Forums ACF PRO Are custom image sizes still available?

Solving

Are custom image sizes still available?

  • Hi all!

    I’m not sure if I’m missing something, but I’m pretty sure I used to be able to grab non-standard image sizes (set up using add_image_size()) out of ACF image fields that were set to return objects. It used to be in $field[“sizes”][“name_of_size”] for the URL, $field[“sizes”][“name_of_size-width”] for the width, etc.

    I’ve noticed that I can no longer seem to do that – potentially with the new version of ACF Pro? At the same time I’ve noticed that the option to return image fields now refers to ‘Array’ rather than ‘Object’ (which of course is more correct but I’m wondering if more has changed than just the name).

    Has anyone else come across this? Is this a new limitation? Something I’m missing?

    Thanks in advance for your responses.

    (I have confirmed that the image size is still set up and functioning correctly; it works fine under the ‘Preview size’ setting in ACF)

  • it is possible to use a non-standard image size.
    as far i can see you use the correct variables.

    $image = get_field('your_image_field');
    	$size = 'name_of_size';
    	$url = $image['sizes'][ $size ];
    	$width = $image['sizes'][ $size . '-width' ];
    	$height = $image['sizes'][ $size . '-height' ];

    just to get sure that your preferred size exist, what is inside your image variable?:

    $image = get_field('your_image_field');
    echo '<pre>';
    echo htmlspecialchars(print_r($image,true));
    echo '</pre>';	
  • Hey mediawerk, thanks very much for your reply.

    Yeah it looks like the size isn’t actually being returned – even though the size definitely does exist. I was wondering if it was perhaps a problem in ACF – or something that has changed? I’ve been using the same code in quite a few sites and it’s been working fine for some time, but I recently upgraded to ACF Pro…

    Here’s the output:

    Array
    (
        [ID] => 659
        [id] => 659
        [title] => MOT_WAPOL_D2_ 110
        [filename] => MOT_WAPOL_D2_-110.jpg
        [url] => http://###/uploads/2015/07/MOT_WAPOL_D2_-110.jpg
        [alt] => 
        [author] => 1
        [description] => 
        [caption] => 
        [name] => mot_wapol_d2_-110
        [date] => 2015-07-10 00:58:30
        [modified] => 2015-07-10 00:58:30
        [mime_type] => image/jpeg
        [type] => image
        [icon] => http://###/wp-includes/images/media/default.png
        [width] => 3504
        [height] => 2336
        [sizes] => Array
            (
                [thumbnail] => http://###/uploads/2015/07/MOT_WAPOL_D2_-110-150x150.jpg
                [thumbnail-width] => 150
                [thumbnail-height] => 150
                [medium] => http://###/uploads/2015/07/MOT_WAPOL_D2_-110-300x200.jpg
                [medium-width] => 300
                [medium-height] => 200
                [large] => http://###/uploads/2015/07/MOT_WAPOL_D2_-110-1024x683.jpg
                [large-width] => 960
                [large-height] => 640
            )

    As I mentioned before, the size is coming through fine everywhere else, including in the ‘Preview size’ setting in the Image field settings in ACF.

    However… I think I just found the problem – possibly a bug! I’m actually using a field on an options page (so my code is $image = get_field('my_image',"options");). When I don’t use it on an options page, all the sizes come through ok!

    Is anyone else able to replicate this?

  • Did the image sizes that you are missing exist when the image was originally uploaded?

    I just did some testing. It is possible for a field to not return some image sizes, if the image size you’re looking for did not exist when the image was created. This is what I did.

    I set up an image field, added an image to it. On the front end get_field returned the image array with all image sizes.

    I then added an new image size. When I reloaded the page this new image size was not included in the image array. It did not exist when I originally uploaded the image.

    I used plugin called AJAX Thumbnail Rebuild to rebuild the images. When I reloaded the page the new image size was included.

  • get custom image size from option page is something that (should) work πŸ˜‰
    why? => because i had a site where i use that πŸ™‚ (crated a few weeks ago)

    but as john say try to recreate the custom size. (with help of plugin, or replace image with a new uploaded and look if that works)

    if that not works, you may add the add_image_size at a place where option page is not able to access it, or something else is strange

  • Ok it’s 2021 and I’m still seeing this issue. If I get an image field from an option fieldset with the field set to return an array the output only includes the standard WordPress Images sizes in the [‘sizes’]. All of the custom image sizes are missing from the array. The image 100% exists and was generated on upload. But I cannot access it since get_field(‘my_image’,’option’) does not have my custom image in the [‘sizes’] array. I’ve deleted. Re-uploaded. Re-generated. Nothing. Weird.

  • Ok so although it’s not in the “sizes” array you can of course still access the image size with a WordPress function. So instead of trying to get a custom image size from an option field but directly referencing the “sizes” array i.e.:

    $image['sizes']['banner']

    I’m just doing

    wp_get_attachment_image_src($image['ID'], 'banner')

    This will output an array. The first item in the array (0) with be your image URL

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.