問題描述

我正在使用 WordPress Multisite 構建一個會員網站。是否可以限制根據所選模板生成的圖像數量?

我已經嘗試了以下代碼行在圖庫模板上生成某些圖像:

// Generate on all uploads
add_theme_support('post-thumbnails');
set_post_thumbnail_size( 1440, 350, true );
add_image_size( 'standard_box', 450, 215, true );
add_image_size( 'default_image', 691, 9999 );

// Generate on gallery template only
if ( is_page_template('page-gallery.php') ) {
add_image_size( 'gallery', 900, 9999 );
add_image_size( 'gallery_thumb', 450, 450, true );
}

這沒有奏效。我做了一些研究,似乎沒有找到任何關於這個問題。如果你能指出我正確的方向,我真的很感激。

最佳解決方案

這對我來説一直是一個 bugb,因為缺少 on-demand 圖像大小,以及隨後的文件數量,如果你有很多的大小!

我可以看到你的努力背後的邏輯 – 麻煩的是,add_image_size 只能真正發揮在 point-of-upload 。因此,is_page_template(..)將始終為 false

一個快速的 google 挖掘 Aqua Resizer,一個旨在解決這個問題的腳本。而不是使用 add_image_size,您可以直接在主題中使用 aq_resize,如果圖像的大小不存在,則會創建並緩存 on-the-fly 。

事實上,我在幾個具有許多圖像大小的網站上使用了類似的,儘管不同的技術。您仍然保存 WordPress 為每個上傳的圖像生成每個大小的開銷 – 它們在請求時生成 on-the-fly(& Cached) 。不同之處在於,您可以像通常一樣使用 WP 的所有標準圖像功能和模板標籤!

另外,如 @Waqas 所提到的,當您從媒體庫中刪除圖像時,使用 Aqua Resizer 將離開孤立的文件。使用我的技術,所有文件將被刪除,因為它們被保存到數據庫& 被 WordPress 認可。

/**
 * Resize internally-registered image sizes on-demand.
 *
 * @link    http://wordpress.stackexchange.com/q/139624/1685
 *
 * @param   mixed   $null
 * @param   int     $id
 * @param   mixed   $size
 * @return  mixed
 */
function wpse_139624_image_downsize( $null, $id, $size ) {
    static $sizes = array(
        'post-thumbnail' => array(
            'height' => 350,
            'width'  => 1440,
            'crop'   => true,
        ),

        'standard_box' => array(
            'height' => 215,
            'width'  => 450,
            'crop'   => true,
        ),

        'default_image' => array(
            'height' => 9999,
            'width'  => 691,
            'crop'   => false,
        ),

        'gallery' => array(
            'height' => 900,
            'width'  => 9999,
            'crop'   => false,
        ),

        'gallery_thumb' => array(
            'height' => 450,
            'width'  => 450,
            'crop'   => true,
        ),
    );

    if ( ! is_string( $size ) || ! isset( $sizes[ $size ] ) )
        return $null;
    if ( ! is_array( $data = wp_get_attachment_metadata( $id ) ) )
        return $null;
    if ( ! empty( $data['sizes'][ $size ] ) )
        return $null;
    if ( $data['height'] <= $sizes[ $size ]['height'] && $data['width'] <= $sizes[ $size ]['width'] )
        return $null;
    if ( ! $file = get_attached_file( $id ) )
        return $null;

    $editor = wp_get_image_editor( $file );

    if ( ! is_wp_error( $editor ) ) {
        $data['sizes'] += $editor->multi_resize(
            array(
                $size => $sizes[ $size ],
            )
        );

        wp_update_attachment_metadata( $id, $data );
    }

    return $null;
}

add_filter( 'image_downsize', 'wpse_139624_image_downsize', 10, 3 );

在實踐中

wp_get_attachment_image( $id, 'gallery' ); // Resized if not already
wp_get_attachment_image_src( $id, 'standard_box' ); // Resized if not already
the_post_thumbnail(); // You get the idea!
// And so forth!

我打算把它變成一個插件,它會自動將所有的 add_image_size 調用轉換成 on-demand 調整大小,所以看這個空間!

次佳解決方案


免責聲明:

– 這個

實際上是一個答案。

旨在幫助您進一步研究該主題。

– 此外

反映了至少感覺到最近更頻繁地發生類似問題的類似問題。


關於 Wordpress 開發的這個主題的其他信息:

注意:列表沒有任何手段訂購和全面。

第三種解決方案

如果你想在飛行中創建拇指,你可以使用 Aqua image resizer,但這個迷你腳本有一個缺點。從庫中刪除圖像時,創建的縮略圖將不會被刪除。但它不是很大的事情。如果需要,您可以通過 SHH commands 這樣做

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。