image_resize

Scale down an image to fit a particular size and save a new copy of the image.

Description

image_resize( (string) $file, (int) $max_w, (int) $max_h, (constant) $crop = false, (constant) $suffix = null, (constant) $dest_path = null, (int) $jpeg_quality = 90 ); 

The PNG transparency will be preserved using the function, as well as the image type. If the file going in is PNG, then the resized image is going to be PNG. The only supported image types are PNG, GIF, and JPEG.

Some functionality requires API to exist, so some PHP version may lose out support. This is not the fault of WordPress (where functionality is downgraded, not actual defects), but of your PHP version.

Parameters (7)

0. $file (string)
Image file path.
1. $max_w (int)
Maximum width to resize to.
2. $max_h (int)
Maximum height to resize to.
3. $crop — Optional. (constant) => false
Whether to crop image or resize.
4. $suffix — Optional. (constant) => null
File suffix.
5. $dest_path — Optional. (constant) => null
New image file path.
6. $jpeg_quality — Optional. (int) => 90
Optional, default is 90. Image quality percentage.

Usage

  1. if ( !function_exists( 'image_resize' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.php'; 
  3.  
  4. // Image file path. 
  5. $file = ''; 
  6.  
  7. // Maximum width to resize to. 
  8. $max_w = -1; 
  9.  
  10. // Maximum height to resize to. 
  11. $max_h = -1; 
  12.  
  13. // Optional. Whether to crop image or resize. 
  14. $crop = false; 
  15.  
  16. // Optional. File suffix. 
  17. $suffix = null; 
  18.  
  19. // Optional. New image file path. 
  20. $dest_path = null; 
  21.  
  22. // Optional, default is 90. Image quality percentage. 
  23. $jpeg_quality = 90; 
  24.  
  25. // NOTICE! Understand what this does before running. 
  26. $result = image_resize($file, $max_w, $max_h, $crop, $suffix, $dest_path, $jpeg_quality); 
  27.  

Defined (1)

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

/wp-includes/deprecated.php  
  1. function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { 
  2. _deprecated_function( __FUNCTION__, '3.5.0', 'wp_get_image_editor()' ); 
  3.  
  4. $editor = wp_get_image_editor( $file ); 
  5. if ( is_wp_error( $editor ) ) 
  6. return $editor; 
  7. $editor->set_quality( $jpeg_quality ); 
  8.  
  9. $resized = $editor->resize( $max_w, $max_h, $crop ); 
  10. if ( is_wp_error( $resized ) ) 
  11. return $resized; 
  12.  
  13. $dest_file = $editor->generate_filename( $suffix, $dest_path ); 
  14. $saved = $editor->save( $dest_file ); 
  15.  
  16. if ( is_wp_error( $saved ) ) 
  17. return $saved; 
  18.  
  19. return $dest_file;