Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

WP_Image_Editor_Imagick::write_image() WordPress Method

The WP_Image_Editor_Imagick::write_image() is a method used to write images to a file. This method can be used to write images in various formats, including JPEG, PNG, and GIF.

WP_Image_Editor_Imagick::write_image( Imagick $image, string $filename ) #

Writes an image to a file or stream.


Parameters

$image

(Imagick)(Required)

$filename

(string)(Required)The destination filename or stream URL.


Top ↑

Return

(true|WP_Error)


Top ↑

Source

File: wp-includes/class-wp-image-editor-imagick.php

	private function write_image( $image, $filename ) {
		if ( wp_is_stream( $filename ) ) {
			/*
			 * Due to reports of issues with streams with `Imagick::writeImageFile()` and `Imagick::writeImage()`, copies the blob instead.
			 * Checks for exact type due to: https://www.php.net/manual/en/function.file-put-contents.php
			 */
			if ( file_put_contents( $filename, $image->getImageBlob() ) === false ) {
				return new WP_Error(
					'image_save_error',
					sprintf(
						/* translators: %s: PHP function name. */
						__( '%s failed while writing image to stream.' ),
						'<code>file_put_contents()</code>'
					),
					$filename
				);
			} else {
				return true;
			}
		} else {
			$dirname = dirname( $filename );

			if ( ! wp_mkdir_p( $dirname ) ) {
				return new WP_Error(
					'image_save_error',
					sprintf(
						/* translators: %s: Directory path. */
						__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
						esc_html( $dirname )
					)
				);
			}

			try {
				return $image->writeImage( $filename );
			} catch ( Exception $e ) {
				return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
			}
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
5.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.