WP_Image_Editor::get_output_format( string $filename = null, string $mime_type = null ): array
- Since
- 3.5.0
- Source
wp-includes/class-wp-image-editor.php:344
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
$filenamestringoptional- Default:
null $mime_typestringoptional- Default:
null
Return
array- { filename|null, extension, mime-type }
Hooks fired · 1
One hook fires while WP_Image_Editor::get_output_format() runs, in this order:
- apply_filters( image_editor_default_mime_type )filterline 393 (+49 into the body)
Filters default mime type prior to getting the file extension.
Uses · 8
- wp_get_image_editor_output_format()Determines the output format for the image editor.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- trailingslashit()Appends a trailing slash.
- wp_basename()i18n-friendly version of basename().
- WP_Image_Editor::get_extension()Returns first matched extension from Mime-type, as mapped from wp_get_mime_types()
- WP_Image_Editor::get_mime_type()Returns first matched mime-type from extension, as mapped from wp_get_mime_types()
- WP_Image_Editor::supports_mime_type()Checks to see if editor supports the mime-type specified.
- WP_Image_Editor::set_quality()Sets Image Compression quality on a 1-100% scale.
Source
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; } $output_format = wp_get_image_editor_output_format( $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 ); } /* * Ensure both $filename and $new_ext are not empty. * $this->get_extension() returns false on error which would effectively remove the extension * from $filename. That shouldn't happen, files without extensions are not supported. */ if ( $filename && $new_ext ) { $dir = pathinfo( $filename, PATHINFO_DIRNAME ); $ext = pathinfo( $filename, PATHINFO_EXTENSION ); $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; } if ( $mime_type && ( $mime_type !== $this->mime_type ) ) { // The image will be converted when saving. Set the quality for the new mime-type if not already set. if ( $mime_type !== $this->output_mime_type ) { $this->output_mime_type = $mime_type; } $this->set_quality(); } elseif ( ! empty( $this->output_mime_type ) ) { // Reset output_mime_type and quality. $this->output_mime_type = null; $this->set_quality(); } return array( $filename, $new_ext, $mime_type ); }History
Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-image-editor.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.