wp/wp-includes/class-wp-image-editor.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   109 	 * @param bool|array $crop  {
   109 	 * @param bool|array $crop  {
   110 	 *     Optional. Image cropping behavior. If false, the image will be scaled (default).
   110 	 *     Optional. Image cropping behavior. If false, the image will be scaled (default).
   111 	 *     If true, image will be cropped to the specified dimensions using center positions.
   111 	 *     If true, image will be cropped to the specified dimensions using center positions.
   112 	 *     If an array, the image will be cropped using the array to specify the crop location:
   112 	 *     If an array, the image will be cropped using the array to specify the crop location:
   113 	 *
   113 	 *
   114 	 *     @type string $0 The x crop position. Accepts 'left' 'center', or 'right'.
   114 	 *     @type string $0 The x crop position. Accepts 'left', 'center', or 'right'.
   115 	 *     @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
   115 	 *     @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
   116 	 * }
   116 	 * }
   117 	 * @return true|WP_Error
   117 	 * @return true|WP_Error
   118 	 */
   118 	 */
   119 	abstract public function resize( $max_w, $max_h, $crop = false );
   119 	abstract public function resize( $max_w, $max_h, $crop = false );
   238 
   238 
   239 	/**
   239 	/**
   240 	 * Sets Image Compression quality on a 1-100% scale.
   240 	 * Sets Image Compression quality on a 1-100% scale.
   241 	 *
   241 	 *
   242 	 * @since 3.5.0
   242 	 * @since 3.5.0
   243 	 *
   243 	 * @since 6.8.0 The `$dims` parameter was added.
   244 	 * @param int $quality Compression Quality. Range: [1,100]
   244 	 *
       
   245 	 * @param int   $quality Compression Quality. Range: [1,100]
       
   246 	 * @param array $dims    Optional. Image dimensions array with 'width' and 'height' keys.
   245 	 * @return true|WP_Error True if set successfully; WP_Error on failure.
   247 	 * @return true|WP_Error True if set successfully; WP_Error on failure.
   246 	 */
   248 
   247 	public function set_quality( $quality = null ) {
   249 	 */
       
   250 	public function set_quality( $quality = null, $dims = array() ) {
   248 		// Use the output mime type if present. If not, fall back to the input/initial mime type.
   251 		// Use the output mime type if present. If not, fall back to the input/initial mime type.
   249 		$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
   252 		$mime_type = ! empty( $this->output_mime_type ) ? $this->output_mime_type : $this->mime_type;
   250 		// Get the default quality setting for the mime type.
   253 		// Get the default quality setting for the mime type.
   251 		$default_quality = $this->get_default_quality( $mime_type );
   254 		$default_quality = $this->get_default_quality( $mime_type );
   252 
   255 
   258 			 * manually without the `$quality` argument.
   261 			 * manually without the `$quality` argument.
   259 			 *
   262 			 *
   260 			 * The WP_Image_Editor::set_quality() method has priority over the filter.
   263 			 * The WP_Image_Editor::set_quality() method has priority over the filter.
   261 			 *
   264 			 *
   262 			 * @since 3.5.0
   265 			 * @since 3.5.0
       
   266 			 * @since 6.8.0 Added the size parameter.
   263 			 *
   267 			 *
   264 			 * @param int    $quality   Quality level between 1 (low) and 100 (high).
   268 			 * @param int    $quality   Quality level between 1 (low) and 100 (high).
   265 			 * @param string $mime_type Image mime type.
   269 			 * @param string $mime_type Image mime type.
       
   270 			 * @param array $size {
       
   271 			 *     Dimensions of the image.
       
   272 			 *
       
   273 			 *     @type int $width  The image width.
       
   274 			 *     @type int $height The image height.
       
   275 			 * }
   266 			 */
   276 			 */
   267 			$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type );
   277 			$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );
   268 
   278 
   269 			if ( 'image/jpeg' === $mime_type ) {
   279 			if ( 'image/jpeg' === $mime_type ) {
   270 				/**
   280 				/**
   271 				 * Filters the JPEG compression quality for backward-compatibility.
   281 				 * Filters the JPEG compression quality for backward-compatibility.
   272 				 *
   282 				 *
   316 		switch ( $mime_type ) {
   326 		switch ( $mime_type ) {
   317 			case 'image/webp':
   327 			case 'image/webp':
   318 				$quality = 86;
   328 				$quality = 86;
   319 				break;
   329 				break;
   320 			case 'image/jpeg':
   330 			case 'image/jpeg':
   321 			case 'image/avif':
       
   322 			default:
   331 			default:
   323 				$quality = $this->default_quality;
   332 				$quality = $this->default_quality;
   324 		}
   333 		}
   325 
   334 
   326 		return $quality;
   335 		return $quality;
   364 		if ( ! $mime_type || ( $file_mime === $mime_type ) ) {
   373 		if ( ! $mime_type || ( $file_mime === $mime_type ) ) {
   365 			$mime_type = $file_mime;
   374 			$mime_type = $file_mime;
   366 			$new_ext   = $file_ext;
   375 			$new_ext   = $file_ext;
   367 		}
   376 		}
   368 
   377 
   369 		/**
   378 		$output_format = wp_get_image_editor_output_format( $filename, $mime_type );
   370 		 * Filters the image editor output format mapping.
       
   371 		 *
       
   372 		 * Enables filtering the mime type used to save images. By default,
       
   373 		 * the mapping array is empty, so the mime type matches the source image.
       
   374 		 *
       
   375 		 * @see WP_Image_Editor::get_output_format()
       
   376 		 *
       
   377 		 * @since 5.8.0
       
   378 		 *
       
   379 		 * @param string[] $output_format {
       
   380 		 *     An array of mime type mappings. Maps a source mime type to a new
       
   381 		 *     destination mime type. Default empty array.
       
   382 		 *
       
   383 		 *     @type string ...$0 The new mime type.
       
   384 		 * }
       
   385 		 * @param string $filename  Path to the image.
       
   386 		 * @param string $mime_type The source image mime type.
       
   387 		 */
       
   388 		$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );
       
   389 
   379 
   390 		if ( isset( $output_format[ $mime_type ] )
   380 		if ( isset( $output_format[ $mime_type ] )
   391 			&& $this->supports_mime_type( $output_format[ $mime_type ] )
   381 			&& $this->supports_mime_type( $output_format[ $mime_type ] )
   392 		) {
   382 		) {
   393 			$mime_type = $output_format[ $mime_type ];
   383 			$mime_type = $output_format[ $mime_type ];
   441 
   431 
   442 	/**
   432 	/**
   443 	 * Builds an output filename based on current file, and adding proper suffix
   433 	 * Builds an output filename based on current file, and adding proper suffix
   444 	 *
   434 	 *
   445 	 * @since 3.5.0
   435 	 * @since 3.5.0
       
   436 	 * @since 6.8.0 Passing an empty string as $suffix will now omit the suffix from the generated filename.
   446 	 *
   437 	 *
   447 	 * @param string $suffix
   438 	 * @param string $suffix
   448 	 * @param string $dest_path
   439 	 * @param string $dest_path
   449 	 * @param string $extension
   440 	 * @param string $extension
   450 	 * @return string filename
   441 	 * @return string filename
   451 	 */
   442 	 */
   452 	public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
   443 	public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
   453 		// $suffix will be appended to the destination filename, just before the extension.
   444 		// If not empty the $suffix will be appended to the destination filename, just before the extension.
   454 		if ( ! $suffix ) {
   445 		if ( $suffix ) {
   455 			$suffix = $this->get_suffix();
   446 			$suffix = '-' . $suffix;
       
   447 		} elseif ( '' !== $suffix ) {
       
   448 			$suffix = '-' . $this->get_suffix();
   456 		}
   449 		}
   457 
   450 
   458 		$dir = pathinfo( $this->file, PATHINFO_DIRNAME );
   451 		$dir = pathinfo( $this->file, PATHINFO_DIRNAME );
   459 		$ext = pathinfo( $this->file, PATHINFO_EXTENSION );
   452 		$ext = pathinfo( $this->file, PATHINFO_EXTENSION );
   460 
   453 
   470 			} else {
   463 			} else {
   471 				$dir = $dest_path;
   464 				$dir = $dest_path;
   472 			}
   465 			}
   473 		}
   466 		}
   474 
   467 
   475 		return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
   468 		return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
   476 	}
   469 	}
   477 
   470 
   478 	/**
   471 	/**
   479 	 * Builds and returns proper suffix for file based on height and width.
   472 	 * Builds and returns proper suffix for file based on height and width.
   480 	 *
   473 	 *