WordPress

Uzyskiwanie najbliższego nazwanego rozmiaru obrazu z tablicy wymiarów

  • 25 stycznia, 2017
  • 3 min read
Uzyskiwanie najbliższego nazwanego rozmiaru obrazu z tablicy wymiarów


Okazało się, image_get_intermediate_size() było dość łatwe do dostosowania do moich potrzeb. Zamiast używać wp_get_attachment_metadata aby uzyskać dostępne rozmiary istniejącego obrazu, używam własnej funkcji, aby uzyskać wszystkie dostępne rozmiary obrazu (zasadniczo takie same jak proponowana tutaj funkcja). Następnie po prostu zwracam nazwę, a nie tablicę metadanych.

To działa, muszę jednak przeprowadzić dokładniejsze testy i być może będę musiał wprowadzić pewne zmiany (nie jestem pewien, czy naprawdę chcę zwrócić „miniaturę” dla rozmiarów mniejszych niż „miniatura” na przykład) …

Oto cała moja funkcja (jest to statyczna metoda klasy użytkowej):

/**
 * Return the closest named size from an array of width and height values.
 *
 * Based off of WordPress's image_get_intermediate_size()
 * If the size matches an existing size then it will be used. If there is no
 * direct match, then the nearest image size larger than the specified size
 * will be used. If nothing is found, then the function will return false.
 * Uses get_image_sizes() to get all available sizes.
 *
 * @param  array|string $size   Image size. Accepts an array of width and height (in that order).
 * @return false|string $data   named image size e.g. 'thumbnail'.
 */
public static function get_named_size( $size ) {

    $image_sizes = self::get_image_sizes();
    $data = array();

    // Find the best match when '$size' is an array.
    if ( is_array( $size ) ) {
        $candidates = array();

        foreach ( $image_sizes as $_size => $data ) {

            // If there's an exact match to an existing image size, short circuit.
            if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
                $candidates[ $data['width'] * $data['height'] ] = array( $_size, $data );
                break;
            }

            // If it's not an exact match, consider larger sizes with the same aspect ratio.
            if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
                if ( wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] ) ) {
                    $candidates[ $data['width'] * $data['height'] ] = array( $_size, $data );
                }
            }
        }

        if ( ! empty( $candidates ) ) {
            // Sort the array by size if we have more than one candidate.
            if ( 1 < count( $candidates ) ) {
                ksort( $candidates );
            }

            $data = array_shift( $candidates );
            $data = $data[0];
        /*
         * When the size requested is smaller than the thumbnail dimensions, we
         * fall back to the thumbnail size to maintain backwards compatibility with
         * pre 4.6 versions of WordPress.
         */
        } elseif ( ! empty( $image_sizes['thumbnail'] ) && $image_sizes['thumbnail']['width'] >= $size[0] && $image_sizes['thumbnail']['width'] >= $size[1] ) {
            $data="thumbnail";
        } else {
            return false;
        }

    } elseif ( ! empty( $image_sizes[ $size ] ) ) {
        $data = $size;
    }

    // If we still don't have a match at this point, return false.
    if ( empty( $data ) ) {
        return false;
    }

    return $data;
}

Którego mogę użyć (wystarczająco blisko), jak początkowo chciałem:

$image_size = My_Utility_Class::get_named_size( array( 150, 150 ) ); // returns 'thumbnail'


Źródło

Warto przeczytać!  rest api — Jak uzyskać niestandardowe pola w poście po opublikowaniu