wp_crop_image
函数
wp_crop_image ( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false )
- 参数
-
-
(string|int)
$src
The source file or Attachment ID.- Required: 是
-
(int)
$src_x
The start x position to crop from.- Required: 是
-
(int)
$src_y
The start y position to crop from.- Required: 是
-
(int)
$src_w
The width to crop.- Required: 是
-
(int)
$src_h
The height to crop.- Required: 是
-
(int)
$dst_w
The destination width.- Required: 是
-
(int)
$dst_h
The destination height.- Required: 是
-
(bool|false)
$src_abs
Optional. If the source crop points are absolute.- Required: 否
- Default: false
-
(string|false)
$dst_file
Optional. The destination file to write to.- Required: 否
- Default: false
-
(string|int)
- 返回值
-
- (string|WP_Error) New filepath on success, WP_Error on failure.
- 定义位置
-
-
wp-admin/includes/image.php
, line 25
-
wp-admin/includes/image.php
- 引入
- 2.1.0
- 弃用
- –
Crops an image to a given size.
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
$src_file = $src;
if ( is_numeric( $src ) ) { // Handle int as attachment ID.
$src_file = get_attached_file( $src );
if ( ! file_exists( $src_file ) ) {
// If the file doesn't exist, attempt a URL fopen on the src link.
// This can occur with certain file replication plugins.
$src = _load_image_to_edit_path( $src, 'full' );
} else {
$src = $src_file;
}
}
$editor = wp_get_image_editor( $src );
if ( is_wp_error( $editor ) ) {
return $editor;
}
$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
if ( is_wp_error( $src ) ) {
return $src;
}
if ( ! $dst_file ) {
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file );
}
/*
* The directory containing the original file may no longer exist when
* using a replication plugin.
*/
wp_mkdir_p( dirname( $dst_file ) );
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
$result = $editor->save( $dst_file );
if ( is_wp_error( $result ) ) {
return $result;
}
if ( ! empty( $result['path'] ) ) {
return $result['path'];
}
return $dst_file;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。