Title: WP_Image_Editor_GD::_save
Published: April 25, 2014
Last modified: May 20, 2026

---

# WP_Image_Editor_GD::_save( resource|GdImage $image, string|null $filename = null, string|null $mime_type = null ): array|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#wp--skip-link--target)

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

 `$image`resource|GdImagerequired

`$filename`string|nulloptional

Default:`null`

`$mime_type`string|nulloptional

Default:`null`

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

 array|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Array
on success or [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
if the file failed to save.

 * `path` string
 * Path to the image file.
 * `file` string
 * Name of the image file.
 * `width` int
 * Image width.
 * `height` int
 * Image height.
 * `mime-type` string
 * The mime type of the image.
 * `filesize` int
 * File size of the image.

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

    ```php
    protected function _save( $image, $filename = null, $mime_type = null ) {
    	list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );

    	if ( ! $filename ) {
    		$filename = $this->generate_filename( null, null, $extension );
    	}

    	if ( function_exists( 'imageinterlace' ) ) {
    		/**
    		 * Filters whether to output progressive images (if available).
    		 *
    		 * @since 6.5.0
    		 *
    		 * @param bool   $interlace Whether to use progressive images for output if available. Default false.
    		 * @param string $mime_type The mime type being saved.
    		 */
    		imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) );
    	}

    	if ( 'image/gif' === $mime_type ) {
    		if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
    			return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    		}
    	} elseif ( 'image/png' === $mime_type ) {
    		// Convert from full colors to index colors, like original PNG.
    		if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
    			imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
    		}

    		if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) {
    			return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    		}
    	} elseif ( 'image/jpeg' === $mime_type ) {
    		if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
    			return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    		}
    	} elseif ( 'image/webp' === $mime_type ) {
    		if ( ! function_exists( 'imagewebp' )
    			|| ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) )
    		) {
    			return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    		}
    	} elseif ( 'image/avif' === $mime_type ) {
    		if ( ! function_exists( 'imageavif' )
    			|| ! $this->make_image( $filename, 'imageavif', array( $image, $filename, $this->get_quality() ) )
    		) {
    			return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    		}
    	} else {
    		return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    	}

    	// Set correct file permissions.
    	$stat  = stat( dirname( $filename ) );
    	$perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
    	chmod( $filename, $perms );

    	return array(
    		'path'      => $filename,
    		/**
    		 * Filters the name of the saved image file.
    		 *
    		 * @since 2.6.0
    		 *
    		 * @param string $filename Name of the file.
    		 */
    		'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
    		'width'     => $this->size['width'],
    		'height'    => $this->size['height'],
    		'mime-type' => $mime_type,
    		'filesize'  => wp_filesize( $filename ),
    	);
    }
    ```

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

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

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

Filters the name of the saved image file.

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

Filters whether to output progressive images (if available).

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

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

Wrapper for PHP filesize with filters and casting the result as an integer.

  | 
| [WP_Image_Editor_GD::make_image()](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/make_image/)`wp-includes/class-wp-image-editor-gd.php` |

Either calls editor’s save function or handles file as a stream.

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

Retrieves the translation of $text.

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

i18n-friendly version of basename().

  | 
| [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.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 4 more](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/_save/?output_format=md#)

| Used by | Description | 
| [WP_Image_Editor_GD::make_subsize()](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/make_subsize/)`wp-includes/class-wp-image-editor-gd.php` |

Create an image sub-size and return the image meta data value for it.

  | 
| [WP_Image_Editor_GD::save()](https://developer.wordpress.org/reference/classes/wp_image_editor_gd/save/)`wp-includes/class-wp-image-editor-gd.php` |

Saves current in-memory image to file.

  |

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

| Version | Description | 
| [6.0.0](https://developer.wordpress.org/reference/since/6.0.0/) | The `$filesize` value was added to the returned array. | 
| [3.5.0](https://developer.wordpress.org/reference/since/3.5.0/) | Introduced. |

## User Contributed Notes

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