返回介绍

wp_crop_image()

发布于 2017-09-11 11:44:30 字数 3762 浏览 996 评论 0 收藏 0

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 )

Crop an Image to a given size.


description


参数

$src

(string|int) (Required) The 源代码 file or Attachment ID.

$src_x

(int) (Required) The start x position to crop from.

$src_y

(int) (Required) The start y position to crop from.

$src_w

(int) (Required) The width to crop.

$src_h

(int) (Required) The height to crop.

$dst_w

(int) (Required) The destination width.

$dst_h

(int) (Required) The destination height.

$src_abs

(int) (Optional) If the 源代码 crop points are absolute.

Default value: false

$dst_file

(string) (Optional) The destination file to write to.

Default value: false


返回值

(string|WP_Error) New filepath on success, WP_Error on failure.


源代码

File: wp-admin/includes/image.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;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-admin/includes/image.php: _load_image_to_edit_path()
  • wp-includes/functions.php: wp_mkdir_p()
  • wp-includes/functions.php: wp_unique_filename()
  • wp-includes/media.php: wp_get_image_editor()
  • wp-includes/post.php: get_attached_file()
  • wp-includes/load.php: is_wp_error()
  • Show 1 more use Hide more uses

Used By

  • wp-admin/includes/ajax-actions.php: wp_ajax_crop_image()
  • wp-admin/custom-header.php: Custom_Image_Header::ajax_header_crop()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2()
  • wp-admin/custom-header.php: Custom_Image_Header::step_3()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文