post_mime_type ) { return $metadata; } if ( ( ! empty( $metadata['width'] ) && ( 0 !== $metadata['width'] ) ) && ( ! empty( $metadata['height'] ) && 0 !== $metadata['height'] ) ) { return $metadata; } $file = get_attached_file( $attachment_id ); if ( ! $file ) { return $metadata; } if ( empty( $metadata['width'] ) ) { $metadata['width'] = 0; } if ( empty( $metadata['height'] ) ) { $metadata['height'] = 0; } if ( empty( $metadata['file'] ) ) { $metadata['file'] = _wp_relative_upload_path( $file ); } if ( empty( $metadata['sizes'] ) ) { $metadata['sizes'] = []; } if ( class_exists( 'Imagick' ) ) { try { $im = new \Imagick( $file ); $image_geometry = $im->getImageGeometry(); $im->clear(); $metadata['width'] = $image_geometry['width']; $metadata['height'] = $image_geometry['height']; } catch ( Throwable $t ) { Logger::log( Logger::LEVEL_ERROR, 'AVIF image dimensions calculation error: ' . $t->getMessage() ); } } return $metadata; } public function __construct() { if ( $this->is_avif_supported() ) { return; } add_filter( 'upload_mimes', [ $this, 'add_to_supported_types' ] ); add_filter( 'mime_types', [ $this, 'add_to_supported_types' ] ); add_filter( 'getimagesize_mimes_to_exts', [ $this, 'add_to_supported_mime_types' ] ); add_filter( 'file_is_displayable_image', [ $this, 'mark_as_displayable' ], 10, 2 ); add_filter( 'wp_generate_attachment_metadata', [ $this, 'fix_avif_images' ], 1, 3 ); } }