WP_Image_Editor_GD::_save()protectedWP 3.5.0

Метод класса: WP_Image_Editor_GD{}

Возвращает

Массив|WP_Error. Array on success or WP_Error if the file failed to save.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->_save( $image, $filename, $mime_type );
$image(resource|GdImage) (обязательный)
-
$filename(строка|null)
-
По умолчанию: null
$mime_type(строка|null)
-
По умолчанию: null

Список изменений

С версии 3.5.0 Введена.
С версии 6.0.0 The $filesize value was added to the returned array.

Код WP_Image_Editor_GD::_save() WP 6.5.2

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 ),
	);
}