image_edit_apply_changes

Performs group of changes on Editor specified.

Description

(WP_Image_Editor) image_edit_apply_changes( (WP_Image_Editor) $image, (array) $changes ); 

Returns (WP_Image_Editor)

WP_Image_Editor instance with changes applied.

Parameters (2)

0. $image (WP_Image_Editor)
WP_Image_Editor instance.
1. $changes (array)
Array of change operations.

Usage

  1. if ( !function_exists( 'image_edit_apply_changes' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image-edit.php'; 
  3.  
  4. // WP_Image_Editor instance. 
  5. $image = null; 
  6.  
  7. // Array of change operations. 
  8. $changes = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = image_edit_apply_changes($image, $changes); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/image-edit.php  
  1. function image_edit_apply_changes( $image, $changes ) { 
  2. if ( is_resource( $image ) ) 
  3. _deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) ); 
  4.  
  5. if ( !is_array($changes) ) 
  6. return $image; 
  7.  
  8. // Expand change operations. 
  9. foreach ( $changes as $key => $obj ) { 
  10. if ( isset($obj->r) ) { 
  11. $obj->type = 'rotate'; 
  12. $obj->angle = $obj->r; 
  13. unset($obj->r); 
  14. } elseif ( isset($obj->f) ) { 
  15. $obj->type = 'flip'; 
  16. $obj->axis = $obj->f; 
  17. unset($obj->f); 
  18. } elseif ( isset($obj->c) ) { 
  19. $obj->type = 'crop'; 
  20. $obj->sel = $obj->c; 
  21. unset($obj->c); 
  22. $changes[$key] = $obj; 
  23.  
  24. // Combine operations. 
  25. if ( count($changes) > 1 ) { 
  26. $filtered = array($changes[0]); 
  27. for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) { 
  28. $combined = false; 
  29. if ( $filtered[$i]->type == $changes[$j]->type ) { 
  30. switch ( $filtered[$i]->type ) { 
  31. case 'rotate': 
  32. $filtered[$i]->angle += $changes[$j]->angle; 
  33. $combined = true; 
  34. break; 
  35. case 'flip': 
  36. $filtered[$i]->axis ^= $changes[$j]->axis; 
  37. $combined = true; 
  38. break; 
  39. if ( !$combined ) 
  40. $filtered[++$i] = $changes[$j]; 
  41. $changes = $filtered; 
  42. unset($filtered); 
  43.  
  44. // Image resource before applying the changes. 
  45. if ( $image instanceof WP_Image_Editor ) { 
  46.  
  47. /** 
  48. * Filters the WP_Image_Editor instance before applying changes to the image. 
  49. * @since 3.5.0 
  50. * @param WP_Image_Editor $image WP_Image_Editor instance. 
  51. * @param array $changes Array of change operations. 
  52. */ 
  53. $image = apply_filters( 'wp_image_editor_before_change', $image, $changes ); 
  54. } elseif ( is_resource( $image ) ) { 
  55.  
  56. /** 
  57. * Filters the GD image resource before applying changes to the image. 
  58. * @since 2.9.0 
  59. * @deprecated 3.5.0 Use wp_image_editor_before_change instead. 
  60. * @param resource $image GD image resource. 
  61. * @param array $changes Array of change operations. 
  62. */ 
  63. $image = apply_filters( 'image_edit_before_change', $image, $changes ); 
  64.  
  65. foreach ( $changes as $operation ) { 
  66. switch ( $operation->type ) { 
  67. case 'rotate': 
  68. if ( $operation->angle != 0 ) { 
  69. if ( $image instanceof WP_Image_Editor
  70. $image->rotate( $operation->angle ); 
  71. else 
  72. $image = _rotate_image_resource( $image, $operation->angle ); 
  73. break; 
  74. case 'flip': 
  75. if ( $operation->axis != 0 ) 
  76. if ( $image instanceof WP_Image_Editor
  77. $image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 ); 
  78. else 
  79. $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); 
  80. break; 
  81. case 'crop': 
  82. $sel = $operation->sel; 
  83.  
  84. if ( $image instanceof WP_Image_Editor ) { 
  85. $size = $image->get_size(); 
  86. $w = $size['width']; 
  87. $h = $size['height']; 
  88.  
  89. $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling 
  90. $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 
  91. } else { 
  92. $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling 
  93. $image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); 
  94. break; 
  95.  
  96. return $image;