WP_Image_Editor_GD::load() WordPress Method

The WP_Image_Editor_GD::load() method is used to load an image from a file or URL. This is a wrapper for the GD library's imagecreatefromstring() function. This method can be used to load an image from a file or URL. It accepts an absolute path to a file, or a URL. When loading from a URL, the image is downloaded to a temporary file before it is loaded. Returns an WP_Image_Editor_GD object on success. False on failure.

WP_Image_Editor_GD::load() #

Loads image from $this->file into new GD Resource.


Return

(true|WP_Error) True if loaded successfully; WP_Error on failure.


Top ↑

Source

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

	public function load() {
		if ( $this->image ) {
			return true;
		}

		if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
			return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
		}

		// Set artificially high because GD uses uncompressed images in memory.
		wp_raise_memory_limit( 'image' );

		$file_contents = @file_get_contents( $this->file );

		if ( ! $file_contents ) {
			return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
		}

		// WebP may not work with imagecreatefromstring().
		if (
			function_exists( 'imagecreatefromwebp' ) &&
			( 'image/webp' === wp_get_image_mime( $this->file ) )
		) {
			$this->image = @imagecreatefromwebp( $this->file );
		} else {
			$this->image = @imagecreatefromstring( $file_contents );
		}

		if ( ! is_gd_image( $this->image ) ) {
			return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
		}

		$size = wp_getimagesize( $this->file );

		if ( ! $size ) {
			return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );
		}

		if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
			imagealphablending( $this->image, false );
			imagesavealpha( $this->image, true );
		}

		$this->update_size( $size[0], $size[1] );
		$this->mime_type = $size['mime'];

		return $this->set_quality();
	}


Top ↑

Changelog

Changelog
VersionDescription
3.5.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.