問題描述
我有 1000 張圖片。只有在需要時,我如何才能使 wordpress 生成縮略圖。例如,家用滑塊將僅使用 10 張圖像,我不希望其他 1000 張圖像生成縮略圖,因為它浪費了空間和資源。
有沒有辦法只有在需要的時候才能使用 add_image_size?
謝謝
更新如你所提到的不是真正的 add_image_size 它需要被觸發。當我使用 the_post_thumbnail(‘slider-thumb’) 時,啓動圖像調整大小是什麼?也許這個放緩的第一個圖像的視圖,但是這個視圖通常是由我生成的,當我實際審查的帖子,所以我不在乎。
所以在我的帖子,滑塊,博客拇指,投資組合縮略圖等我有 1000 個圖像,我只需要 10 個圖像調整大小的滑塊,我看到很多浪費的資源,以生成其他 990 圖像的縮略圖大小。
希望清楚現在,對不起我的英語
最佳解決方案
看看奧託的 Dynamic Image Resizer plugin
This plugin changes the way WordPress creates images to make it generate the images only when they are actually used somewhere, on the fly. Images created thusly will be saved in the normal upload directories, for later fast sending by the webserver. The result is that space is saved (since images are only created when needed), and uploading images is much faster (since it’s not generating the images on upload anymore).
次佳解決方案
把它放在你的主題功能文件中。它會阻止 Wordpress 創建任何東西,但上傳 3 默認大小。
當然後以特定大小請求圖像,該圖像尚未生成時,將僅創建一次圖像。
add_filter('image_downsize', 'ml_media_downsize', 10, 3);
function ml_media_downsize($out, $id, $size) {
// If image size exists let WP serve it like normally
$imagedata = wp_get_attachment_metadata($id);
if (is_array($imagedata) && isset($imagedata['sizes'][$size]))
return false;
// Check that the requested size exists, or abort
global $_wp_additional_image_sizes;
if (!isset($_wp_additional_image_sizes[$size]))
return false;
// Make the new thumb
if (!$resized = image_make_intermediate_size(
get_attached_file($id),
$_wp_additional_image_sizes[$size]['width'],
$_wp_additional_image_sizes[$size]['height'],
$_wp_additional_image_sizes[$size]['crop']
))
return false;
// Save image meta, or WP can't see that the thumb exists now
$imagedata['sizes'][$size] = $resized;
wp_update_attachment_metadata($id, $imagedata);
// Return the array for displaying the resized image
$att_url = wp_get_attachment_url($id);
return array(dirname($att_url) . '/' . $resized['file'], $resized['width'], $resized['height'], true);
}
add_filter('intermediate_image_sizes_advanced', 'ml_media_prevent_resize_on_upload');
function ml_media_prevent_resize_on_upload($sizes) {
// Removing these defaults might cause problems, so we don't
return array(
'thumbnail' => $sizes['thumbnail'],
'medium' => $sizes['medium'],
'large' => $sizes['large']
);
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。