Title: WP_Image_Editor::get_output_format
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#user-contributed-notes)

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

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#description)󠁿

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

Provides corrected filename only if filename is provided.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#parameters)󠁿

 `$filename`stringoptional

Default:`null`

`$mime_type`stringoptional

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#return)󠁿

 array `filename|null`, extension, mime-type

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#source)󠁿

    ```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;
    	}

    	$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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-image-editor.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-image-editor.php#L344)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-image-editor.php#L344-L422)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#hooks)󠁿

 [apply_filters( ‘image_editor_default_mime_type’, string $mime_type )](https://developer.wordpress.org/reference/hooks/image_editor_default_mime_type/)

Filters default mime type prior to getting the file extension.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_image_editor_output_format()](https://developer.wordpress.org/reference/functions/wp_get_image_editor_output_format/)`wp-includes/media.php` |

Determines the output format for the image editor.

  | 
| [WP_Image_Editor::get_extension()](https://developer.wordpress.org/reference/classes/wp_image_editor/get_extension/)`wp-includes/class-wp-image-editor.php` |

Returns first matched extension from Mime-type, as mapped from [wp_get_mime_types()](https://developer.wordpress.org/reference/functions/wp_get_mime_types/)

  | 
| [WP_Image_Editor::get_mime_type()](https://developer.wordpress.org/reference/classes/wp_image_editor/get_mime_type/)`wp-includes/class-wp-image-editor.php` |

Returns first matched mime-type from extension, as mapped from [wp_get_mime_types()](https://developer.wordpress.org/reference/functions/wp_get_mime_types/)

  | 
| [WP_Image_Editor::set_quality()](https://developer.wordpress.org/reference/classes/wp_image_editor/set_quality/)`wp-includes/class-wp-image-editor.php` |

Sets Image Compression quality on a 1-100% scale.

  | 
| [WP_Image_Editor::supports_mime_type()](https://developer.wordpress.org/reference/classes/wp_image_editor/supports_mime_type/)`wp-includes/class-wp-image-editor.php` |

Checks to see if editor supports the mime-type specified.

  | 
| [wp_basename()](https://developer.wordpress.org/reference/functions/wp_basename/)`wp-includes/formatting.php` |

i18n-friendly version of basename().

  | 
| [trailingslashit()](https://developer.wordpress.org/reference/functions/trailingslashit/)`wp-includes/formatting.php` |

Appends a trailing slash.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 3 more](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.5.0](https://developer.wordpress.org/reference/since/3.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/?output_format=md#comment-content-6572)
 2.   [Darshit Rajyaguru](https://profiles.wordpress.org/darshitrajyaguru97/)  [  3 years ago  ](https://developer.wordpress.org/reference/classes/wp_image_editor/get_output_format/#comment-6572)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_image_editor%2Fget_output_format%2F%23comment-6572)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_image_editor%2Fget_output_format%2F%23comment-6572)
 4. Get the output format of the edited image.
 5.     ```php
        $editor = wp_get_image_editor( 'path/to/your/image.jpg' );
        if ( ! is_wp_error( $editor ) ) {
            $output_format = $editor->get_output_format();
            echo $output_format; // e.g., "image/jpeg"
        }
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_image_editor%2Fget_output_format%2F%3Freplytocom%3D6572%23feedback-editor-6572)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_image_editor%2Fget_output_format%2F)
before being able to contribute a note or feedback.