WordPress wp_get_image_editor returns “invalid_image” error

I’ve passed a direct URL to an image on my site into the wp_get_image_editor() function, and the function always returns the “invalid_image” error.

The URL is certainly correct and the image is certainly real; Chrome lets me click the URL in the debug output and it opens a new tab showing the image. The image is a 620×413 JPEG, so it’s not exactly an edge case (I also tested a 700×490 png just to be sure). I also tried using the URL of an unrelated publicly-accessible image that I found on Google image search, and the same thing happened.

Read More

To make things extra confusing, this is happening inside of a plugin that worked perfectly on the Ubuntu install I developed it on, and started throwing a fit only after I deployed it to a production server. This makes me wonder if it might be some sort of esoteric configuration issue, but I don’t see how it could be. I’m using WordPress 4.2.2.

Related posts

2 comments

  1. Finally solved this by using the local path to the image instead of the public path (wp-content/uploads/2015/06/my_image.png instead of www.mysite.com/wp-content/uploads/2015/06/my_image.png). WordPress could really use more helpful messaging here.

  2. Additionally, here is a function to use everywhere in the website. Add this in functions.php:

    function ab_image_resize($imgurl, $width, $height, $imgname) {
         if(!$imgurl) { $imgurl = get_stylesheet_directory().'/default.png'; } //set a default image if no image is found
             $image = wp_get_image_editor( $imgurl );
             if ( ! is_wp_error( $image ) ) {
                $image->resize( $width, $height, true );
                $filename = $image->generate_filename( $imgname, ABSPATH.'wp-content/uploads/ab_resized/', NULL ); //create the upload folder; here: ab_resized
                $image->save( $filename );
                $new_img_name = basename($filename);
                return get_site_url().'/wp-content/uploads/ab_resized/'.$new_img_name;
    }
       else {
            return "An error occured while saving the image";
       }
    }
    

    Call this as:

     ab_image_resize('full_image_url_here', $width, $height, 'any_name_here');
    

Comments are closed.