seiya.n1987
10/23/2019 - 2:06 AM

[test] 画像サイズの比較によるクラスの判定

<?php
/***
 *
 * #画像クラスの判定
 * 既に設定された画像の縦横の長さを
 *
 * @param array $img width, heightのキーを持つ画像配列
 * @param string $operator 比較演算子
 * @param string $base_class 初期設定のhtmlクラス名
 * @param string $change_class 比較結果がtrueだった場合に書き換えるhtmlクラス名
 * @return string htmlクラス名
 */
function switch_class_by_image_size( $img, $operator, $base_class, $change_class ) {

    $switch_class = $base_class;
    $width = 1;
    $height = 1;
    $result = false;

    if( is_array( $img ) && array_key_exists( 'width', $img ) && array_key_exists( 'height', $img ) ) {

        $width = (int)$img['width'];
        $height = (int)$img['height'];

        $comparison = bccomp( $width, $height );

        switch ( $operator ) {
            case '>':
                if( $comparison === 1 ) {
                    $result = true;
                }
                break;

            case '<':
                if( $comparison === -1 ) {
                    $result = true;
                }
                break;

            case '=':
                if( $comparison === 0 ) {
                    $result = true;
                }
                break;

            case '>=':
                if( in_array( $comparison, array( 0, 1 ) ) ) {
                    $result = true;
                }
                break;

            case '<=':
                if( in_array( $comparison, array( 0, -1 ) ) ) {
                    $result = true;
                }
                break;

            default:
                break;

        }

    }

    if( $result ) {
        $switch_class = $change_class;
    }

    return $switch_class;
}