wp_save_image_file( string $filename, WP_Image_Editor $image, string $mime_type, int $post_id )

Saves image to file.


Parameters

$filename

(Required) Name of the file to be saved.

$image

(Required) The image editor instance.

$mime_type

(Required) The mime type of the image.

$post_id

(Required) Attachment post ID.


Return

(array|WP_Error|bool) Array on success or WP_Error if the file failed to save.<br> When called with a deprecated value for the $image parameter, i.e. a non-WP_Image_Editor image resource or GdImage instance, the function will return true on success, false on failure.<br>

  • 'path'
    (string) Path to the image file.<br>
  • 'file'
    (string) Name of the image file.<br>
  • 'width'
    (int) Image width.<br>
  • 'height'
    (int) Image height.<br>
  • 'mime-type'
    (string) The mime type of the image.<br>
  • 'filesize'
    (int) File size of the image.<br>


Source

File: wp-admin/includes/image-edit.php

function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
	if ( $image instanceof WP_Image_Editor ) {

		/** This filter is documented in wp-admin/includes/image-edit.php */
		$image = apply_filters( 'image_editor_save_pre', $image, $post_id );

		/**
		 * Filters whether to skip saving the image file.
		 *
		 * Returning a non-null value will short-circuit the save method,
		 * returning that value instead.
		 *
		 * @since WP-3.5.0
		 *
		 * @param mixed           $override  Value to return instead of saving. Default null.
		 * @param string          $filename  Name of the file to be saved.
		 * @param WP_Image_Editor $image     WP_Image_Editor instance.
		 * @param string          $mime_type Image mime type.
		 * @param int             $post_id   Post ID.
		 */
		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );

		if ( null !== $saved )
			return $saved;

		return $image->save( $filename, $mime_type );
	} else {
		_deprecated_argument( __FUNCTION__, 'WP-3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );

		/** This filter is documented in wp-admin/includes/image-edit.php */
		$image = apply_filters( 'image_save_pre', $image, $post_id );

		/**
		 * Filters whether to skip saving the image file.
		 *
		 * Returning a non-null value will short-circuit the save method,
		 * returning that value instead.
		 *
		 * @since WP-2.9.0
		 * @deprecated WP-3.5.0 Use wp_save_image_editor_file instead.
		 *
		 * @param mixed           $override  Value to return instead of saving. Default null.
		 * @param string          $filename  Name of the file to be saved.
		 * @param WP_Image_Editor $image     WP_Image_Editor instance.
		 * @param string          $mime_type Image mime type.
		 * @param int             $post_id   Post ID.
		 */
		$saved = apply_filters( 'wp_save_image_file', null, $filename, $image, $mime_type, $post_id );

		if ( null !== $saved )
			return $saved;

		switch ( $mime_type ) {
			case 'image/jpeg':

				/** This filter is documented in wp-includes/class-wp-image-editor.php */
				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
			case 'image/png':
				return imagepng( $image, $filename );
			case 'image/gif':
				return imagegif( $image, $filename );
			default:
				return false;
		}
	}
}


Changelog

Changelog
Version Description
6.0.0 The $filesize value was added to the returned array.
3.5.0 The $image parameter expects a WP_Image_Editor instance.
2.9.0 Introduced.