resize only height using wp_get_image_editor

I want to resize an image keeping its original width unchanged. I only want to resize the height to 300px. I am trying following, but it changes the width automatically. How can I keep it unchanged?

$width = NULL;
$height = 300;

$image = wp_get_image_editor( $_FILES['u_img']['tmp_name'] );
$image -> resize( $width, $height, true );

1 Answer
1

You can get the original sizes of the image being uploaded:

list( $uploaded_width, $uploaded_height ) = getimagesize( $_FILES['u_img']['tmp_name'] );

Then you can use those values to size the image as you like:

$height = 300;
$image = wp_get_image_editor( $_FILES['u_img']['tmp_name'] );
$image->resize( $uploaded_width, $height, true );

Leave a Comment