I'm using wp_get_image_editor() to resize and save an image that is being uploaded to the Media Library.

$image = wp_get_image_editor( $newattachment_url );
    if ( ! is_wp_error( $image ) ) {
        $image->rotate( 0 );
        $image->resize( 300, 300, false );

This is working fine however it is saving to the root of WordPress. How can I instead upload this file to the media library? I want to have the orignal image and the resized image in the media library.

$newattachment_url is the URL of the orignally uploaded image.

1 Answer 1


You need the generate_filename method of the image editor class. Like this:

$filename = $img->generate_filename( 'resized', ABSPATH.'wp-content/uploads/resized-images/');

This will save the image, renamed originalname-resized, in the resized-images directory of the upload folder. This does not mean WordPress knows it is there. That depends on the context in which you are calling wp_get_image_editor().

  • Thanks for this. I ended up using media_sideload_image() to copy the resized image in the WP root to the media library and then using PHP's unlink() to remove the resized image in the root.
    – Josh
    Commented Jun 10, 2016 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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