My theme is using a function like below to get post feature image with a specific size. From font-end, 'photography-gallery-grid' means to crop image with height 529px and keep the width proportionally though I can not find where it is.

  $small_image_url = wp_get_attachment_image_src($image_id, 'photography-gallery-grid', true);

What I need is I have external image url and trying to use instead of the one in Media Library, I want to crop it the same way that function does. I've been trying but still can not find a solution yet. Please help me for example:

 I want http://imagehost.com/doraemon.jpg  
 to become http://imagehost.com/doraemon-700x529.jpg
  • So how it's connected to wordpress, if you not going to copy this image to your host ? Commented Apr 21, 2017 at 10:40
  • You want to crop the external image without saving it? The only possible solution here will be using JavaScript. If you wanna crop it on the fly, it will burst down your resources. Let me know if you need further help.
    – Johansson
    Commented Apr 21, 2017 at 10:43

1 Answer 1


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.

  • Hi, thanks for your fast response, the reason is my WP is just a corporate site for selling photos (which are located in a different host site for purchasing). I thought this way will help me from uploading to WP again. I will give it a try.
    – pexichdu
    Commented Apr 21, 2017 at 11:06

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.