wpseek.com
A WordPress-centric search engine for devs and theme authors



_wp_get_primary_and_additional_mime_types › WordPress Function

Since6.1.0
Deprecatedn/a
_wp_get_primary_and_additional_mime_types ( $file, $attachment_id )
Access:
  • private
Parameters: (2)
  • (string) $file Full path to the image file.
    Required: Yes
  • (int) $attachment_id Attachment ID to process.
    Required: Yes
Returns:
  • (array) An array with two entries, the primary mime type and the list of additional mime types.
Defined at:
Codex:

Extracts the primary and additional mime output types for an image from the $image_mime_transforms.



Source

function _wp_get_primary_and_additional_mime_types( $file, $attachment_id ) {
	$image_mime_transforms = wp_upload_image_mime_transforms( $attachment_id );
	$original_mime_type    = wp_get_image_mime( $file );
	$output_mime_types     = isset( $image_mime_transforms[ $original_mime_type ] ) ? $image_mime_transforms[ $original_mime_type ] : array( $original_mime_type );

	// Exclude any output mime types that the system doesn't support.
	$output_mime_types = array_values(
		array_filter(
			$output_mime_types,
			function( $mime_type ) {
				return wp_image_editor_supports(
					array(
						'mime_type' => $mime_type,
					)
				);
			}
		)
	);

	// Handle an empty value for $output_mime_types: only output the original type.
	if ( empty( $output_mime_types ) ) {
		return array( $original_mime_type, array() );
	}

	// Use original mime type as primary mime type, or alternatively the first one.
	$primary_mime_type_key = array_search( $original_mime_type, $output_mime_types, true );
	if ( false === $primary_mime_type_key ) {
		$primary_mime_type_key = 0;
	}
	// Split output mime types into primary mime type and additional mime types.
	$additional_mime_types     = $output_mime_types;
	list( $primary_mime_type ) = array_splice( $additional_mime_types, $primary_mime_type_key, 1 );

	return array(
		$primary_mime_type,
		$additional_mime_types,
	);
}