GC_Image_Editor_GD::load()

最后更新于:2021-11-27 23:38:26

GC_Image_Editor_GD::load()

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

响应

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

源文件

文件: gc-includes/class-gc-image-editor-gd.php

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

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

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

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

		if ( ! $file_contents ) {
			return new GC_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file );
		}

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

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

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

		if ( ! $size ) {
			return new GC_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();
	}