image_edit_apply_changes()
云策文档标注
概述
image_edit_apply_changes() 函数用于对指定的 WP_Image_Editor 实例应用一组图像编辑操作,如旋转、翻转和裁剪。它处理操作数组,优化组合,并通过过滤器支持扩展。
关键要点
- 接受两个参数:$image(WP_Image_Editor 实例)和 $changes(操作数组),返回应用更改后的 WP_Image_Editor 实例。
- 支持的操作类型包括 'rotate'、'flip' 和 'crop',通过对象属性(如 r、f、c)映射。
- 自动组合连续相同类型的操作(如旋转角度相加、翻转轴异或),以提高效率。
- 提供过滤器 wp_image_editor_before_change 和已弃用的 image_edit_before_change,允许在应用更改前修改图像资源或实例。
- 兼容旧版 GD 图像资源,但自 WordPress 3.5.0 起推荐使用 WP_Image_Editor。
代码示例
function image_edit_apply_changes( $image, $changes ) {
// 函数实现代码,包括参数检查、操作扩展、组合和应用。
}注意事项
- 自 WordPress 3.5.0 起,$image 参数应为 WP_Image_Editor 实例,使用 GD 图像资源会触发弃用警告。
- 操作数组需为对象数组,支持 'rotate'、'flip'、'crop' 类型,具体属性如 angle、axis、sel 需正确设置。
- 过滤器 wp_image_editor_before_change 用于 WP_Image_Editor 实例,而 image_edit_before_change 已弃用,适用于 GD 资源。
原文内容
Performs group of changes on Editor specified.
Parameters
$imageWP_Image_Editorrequired-
WP_Image_Editor instance.
$changesarrayrequired-
Array of change operations.
Source
function image_edit_apply_changes( $image, $changes ) {
if ( is_gd_image( $image ) ) {
/* translators: 1: $image, 2: WP_Image_Editor */
_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
}
if ( ! is_array( $changes ) ) {
return $image;
}
// Expand change operations.
foreach ( $changes as $key => $obj ) {
if ( isset( $obj->r ) ) {
$obj->type = 'rotate';
$obj->angle = $obj->r;
unset( $obj->r );
} elseif ( isset( $obj->f ) ) {
$obj->type = 'flip';
$obj->axis = $obj->f;
unset( $obj->f );
} elseif ( isset( $obj->c ) ) {
$obj->type = 'crop';
$obj->sel = $obj->c;
unset( $obj->c );
}
$changes[ $key ] = $obj;
}
// Combine operations.
if ( count( $changes ) > 1 ) {
$filtered = array( $changes[0] );
for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) {
$combined = false;
if ( $filtered[ $i ]->type === $changes[ $j ]->type ) {
switch ( $filtered[ $i ]->type ) {
case 'rotate':
$filtered[ $i ]->angle += $changes[ $j ]->angle;
$combined = true;
break;
case 'flip':
$filtered[ $i ]->axis ^= $changes[ $j ]->axis;
$combined = true;
break;
}
}
if ( ! $combined ) {
$filtered[ ++$i ] = $changes[ $j ];
}
}
$changes = $filtered;
unset( $filtered );
}
// Image resource before applying the changes.
if ( $image instanceof WP_Image_Editor ) {
/**
* Filters the WP_Image_Editor instance before applying changes to the image.
*
* @since 3.5.0
*
* @param WP_Image_Editor $image WP_Image_Editor instance.
* @param array $changes Array of change operations.
*/
$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
} elseif ( is_gd_image( $image ) ) {
/**
* Filters the GD image resource before applying changes to the image.
*
* @since 2.9.0
* @deprecated 3.5.0 Use 'wp_image_editor_before_change' instead.
*
* @param resource|GdImage $image GD image resource or GdImage instance.
* @param array $changes Array of change operations.
*/
$image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
}
foreach ( $changes as $operation ) {
switch ( $operation->type ) {
case 'rotate':
if ( 0 !== $operation->angle ) {
if ( $image instanceof WP_Image_Editor ) {
$image->rotate( $operation->angle );
} else {
$image = _rotate_image_resource( $image, $operation->angle );
}
}
break;
case 'flip':
if ( 0 !== $operation->axis ) {
if ( $image instanceof WP_Image_Editor ) {
$image->flip( ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
} else {
$image = _flip_image_resource( $image, ( $operation->axis & 1 ) !== 0, ( $operation->axis & 2 ) !== 0 );
}
}
break;
case 'crop':
$sel = $operation->sel;
if ( $image instanceof WP_Image_Editor ) {
$size = $image->get_size();
$w = $size['width'];
$h = $size['height'];
$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
$image->crop( (int) ( $sel->x * $scale ), (int) ( $sel->y * $scale ), (int) ( $sel->w * $scale ), (int) ( $sel->h * $scale ) );
} else {
$scale = isset( $sel->r ) ? $sel->r : 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
}
break;
}
}
return $image;
}
Hooks
- apply_filters_deprecated( ‘image_edit_before_change’, resource|GdImage $image, array $changes )
-
Filters the GD image resource before applying changes to the image.
- apply_filters( ‘wp_image_editor_before_change’, WP_Image_Editor $image, array $changes )
-
Filters the WP_Image_Editor instance before applying changes to the image.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |