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 );
  • You realize that if you do that it will skew the aspect ratio, or am I misunderstanding what you are trying to do? – jackreichert Jun 8 '15 at 19:37
  • ...unless you set the crop flag like you did in your example... – jackreichert Jun 8 '15 at 19:45

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.