WP_Image_Editor::get_output_format( string $filename = null, string $mime_type = null )

Returns preferred mime-type and extension based on provided file’s extension and mime, or current file’s extension and mime.

Description

Will default to $this->default_mime_type if requested is not supported.

Provides corrected filename only if filename is provided.

Parameters

$filename

(string) (Optional)

Default value: null

$mime_type

(string) (Optional)

Default value: null

Return

(array) filename|null, extension, mime-type

Source

File: wp-includes/class-wp-image-editor.php

protected function get_output_format( $filename = null, $mime_type = null ) {
		$new_ext = null;

		// By default, assume specified type takes priority.
		if ( $mime_type ) {
			$new_ext = $this->get_extension( $mime_type );
		}

		if ( $filename ) {
			$file_ext  = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
			$file_mime = $this->get_mime_type( $file_ext );
		} else {
			// If no file specified, grab editor's current extension and mime-type.
			$file_ext  = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
			$file_mime = $this->mime_type;
		}

		// Check to see if specified mime-type is the same as type implied by
		// file extension. If so, prefer extension from file.
		if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
			$mime_type = $file_mime;
			$new_ext   = $file_ext;
		}

		/**
		 * Filters the image editor output format mapping.
		 *
		 * Enables filtering the mime type used to save images. By default,
		 * the mapping array is empty, so the mime type matches the source image.
		 *
		 * This should be considered experimental as there are a few edge cases that
		 * still need to be solved.
		 *
		 * When this filter is used in combination with the {@see 'wp_editor_set_quality'}
		 * filter, the image quality value used will be that of the original mime type,
		 * not the mapped one. This could result in unexpected image quality for generated
		 * images. See https://core.trac.wordpress.org/ticket/53667 for more details.
		 *
		 * When a mime type is mapped to another, and two images with the same name are
		 * uploaded (image.jpg and image.webp, for example), the generated images for one
		 * will potentially overwrite the other's.
		 * See https://core.trac.wordpress.org/ticket/53668 for more details.
		 *
		 * @see WP_Image_Editor::get_output_format()
		 *
		 * @since 5.8.0
		 *
		 * @param string[] $output_format {
		 *     An array of mime type mappings. Maps a source mime type to a new
		 *     destination mime type. Default empty array.
		 *
		 *     @type string ...$0 The new mime type.
		 * }
		 * @param string $filename  Path to the image.
		 * @param string $mime_type The source image mime type.
		 * }
		 */
		$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );

		if ( isset( $output_format[ $mime_type ] )
			&& $this->supports_mime_type( $output_format[ $mime_type ] )
		) {
			$mime_type = $output_format[ $mime_type ];
			$new_ext   = $this->get_extension( $mime_type );
		}

		// Double-check that the mime-type selected is supported by the editor.
		// If not, choose a default instead.
		if ( ! $this->supports_mime_type( $mime_type ) ) {
			/**
			 * Filters default mime type prior to getting the file extension.
			 *
			 * @see wp_get_mime_types()
			 *
			 * @since 3.5.0
			 *
			 * @param string $mime_type Mime type string.
			 */
			$mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
			$new_ext   = $this->get_extension( $mime_type );
		}

		if ( $filename ) {
			$dir = pathinfo( $filename, PATHINFO_DIRNAME );
			$ext = pathinfo( $filename, PATHINFO_EXTENSION );

			$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
		}

		return array( $filename, $new_ext, $mime_type );
	}

Changelog

Version Description
3.5.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format