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?
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.