Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Created October 19, 2023 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertdevore/21c436a50c7f6a8148a6416bf93c376a to your computer and use it in GitHub Desktop.
Save robertdevore/21c436a50c7f6a8148a6416bf93c376a to your computer and use it in GitHub Desktop.
<?php
/**
* Get image sizes by ID
*
* @param int $media_id The media ID
*
* @return array|bool
*/
function get_image_sizes_by_id( $media_id ) {
// Check if the media ID is valid.
if ( wp_attachment_is_image( $media_id ) ) {
// Get the image size information for the full size.
$image_size_info = wp_get_attachment_image_src( $media_id, 'full' );
if ( $image_size_info ) {
// Extract the width and height from the returned array
list( $url, $width, $height ) = $image_size_info;
// Return the dimensions as an array
return array(
'width' => $width,
'height' => $height
);
}
}
// If the media ID is not valid or doesn't exist, return false.
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment