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()
定義位置
相關方法
has_image_sizeimage_downsizeadd_image_sizeget_dirsizeimage_resize_dimensions
引入
2.5.0
棄用
3.5.0

image_resize: 此函式用於調整影象的大小。

縮小影象以適應一個特定的尺寸,並儲存一個新的影象副本。

使用該函式,PNG的透明度將被保留,影象型別也是如此。如果進入的檔案是PNG,那麼調整後的影象也將是PNG。唯一支援的影象型別是PNG、GIF和JPEG。

有些功能需要API的存在,所以有些PHP版本可能會失去支援: 這不是WordPress的錯(功能被降級,而不是實際的缺陷),而是你的PHP版本的問題。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >