函数文档

wp_stream_image()

💡 云策文档标注

概述

wp_stream_image() 函数用于将 WP_Image_Editor 实例中的图像流式传输到浏览器。它处理图像输出,支持多种 MIME 类型,并包含向后兼容性逻辑。

关键要点

  • 函数接受三个必需参数:$image(WP_Image_Editor 实例)、$mime_type(图像 MIME 类型)和 $attachment_id(附件 ID)。
  • 返回布尔值:成功时返回 true,失败时返回 false。
  • 如果 $image 是 WP_Image_Editor 实例,则使用 image_editor_save_pre 过滤器并调用 $image->stream() 方法。
  • 对于非 WP_Image_Editor 实例(已弃用),使用 image_save_pre 过滤器(已弃用)和 PHP GD 函数处理不同 MIME 类型。
  • 支持 MIME 类型包括 image/jpeg、image/png、image/gif、image/webp 和 image/avif,后两者需检查函数存在性。
  • 自 WordPress 3.5.0 起,推荐使用 WP_Image_Editor 实例,旧方式已标记为弃用。

代码示例

function wp_stream_image( $image, $mime_type, $attachment_id ) {
    if ( $image instanceof WP_Image_Editor ) {
        $image = apply_filters( 'image_editor_save_pre', $image, $attachment_id );
        if ( is_wp_error( $image->stream( $mime_type ) ) ) {
            return false;
        }
        return true;
    } else {
        _deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
        $image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
        switch ( $mime_type ) {
            case 'image/jpeg':
                header( 'Content-Type: image/jpeg' );
                return imagejpeg( $image, null, 90 );
            case 'image/png':
                header( 'Content-Type: image/png' );
                return imagepng( $image );
            case 'image/gif':
                header( 'Content-Type: image/gif' );
                return imagegif( $image );
            case 'image/webp':
                if ( function_exists( 'imagewebp' ) ) {
                    header( 'Content-Type: image/webp' );
                    return imagewebp( $image, null, 90 );
                }
                return false;
            case 'image/avif':
                if ( function_exists( 'imageavif' ) ) {
                    header( 'Content-Type: image/avif' );
                    return imageavif( $image, null, 90 );
                }
                return false;
            default:
                return false;
        }
    }
}

注意事项

  • 自 WordPress 3.5.0 起,$image 参数应使用 WP_Image_Editor 实例,旧方式已弃用并可能在未来版本中移除。
  • 使用 image_editor_save_pre 过滤器可修改 WP_Image_Editor 实例,而 image_save_pre 过滤器已弃用,建议迁移。
  • 对于 image/webp 和 image/avif 类型,需确保 PHP 环境支持相应函数(imagewebp 和 imageavif),否则返回 false。
  • 函数内部处理 HTTP 头设置(如 Content-Type),开发者无需额外设置。

📄 原文内容

Streams image in WP_Image_Editor to browser.

Parameters

$imageWP_Image_Editorrequired
The image editor instance.
$mime_typestringrequired
The mime type of the image.
$attachment_idintrequired
The image’s attachment post ID.

Return

bool True on success, false on failure.

Source

function wp_stream_image( $image, $mime_type, $attachment_id ) {
	if ( $image instanceof WP_Image_Editor ) {

		/**
		 * Filters the WP_Image_Editor instance for the image to be streamed to the browser.
		 *
		 * @since 3.5.0
		 *
		 * @param WP_Image_Editor $image         The image editor instance.
		 * @param int             $attachment_id The attachment post ID.
		 */
		$image = apply_filters( 'image_editor_save_pre', $image, $attachment_id );

		if ( is_wp_error( $image->stream( $mime_type ) ) ) {
			return false;
		}

		return true;
	} else {
		/* translators: 1: $image, 2: WP_Image_Editor */
		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );

		/**
		 * Filters the GD image resource to be streamed to the browser.
		 *
		 * @since 2.9.0
		 * @deprecated 3.5.0 Use 'image_editor_save_pre' instead.
		 *
		 * @param resource|GdImage $image         Image resource to be streamed.
		 * @param int              $attachment_id The attachment post ID.
		 */
		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );

		switch ( $mime_type ) {
			case 'image/jpeg':
				header( 'Content-Type: image/jpeg' );
				return imagejpeg( $image, null, 90 );
			case 'image/png':
				header( 'Content-Type: image/png' );
				return imagepng( $image );
			case 'image/gif':
				header( 'Content-Type: image/gif' );
				return imagegif( $image );
			case 'image/webp':
				if ( function_exists( 'imagewebp' ) ) {
					header( 'Content-Type: image/webp' );
					return imagewebp( $image, null, 90 );
				}
				return false;
			case 'image/avif':
				if ( function_exists( 'imageavif' ) ) {
					header( 'Content-Type: image/avif' );
					return imageavif( $image, null, 90 );
				}
				return false;
			default:
				return false;
		}
	}
}

Hooks

apply_filters( ‘image_editor_save_pre’, WP_Image_Editor $image, int $attachment_id )

Filters the WP_Image_Editor instance for the image to be streamed to the browser.

apply_filters_deprecated( ‘image_save_pre’, resource|GdImage $image, int $attachment_id )

Filters the GD image resource to be streamed to the browser.

Changelog

Version Description
2.9.0 Introduced.