wp_save_image_file

Saves Image to File.

Description

wp_save_image_file( (string) $filename, (WP_Image_Editor) $image, (string) $mime_type, (int) $post_id ); 

Parameters (4)

0. $filename (string)
The filename.
1. $image (WP_Image_Editor)
The image.
2. $mime_type (string)
The mime type.
3. $post_id (int)
The post id.

Usage

  1. if ( !function_exists( 'wp_save_image_file' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image-edit.php'; 
  3.  
  4. // The filename. 
  5. $filename = ''; 
  6.  
  7. // The image. 
  8. $image = null; 
  9.  
  10. // The mime type. 
  11. $mime_type = ''; 
  12.  
  13. // The post id. 
  14. $post_id = -1; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wp_save_image_file($filename, $image, $mime_type, $post_id); 
  18.  

Defined (1)

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

/wp-admin/includes/image-edit.php  
  1. function wp_save_image_file( $filename, $image, $mime_type, $post_id ) { 
  2. if ( $image instanceof WP_Image_Editor ) { 
  3.  
  4. /** This filter is documented in wp-admin/includes/image-edit.php */ 
  5. $image = apply_filters( 'image_editor_save_pre', $image, $post_id ); 
  6.  
  7. /** 
  8. * Filters whether to skip saving the image file. 
  9. * Returning a non-null value will short-circuit the save method,  
  10. * returning that value instead. 
  11. * @since 3.5.0 
  12. * @param mixed $override Value to return instead of saving. Default null. 
  13. * @param string $filename Name of the file to be saved. 
  14. * @param WP_Image_Editor $image WP_Image_Editor instance. 
  15. * @param string $mime_type Image mime type. 
  16. * @param int $post_id Post ID. 
  17. */ 
  18. $saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id ); 
  19.  
  20. if ( null !== $saved ) 
  21. return $saved; 
  22.  
  23. return $image->save( $filename, $mime_type ); 
  24. } else { 
  25. _deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) ); 
  26.  
  27. /** This filter is documented in wp-admin/includes/image-edit.php */ 
  28. $image = apply_filters( 'image_save_pre', $image, $post_id ); 
  29.  
  30. /** 
  31. * Filters whether to skip saving the image file. 
  32. * Returning a non-null value will short-circuit the save method,  
  33. * returning that value instead. 
  34. * @since 2.9.0 
  35. * @deprecated 3.5.0 Use wp_save_image_editor_file instead. 
  36. * @param mixed $override Value to return instead of saving. Default null. 
  37. * @param string $filename Name of the file to be saved. 
  38. * @param WP_Image_Editor $image WP_Image_Editor instance. 
  39. * @param string $mime_type Image mime type. 
  40. * @param int $post_id Post ID. 
  41. */ 
  42. $saved = apply_filters( 'wp_save_image_file', null, $filename, $image, $mime_type, $post_id ); 
  43.  
  44. if ( null !== $saved ) 
  45. return $saved; 
  46.  
  47. switch ( $mime_type ) { 
  48. case 'image/jpeg': 
  49.  
  50. /** This filter is documented in wp-includes/class-wp-image-editor.php */ 
  51. return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 
  52. case 'image/png': 
  53. return imagepng( $image, $filename ); 
  54. case 'image/gif': 
  55. return imagegif( $image, $filename ); 
  56. default: 
  57. return false;