Skip to content

Instantly share code, notes, and snippets.

@rickalday
Last active August 29, 2015 14:07
Show Gist options
  • Save rickalday/3accbe8f788b8273fd8b to your computer and use it in GitHub Desktop.
Save rickalday/3accbe8f788b8273fd8b to your computer and use it in GitHub Desktop.
WordPress image resize functon
/**
* Resize an image to the specified dimensions
* http://codex.wordpress.org/Class_Reference/WP_Image_Editor
*
* Returns the new image file path
*
* @param image_url
* @param width
* @param width
* @return resized image file path
function image_resize( $image_url=null, $width=null, $height=null ){
$img = wp_get_image_editor( $image_url );
if ( ! is_wp_error( $img ) ) {
// resize if height and width supplied
if ( $width || $height ) {
if ( $width >= $height ) {
$max = $width;
} else {
$max = $height;
}
$img->resize( $max, $max, false );
$img->set_quality( 100 );
}
$img->stream();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment