performance - wp_get_attachment_image_src() regenerate custom size images everytime

admin2025-01-08  3

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?

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?

Share Improve this question edited Apr 17, 2016 at 10:08 Jevuska 1,16010 silver badges21 bronze badges asked Apr 20, 2015 at 19:38 saxapsaxap 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269096a1326.html

最新回复(0)