wp_crop_image()
云策文档标注
概述
wp_crop_image() 是 WordPress 核心函数,用于将图像裁剪到指定尺寸。它支持通过附件 ID 或文件路径作为源,处理裁剪参数并生成新文件,返回新文件路径或 WP_Error 错误对象。
关键要点
- 函数接受多个参数:源文件或附件 ID、裁剪起始坐标和尺寸、目标尺寸,以及可选参数如绝对坐标和输出文件路径。
- 内部处理包括:验证源文件存在性、使用 wp_get_image_editor() 获取图像编辑器、执行裁剪操作、生成唯一文件名并保存文件。
- 返回值为字符串(新文件路径)或 WP_Error 对象,便于错误处理。
- 与多个 WordPress 函数集成,如 get_attached_file()、wp_mkdir_p() 和 wp_unique_filename(),确保文件操作的可靠性。
代码示例
$attachment_id = 100; // 图像附件 ID。
$cropped_file = wp_crop_image(
$attachment_id, // 附件 ID。
1, // 图像上的 x 位置。
70, // 图像上的 y 位置。
100, // 裁剪宽度。
100, // 裁剪高度。
100, // 目标裁剪图像宽度。
100 // 目标裁剪图像高度。
);
// 这将返回 100x100 裁剪图像。
if ( ! is_wp_error( $cropped_file ) ) {
echo $cropped_file; // 将返回裁剪文件的绝对路径。
}
原文内容
Crops an image to a given size.
Parameters
$srcstring|intrequired-
The source file or Attachment ID.
$src_xintrequired-
The start x position to crop from.
$src_yintrequired-
The start y position to crop from.
$src_wintrequired-
The width to crop.
$src_hintrequired-
The height to crop.
$dst_wintrequired-
The destination width.
$dst_hintrequired-
The destination height.
$src_absbool|falseoptional-
If the source crop points are absolute.
Default:
false $dst_filestring|falseoptional-
The destination file to write to.
Default:
false
Source
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;
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Skip to note 2 content
Bunty
$attachment_id = 100; // Image attachment ID. $cropped_file = wp_crop_image( $attachment_id, // Attachment ID. 1, // Position x on the image. 70, // Position y on the image. 100, // Selected for cropping width. 100, // Selected for cropping height. 100, // Desired Cropped image width. 100 // Desired cropped image height. ); // This will return 100x100 cropped image. if ( ! is_wp_error( $cropped_file ) ) { echo $cropped_file; // Will return absolute path of cropped file. }