wp_get_attachment_image_src() regenerate custom size images everytime

Question

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?

0
, saxap 6 years 2015-04-20T14:38:33-05:00 0 Answers 88 views 0

Leave an answer

Browse
Browse