wp_getimagesize( string $filename, array $image_info = null ): array|false

Allows PHP’s getimagesize() to be debuggable when necessary.

Parameters

$filenamestringrequired
The file path.
$image_infoarrayoptional
Extended image information (passed by reference).

Default:null

Return

array|false Array of image information or false on failure.

Source

	 * @since 6.7.0
	 *
	 * @param int|null $post_id The result of the post ID lookup. Null to indicate
	 *                          no lookup has been attempted. Default null.
	 * @param string   $url     The URL being looked up.
	 */
	$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );
	if ( null !== $post_id ) {
		return (int) $post_id;
	}

	$dir  = wp_get_upload_dir();
	$path = $url;

	$site_url   = parse_url( $dir['url'] );
	$image_path = parse_url( $path );

	// Force the protocols to match if needed.
	if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
		$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );
	}

	if ( str_starts_with( $path, $dir['baseurl'] . '/' ) ) {
		$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
	}

	$sql = $wpdb->prepare(
		"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
		$path
	);

	$results = $wpdb->get_results( $sql );
	$post_id = null;

	if ( $results ) {
		// Use the first available result, but prefer a case-sensitive match, if exists.
		$post_id = reset( $results )->post_id;

		if ( count( $results ) > 1 ) {
			foreach ( $results as $result ) {
				if ( $path === $result->meta_value ) {
					$post_id = $result->post_id;
					break;
				}
			}
		}
	}

	/**
	 * Filters an attachment ID found by URL.
	 *
	 * @since 4.2.0
	 *
	 * @param int|null $post_id The post_id (if any) found by the function.
	 * @param string   $url     The URL being looked up.
	 */
	return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url );
}

/**
 * Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view.
 *
 * @since 4.0.0
 *
 * @return string[] The relevant CSS file URLs.
 */
function wpview_media_sandbox_styles() {
	$version        = 'ver=' . get_bloginfo( 'version' );
	$mediaelement   = includes_url( "js/mediaelement/mediaelementplayer-legacy.min.css?$version" );
	$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" );

	return array( $mediaelement, $wpmediaelement );
}

/**
 * Registers the personal data exporter for media.
 *
 * @param array[] $exporters An array of personal data exporters, keyed by their ID.
 * @return array[] Updated array of personal data exporters.
 */
function wp_register_media_personal_data_exporter( $exporters ) {
	$exporters['wordpress-media'] = array(
		'exporter_friendly_name' => __( 'WordPress Media' ),
		'callback'               => 'wp_media_personal_data_exporter',
	);

Changelog

VersionDescription
6.5.0Added support for AVIF images.
5.8.0Added support for WebP images.
5.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Usage (if you only have image URL):

    <?php
    $src          = 'https://example.com/path/to/image.jpg';
    $upload_dir   = wp_upload_dir();
    $image_path   = str_replace( $upload_dir[ 'baseurl' ], $upload_dir[ 'basedir' ], $src );
    $getimagesize = wp_getimagesize( $image_path );
    ?>
    
    <img <?php echo $getimagesize[3]; ?> src="<?php echo esc_url( $src ); ?>" />

    Returns array of data:

    $getimagesize[0]          = The width of the image.
    $getimagesize[1]          = The height of the image.
    $getimagesize[2]          = One of the IMAGETYPE_ constants indicating the type of the image.
    $getimagesize[3]          = A text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
    $getimagesize['mime']     = The correspondent MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header.
    $getimagesize['channels'] = 3 for RGB pictures and 4 for CMYK pictures.
    $getimagesize['bits']     = The number of bits for each color.
    
    Array
    (
        [0] => 440
        [1] => 95
        [2] => 3
        [3] => width="440" height="95"
        [bits] => 8
        [mime] => image/png
    )

You must log in before being able to contribute a note or feedback.