wp_save_image

Saves image to post along with enqueued changes in $_REQUEST['history'].

Description

wp_save_image( (int) $post_id ); 

Parameters (1)

0. $post_id (int)
The post id.

Usage

  1. if ( !function_exists( 'wp_save_image' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image-edit.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_save_image($post_id); 
  9.  

Defined (1)

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

/wp-admin/includes/image-edit.php  
  1. function wp_save_image( $post_id ) { 
  2. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  3.  
  4. $return = new stdClass; 
  5. $success = $delete = $scaled = $nocrop = false; 
  6. $post = get_post( $post_id ); 
  7.  
  8. $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); 
  9. if ( is_wp_error( $img ) ) { 
  10. $return->error = esc_js( __('Unable to create new image.') ); 
  11. return $return; 
  12.  
  13. $fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0; 
  14. $fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0; 
  15. $target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : ''; 
  16. $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do']; 
  17.  
  18. if ( $scale && $fwidth > 0 && $fheight > 0 ) { 
  19. $size = $img->get_size(); 
  20. $sX = $size['width']; 
  21. $sY = $size['height']; 
  22.  
  23. // Check if it has roughly the same w / h ratio. 
  24. $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2); 
  25. if ( -0.1 < $diff && $diff < 0.1 ) { 
  26. // Scale the full size image. 
  27. if ( $img->resize( $fwidth, $fheight ) ) 
  28. $scaled = true; 
  29.  
  30. if ( !$scaled ) { 
  31. $return->error = esc_js( __('Error while saving the scaled image. Please reload the page and try again.') ); 
  32. return $return; 
  33. } elseif ( !empty($_REQUEST['history']) ) { 
  34. $changes = json_decode( wp_unslash($_REQUEST['history']) ); 
  35. if ( $changes ) 
  36. $img = image_edit_apply_changes($img, $changes); 
  37. } else { 
  38. $return->error = esc_js( __('Nothing to save, the image has not changed.') ); 
  39. return $return; 
  40.  
  41. $meta = wp_get_attachment_metadata($post_id); 
  42. $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); 
  43.  
  44. if ( !is_array($meta) ) { 
  45. $return->error = esc_js( __('Image data does not exist. Please re-upload the image.') ); 
  46. return $return; 
  47.  
  48. if ( !is_array($backup_sizes) ) 
  49. $backup_sizes = array(); 
  50.  
  51. // Generate new filename. 
  52. $path = get_attached_file( $post_id ); 
  53.  
  54. $basename = pathinfo( $path, PATHINFO_BASENAME ); 
  55. $dirname = pathinfo( $path, PATHINFO_DIRNAME ); 
  56. $ext = pathinfo( $path, PATHINFO_EXTENSION ); 
  57. $filename = pathinfo( $path, PATHINFO_FILENAME ); 
  58. $suffix = time() . rand(100, 999); 
  59.  
  60. if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && 
  61. isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $basename ) { 
  62.  
  63. if ( 'thumbnail' == $target ) { 
  64. $new_path = "{$dirname}/{$filename}-temp.{$ext}"; 
  65. } else { 
  66. $new_path = $path; 
  67. } else { 
  68. while ( true ) { 
  69. $filename = preg_replace( '/-e([0-9]+)$/', '', $filename ); 
  70. $filename .= "-e{$suffix}"; 
  71. $new_filename = "{$filename}.{$ext}"; 
  72. $new_path = "{$dirname}/$new_filename"; 
  73. if ( file_exists($new_path) ) { 
  74. $suffix++; 
  75. } else { 
  76. break; 
  77.  
  78. // Save the full-size file, also needed to create sub-sizes. 
  79. if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) { 
  80. $return->error = esc_js( __('Unable to save the image.') ); 
  81. return $return; 
  82.  
  83. if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) { 
  84. $tag = false; 
  85. if ( isset( $backup_sizes['full-orig'] ) ) { 
  86. if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] !== $basename ) { 
  87. $tag = "full-$suffix"; 
  88. } else { 
  89. $tag = 'full-orig'; 
  90.  
  91. if ( $tag ) { 
  92. $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $basename ); 
  93. $success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path ); 
  94.  
  95. $meta['file'] = _wp_relative_upload_path( $new_path ); 
  96.  
  97. $size = $img->get_size(); 
  98. $meta['width'] = $size['width']; 
  99. $meta['height'] = $size['height']; 
  100.  
  101. if ( $success && ('nothumb' == $target || 'all' == $target) ) { 
  102. if ( 'nothumb' == $target ) 
  103. $sizes = array_diff( $sizes, array('thumbnail') ); 
  104.  
  105. $return->fw = $meta['width']; 
  106. $return->fh = $meta['height']; 
  107. } elseif ( 'thumbnail' == $target ) { 
  108. $sizes = array( 'thumbnail' ); 
  109. $success = $delete = $nocrop = true; 
  110.  
  111. /** 
  112. * We need to remove any existing resized image files because 
  113. * a new crop or rotate could generate different sizes (and hence, filenames),  
  114. * keeping the new resized images from overwriting the existing image files. 
  115. * https://core.trac.wordpress.org/ticket/32171 
  116. */ 
  117. if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE && ! empty( $meta['sizes'] ) ) { 
  118. foreach ( $meta['sizes'] as $size ) { 
  119. if ( ! empty( $size['file'] ) && preg_match( '/-e[0-9]{13}-/', $size['file'] ) ) { 
  120. $delete_file = path_join( $dirname, $size['file'] ); 
  121. wp_delete_file( $delete_file ); 
  122.  
  123. if ( isset( $sizes ) ) { 
  124. $_sizes = array(); 
  125.  
  126. foreach ( $sizes as $size ) { 
  127. $tag = false; 
  128. if ( isset( $meta['sizes'][$size] ) ) { 
  129. if ( isset($backup_sizes["$size-orig"]) ) { 
  130. if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] ) 
  131. $tag = "$size-$suffix"; 
  132. } else { 
  133. $tag = "$size-orig"; 
  134.  
  135. if ( $tag ) 
  136. $backup_sizes[$tag] = $meta['sizes'][$size]; 
  137.  
  138. if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { 
  139. $width = intval( $_wp_additional_image_sizes[ $size ]['width'] ); 
  140. $height = intval( $_wp_additional_image_sizes[ $size ]['height'] ); 
  141. $crop = ( $nocrop ) ? false : $_wp_additional_image_sizes[ $size ]['crop']; 
  142. } else { 
  143. $height = get_option( "{$size}_size_h" ); 
  144. $width = get_option( "{$size}_size_w" ); 
  145. $crop = ( $nocrop ) ? false : get_option( "{$size}_crop" ); 
  146.  
  147. $_sizes[ $size ] = array( 'width' => $width, 'height' => $height, 'crop' => $crop ); 
  148.  
  149. $meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes ) ); 
  150.  
  151. unset( $img ); 
  152.  
  153. if ( $success ) { 
  154. wp_update_attachment_metadata( $post_id, $meta ); 
  155. update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes); 
  156.  
  157. if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) { 
  158. // Check if it's an image edit from attachment edit screen 
  159. if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) { 
  160. $thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true ); 
  161. $return->thumbnail = $thumb_url[0]; 
  162. } else { 
  163. $file_url = wp_get_attachment_url($post_id); 
  164. if ( ! empty( $meta['sizes']['thumbnail'] ) && $thumb = $meta['sizes']['thumbnail'] ) { 
  165. $return->thumbnail = path_join( dirname($file_url), $thumb['file'] ); 
  166. } else { 
  167. $return->thumbnail = "$file_url?w=128&h=128"; 
  168. } else { 
  169. $delete = true; 
  170.  
  171. if ( $delete ) { 
  172. wp_delete_file( $new_path ); 
  173.  
  174. $return->msg = esc_js( __('Image saved') ); 
  175. return $return;