wp_get_image_editor_output_format()


WordPressのwp_get_image_editor_output_format()関数は、画像エディタの出力形式を取得するために使用されます。この関数は、編集された画像がどの形式で保存されるかを確認する際に便利です。

構文

wp_get_image_editor_output_format( WP_Image_Editor $image_editor );
  • $image_editor (WP_Image_Editor) — 画像エディタオブジェクト。

例1: 現在の出力形式を取得する

このコードは、画像エディタの現在の出力形式を取得して表示します。

<?php
$image_editor=wp_get_image_editor('path/to/image.jpg');
if(!is_wp_error($image_editor)){
    echo wp_get_image_editor_output_format($image_editor);
}
?>

例2: 出力形式に基づいて処理を行う

出力形式が「jpeg」の場合に特定の処理を行います。

<?php
$image_editor=wp_get_image_editor('path/to/image.jpg');
if(!is_wp_error($image_editor)){
    $format=wp_get_image_editor_output_format($image_editor);
    if($format==='jpeg'){
        // JPEG用の処理
    }
}
?>

例3: 編集後の画像を特定のフォーマットで保存する

編集された画像をフォーマット「png」で保存します。

<?php
$image_editor=wp_get_image_editor('path/to/image.jpg');
if(!is_wp_error($image_editor)){
    $image_editor->set_mime_type('image/png');
    $image_editor->save('path/to/new-image.png');
}
?>

例4: サポートされているフォーマットをチェックする

画像エディタが特定のフォーマットをサポートしているかを確認します。

<?php
$image_editor=wp_get_image_editor('path/to/image.jpg');
if(!is_wp_error($image_editor)){
    $supported_formats=$image_editor->get_supported_mime_types();
    if(in_array('image/webp',$supported_formats)){
        // WebPフォーマットがサポートされています
    }
}
?>

例5: 編集後の画像を動的に処理する

画像の出力形式に基づいて異なる処理を動的に適用します。

<?php
$image_editor=wp_get_image_editor('path/to/image.jpg');
if(!is_wp_error($image_editor)){
    $format=wp_get_image_editor_output_format($image_editor);
    switch($format){
        case 'jpeg':
            // JPEG処理
            break;
        case 'png':
            // PNG処理
            break;
        case 'gif':
            // GIF処理
            break;
    }
}
?>

注意事項

  • この関数を使用する際、画像エディタが正しく初期化されていることを確認してください。
  • 無効なファイルパスやサポートされていないフォーマットが原因でエラーが発生する可能性があります。

関連機能: