wp/wp-includes/class-wp-image-editor-gd.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    68 			case 'image/png':
    68 			case 'image/png':
    69 				return ( $image_types & IMG_PNG ) != 0;
    69 				return ( $image_types & IMG_PNG ) != 0;
    70 			case 'image/gif':
    70 			case 'image/gif':
    71 				return ( $image_types & IMG_GIF ) != 0;
    71 				return ( $image_types & IMG_GIF ) != 0;
    72 			case 'image/webp':
    72 			case 'image/webp':
    73 				return ( $image_types & IMG_WEBP ) != 0; // phpcs:ignore PHPCompatibility.Constants.NewConstants.img_webpFound
    73 				return ( $image_types & IMG_WEBP ) != 0;
    74 		}
    74 		}
    75 
    75 
    76 		return false;
    76 		return false;
    77 	}
    77 	}
    78 
    78 
    87 		if ( $this->image ) {
    87 		if ( $this->image ) {
    88 			return true;
    88 			return true;
    89 		}
    89 		}
    90 
    90 
    91 		if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
    91 		if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
    92 			return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file );
    92 			return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
    93 		}
    93 		}
    94 
    94 
    95 		// Set artificially high because GD uses uncompressed images in memory.
    95 		// Set artificially high because GD uses uncompressed images in memory.
    96 		wp_raise_memory_limit( 'image' );
    96 		wp_raise_memory_limit( 'image' );
    97 
    97 
    98 		$file_contents = @file_get_contents( $this->file );
    98 		$file_contents = @file_get_contents( $this->file );
    99 
    99 
   100 		if ( ! $file_contents ) {
   100 		if ( ! $file_contents ) {
   101 			return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file );
   101 			return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );
   102 		}
   102 		}
   103 
   103 
   104 		// WebP may not work with imagecreatefromstring().
   104 		// WebP may not work with imagecreatefromstring().
   105 		if (
   105 		if (
   106 			function_exists( 'imagecreatefromwebp' ) &&
   106 			function_exists( 'imagecreatefromwebp' ) &&
   231 	 *
   231 	 *
   232 	 *     Either a height or width must be provided.
   232 	 *     Either a height or width must be provided.
   233 	 *     If one of the two is set to null, the resize will
   233 	 *     If one of the two is set to null, the resize will
   234 	 *     maintain aspect ratio according to the source image.
   234 	 *     maintain aspect ratio according to the source image.
   235 	 *
   235 	 *
   236 	 *     @type array $size {
   236 	 *     @type array ...$0 {
   237 	 *         Array of height, width values, and whether to crop.
   237 	 *         Array of height, width values, and whether to crop.
   238 	 *
   238 	 *
   239 	 *         @type int  $width  Image width. Optional if `$height` is specified.
   239 	 *         @type int  $width  Image width. Optional if `$height` is specified.
   240 	 *         @type int  $height Image height. Optional if `$width` is specified.
   240 	 *         @type int  $height Image height. Optional if `$width` is specified.
   241 	 *         @type bool $crop   Optional. Whether to crop the image. Default false.
   241 	 *         @type bool $crop   Optional. Whether to crop the image. Default false.
   421 
   421 
   422 	/**
   422 	/**
   423 	 * Saves current in-memory image to file.
   423 	 * Saves current in-memory image to file.
   424 	 *
   424 	 *
   425 	 * @since 3.5.0
   425 	 * @since 3.5.0
   426 	 *
   426 	 * @since 5.9.0 Renamed `$filename` to `$destfilename` to match parent class
   427 	 * @param string|null $filename
   427 	 *              for PHP 8 named parameter support.
   428 	 * @param string|null $mime_type
   428 	 *
       
   429 	 * @param string|null $destfilename Optional. Destination filename. Default null.
       
   430 	 * @param string|null $mime_type    Optional. The mime-type. Default null.
   429 	 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
   431 	 * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
   430 	 */
   432 	 */
   431 	public function save( $filename = null, $mime_type = null ) {
   433 	public function save( $destfilename = null, $mime_type = null ) {
   432 		$saved = $this->_save( $this->image, $filename, $mime_type );
   434 		$saved = $this->_save( $this->image, $destfilename, $mime_type );
   433 
   435 
   434 		if ( ! is_wp_error( $saved ) ) {
   436 		if ( ! is_wp_error( $saved ) ) {
   435 			$this->file      = $saved['path'];
   437 			$this->file      = $saved['path'];
   436 			$this->mime_type = $saved['mime-type'];
   438 			$this->mime_type = $saved['mime-type'];
   437 		}
   439 		}
   493 			 */
   495 			 */
   494 			'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
   496 			'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
   495 			'width'     => $this->size['width'],
   497 			'width'     => $this->size['width'],
   496 			'height'    => $this->size['height'],
   498 			'height'    => $this->size['height'],
   497 			'mime-type' => $mime_type,
   499 			'mime-type' => $mime_type,
       
   500 			'filesize'  => wp_filesize( $filename ),
   498 		);
   501 		);
   499 	}
   502 	}
   500 
   503 
   501 	/**
   504 	/**
   502 	 * Returns stream of current image.
   505 	 * Returns stream of current image.
   531 	/**
   534 	/**
   532 	 * Either calls editor's save function or handles file as a stream.
   535 	 * Either calls editor's save function or handles file as a stream.
   533 	 *
   536 	 *
   534 	 * @since 3.5.0
   537 	 * @since 3.5.0
   535 	 *
   538 	 *
   536 	 * @param string|stream $filename
   539 	 * @param string   $filename
   537 	 * @param callable      $function
   540 	 * @param callable $callback
   538 	 * @param array         $arguments
   541 	 * @param array    $arguments
   539 	 * @return bool
   542 	 * @return bool
   540 	 */
   543 	 */
   541 	protected function make_image( $filename, $function, $arguments ) {
   544 	protected function make_image( $filename, $callback, $arguments ) {
   542 		if ( wp_is_stream( $filename ) ) {
   545 		if ( wp_is_stream( $filename ) ) {
   543 			$arguments[1] = null;
   546 			$arguments[1] = null;
   544 		}
   547 		}
   545 
   548 
   546 		return parent::make_image( $filename, $function, $arguments );
   549 		return parent::make_image( $filename, $callback, $arguments );
   547 	}
   550 	}
   548 }
   551 }