画像サイズに応じてJPEG圧縮を変更する


8

ショート。「大きな」画像を90%圧縮し、「中」を60%圧縮したい。あなたが知っている多くのafを持っています、時々大きな画像は高圧縮の影響を受けますが、他の小さな画像はそうではありません。

この機能により、すべてのjpg画像をリサンプルできます

function custom_jpg_compression($args) {
    return 90;
}
add_filter('jpeg_quality', 'custom_jpg_compression');

画像サイズでフィルタリングするには?

回答:


11

非常に特別なフィルター

jpeg_qualityフィルタは、本当に特別なもの:それは、3つの異なる例で使用されますと、あなたは、決定するために第二引数を使用してきた場合、あなたがフィルターかどうかを使用します。

すべてをさせてはいけない

そのような特別なフィルターの主な問題は、それを削除しないと、後のアクションで起動する可能性があることです-最初のチェックの後で実行できるようにします。そのwp_save_image_file()ため、圧縮を変更するかどうかを確認するために、別のフィルターを取得する必要があります。別の保存プロセスで無効にするには、圧縮を変更する直前に削除します。

クールキッド

本当に奇妙なことは、WPはすべての保存プロセスに対してデフォルトの圧縮率90%(10%低下した品質)を使用することです。つまり、画像をアップロード/トリミング/編集するたびに、その品質が低下します...これは、アップロードするときに最良ではない画像の痛みです(多くの赤を含む画像でテストしてください)高コントラストの背景)。しかし ...本当にすごいのは、この動作を変更できることです。したがって、圧縮を変更したいのですが、コアの許容量よりもはるかに優れた品質を同時に向上させる必要があります。

/**
 * Alter the image compression, depending on case
 * 
 * @param  int $compression
 * @param  string $case
 * @return int $compression
 */
function wpse58600_custom_jpg_compression_cb( $compression, $case )
{
    global $size_switch;

    // Should only fire once - don't leave it in for later cases
    remove_filter( current_filter(), __FUNCTION__ );

    // Alter the compression, depending on the case
    switch ( $case )
    {
        case( 'edit_image' ) :
            // We only add the compression, if the switch triggered,
            // which means, that the size is smaller, than set in the main function.
            // 60 is the percentage value, that gets added as compression to the smaller images.
            $compression = $size_switch ? 60 : 100;
            break;

        case( 'image_resize' ) :
            // Better leave it on 100% for resize
            $compression = 100;
            break;

        case( 'wp_crop_image' ) :
            // Better leave it on 100% for crop
            // We already compressed it on the camera, the desktop/handheld device 
            // and the server previously. That's enough so far.
            $compression = 100;
            break;
    }

    return $compression;
}

/**
 * Alter the compression for JPEG mime type images
 * Checks for a specific min size of the image, before altering it
 * 
 * @param  string $image
 * @param  int $post_id 
 * @return string $image
 */
function wpse58600_custom_jpg_compression( $image, $post_id )
{
    global $size_switch;
    $size_switch = false;

    // Define the size, that stops adding a compression
    $trigger_size = 641;

    // Get the sizes
    $size_x = imagesx( $image );
    $size_y = imagesy( $image );

    // Add the filter only in case
    if ( $trigger_size < $size_x )
    {
        $size_switch = true;
    }
    add_filter( 'jpeg_quality', 'wpse58600_custom_jpg_compression_cb', 20, 2 );

    return $image;
}
add_filter( 'image_save_pre', 'wpse58600_custom_jpg_compression', 20, 2 );

編集: @toschoとの短い議論の後、コールバック全体を次のように減らすことができると彼は指摘しました:

function wpse58600_custom_jpg_compression_cb( $compression, $case )
{
    // Should only fire once - don't leave it in for later cases
    remove_filter( current_filter(), __FUNCTION__ );

    return ( $GLOBALS['size_switch'] && 'edit_image' === $case ) ? 60 : 100;
}

現在作業中のプラグインからコードを引き出したので、switchに設定を追加する必要がありましglobalた。OOPアプローチであるため、プラグインではを使用しないことにも注意する必要があります。上記↑で読むことができるコードは、主にプラグインからのコードを削減および変更したものであり、若干の残り物があり、後の読者のために説明するためのものであり、引き続き機能します。プラグインとして使用したい場合は、個人のユースケースに応じて最適化を行うことができます。


ノート:

さまざまなタスクの調査から$case、次の手順で複数のがトリガーされることがわかりました。

  • 回転:edit-image» image-resize(選択した任意のサイズの後の1倍-サムネイルなど)
  • ミラー:edit-image» image-resize(-"-)

つまり、のフィルターコールバックはjpeq_quality、画像の回転/ミラーリングに対して2倍、追加サイズに対して+1倍をトリガーします。したがって、100%未満の場合、品質が2度低下します。私はこのトピックについて多くの調査を行いましたが、どのような正確な関数がこの動作を引き起こすのかはまだ完全にはわかりません。


ups you got me、print_r / var_dump is not work。どうすれば出力できますか?
DarkGhostHunter 2012

を使用しecho '<pre>'.var_export( $image, true ).'</pre>';ます。exit;その後に置くと、ページのリロードなどでスキップされません
kaiser

くそー、森の中で失われた-それがどこに印刷されているのかわかりません 返される$ imageの前にそのコードがあります。
DarkGhostHunter

wpse58600_custom_jpg_compression()関数の先頭に追加します。
カイザー2012

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.