Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Created October 19, 2023 23:15

Revisions

  1. robertdevore created this gist Oct 19, 2023.
    23 changes: 23 additions & 0 deletions get-explicit-image-sizes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?php
    /**
    * Get explicit image sizes
    *
    * @param string $image_url The image URL - https:// ...
    *
    * @return string
    */
    function get_explicit_image_sizes( $image_url ) {

    $img_explicit = '';

    if ( $image_url ) {
    $logo_media_id = get_media_id_from_url( $image_url );
    $img_dimensions = get_image_sizes_by_id( $logo_media_id );

    if ( $img_dimensions ) {
    $img_explicit = ' width="' . $img_dimensions['width'] . '" height="' . $img_dimensions['height'] . '" ';
    }
    }

    return $img_explicit;
    }