0

i try to make simple gallery from post's attachment images with custom sizes.

i wrote simple loop:

$original = array();
$gallery = array();
$gallery_min = array();

$imgs_args = array(
    'post_status'    => null,
    'post_type'      => 'attachment',
    'post_parent'    => get_the_ID(),
    'post_mime_type' => 'image',
    'order'          => 'ASC',
    'numberposts'  => 99999
);
$post_images = get_posts($imgs_args);
if ($post_images) :
    foreach ($post_images as $a) :
        $original[] = wp_get_attachment_image_src($a->ID, 'gallery_origin');
        $gallery[] = wp_get_attachment_image_src($a->ID, 'gallery');
        $gallery_min[] = wp_get_attachment_image_src($a->ID, 'gallery_min');
    endforeach;
endif;

but performences page becomes so poor. i check creation time of my custom size images and saw that it time equals page loading time. It means that wp_get_attachment_image_src() regenerate my images everytime when page is loading.

i try to use image_get_intermediate_size() function to check custom size image exists:

foreach ($post_images as $a) :
    $img = image_get_intermediate_size( $a->ID, 'gallery_origin' );
    if ($img) {
        $original[] = $img['url'];
    } else {
        $img = wp_get_attachment_image_src($a->ID, 'gallery_origin');
        $original[] = $img[0];
    }
    $img = image_get_intermediate_size( $a->ID, 'gallery2' );
    if ($img) {
        $gallery[] = $img['url'];
    } else {
        $img = wp_get_attachment_image_src($a->ID, 'gallery2');
        $gallery[] = $img[0];
    }
    $img = image_get_intermediate_size( $a->ID, 'gallery_min' );
        if ($img) {
            $gallery_min[] = $img['url'];
        } else {
            $img = wp_get_attachment_image_src($a->ID, 'gallery_min');
            $gallery_min[] = $img[0];
        }
endforeach;

and permormance becomes great.

is the wp_get_attachment_image_src() function should not to check already generated image before returning the array?

1 Answer 1

0

No, the wp_get_attachment_image_src function calls image_downsize, which in turn calls image_get_intermediate_size itself. It doesn't cause generation of the images, it is simply getting the relevant images from the meta and choosing the correct one to use.

2
  • ok, but anyway how it could happen, on other sites wp_get_attachment_image_src always work great..
    – saxap
    Apr 21, 2016 at 6:15
  • The way it could happen is if you have some other code causing it to happen. WP does not generate image sizes on-the-fly, only at the time of the image upload.
    – Otto
    Apr 21, 2016 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.