image_resize

函数


image_resize ( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 )
参数
  • (string)
    $file
    Image file path.
    Required:
  • (int)
    $max_w
    Maximum width to resize to.
    Required:
  • (int)
    $max_h
    Maximum height to resize to.
    Required:
  • (bool)
    $crop
    Optional. Whether to crop image or resize. Default false.
    Required:
    Default: false
  • (string)
    $suffix
    Optional. File suffix. Default null.
    Required:
    Default: null
  • (string)
    $dest_path
    Optional. New image file path. Default null.
    Required:
    Default: null
  • (int)
    $jpeg_quality
    Optional. Image quality percentage. Default 90.
    Required:
    Default: 90
返回值
  • (mixed) WP_Error on failure. String with new destination path.
相关
  • wp_get_image_editor()
定义位置
  • wp-includes/deprecated.php
    , line 3238
引入
2.5.0
弃用
3.5.0

缩小图像以适应一个特定的尺寸,并保存一个新的图像副本。

使用该函数,PNG的透明度将被保留,图像类型也是如此。如果进入的文件是PNG,那么调整后的图像也将是PNG。唯一支持的图像类型是PNG、GIF和JPEG。

有些功能需要API的存在,所以有些PHP版本可能会失去支持。这不是WordPress的错(功能被降级,而不是实际的缺陷),而是你的PHP版本的问题。

function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
	_deprecated_function( __FUNCTION__, '3.5.0', 'wp_get_image_editor()' );

	$editor = wp_get_image_editor( $file );
	if ( is_wp_error( $editor ) )
		return $editor;
	$editor->set_quality( $jpeg_quality );

	$resized = $editor->resize( $max_w, $max_h, $crop );
	if ( is_wp_error( $resized ) )
		return $resized;

	$dest_file = $editor->generate_filename( $suffix, $dest_path );
	$saved = $editor->save( $dest_file );

	if ( is_wp_error( $saved ) )
		return $saved;

	return $dest_file;
}