Resize External Image

I wrote this function for you, which will first make a copy of the external image, crop it , and return the ID of it:

function external_image_loader ($image_url, $post_id, $title) {
    $img_name = basename ($image_url);
    global $wpdb;
    $query = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value="$img_name"";
    $id = $wpdb->get_var($query);
    if (is_numeric($id)) {
        return $id;
    } else {
        $attachment_src = media_sideload_image( $image_url, $post_id, $title,'src' );
        $query = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value="$img_name"";
        $id = $wpdb->get_var($query);
        return $id;
    }
}

This is how you are gonna use this function:

$small_image_url = wp_get_attachment_image_src(external_image_loader($image_url, $post_id, $title), 'photography-gallery-grid', true);

How does it work?

First, the function is fed with an image URL and a post id. These 2 are required, however the title is not. It will check if the image already exists in your database, if id does, the function returns an ID to wp_get_attachment_image_src() which will end in the result you are after.

But if not, it will download a copy of the image to your uploads folder, by using media_sideload_image(). After this, it will search for the image in the database and return an ID for it.

If the image has a height of 529px and smaller, it will not be cropped.

If you don’t want to save a copy of the image, you have to do this by JavaScript. Take a look into this question.

As i mentioned before, doing this is also possible by PHP without saving the image, but this will end up in exhausting your server’s resources and slowing down your website.