将图像裁剪为给定大小。
原型
wp_crop_image( string|int $src, int $src_x, int $src_y, int $src_w, int $src_h, int $dst_w, int $dst_h, int $src_abs = false, string $dst_file = false )
参数
$src
(string|int)
(Required)
源文件或附件ID。
$src_x
(int)
(Required)
从中开始的x位置。
$src_y
(int)
(Required)
从开始y位置进行裁剪。
$src_w
(int)
(Required)
要裁剪的宽度。
$src_h
(int)
(Required)
裁剪的高度。
$dst_w
(int)
(Required)
目的地宽度。
$dst_h
(int)
(Required)
目的地高度。
$src_abs
(int)
(Optional)
如果源裁剪点是绝对的。
$dst_file
(string)
(Optional)
要写入的目标文件。
返回值
(string|WP_Error)
成功的新文件路径,
源文件
路径:wp-admin/includes/image.php
<?php
...
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( basename( $src_file ), 'cropped-' . 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 ), basename( $dst_file ) );
$result = $editor->save( $dst_file );
if ( is_wp_error( $result ) )
return $result;
return $dst_file;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_crop_image/