0

I'm trying to download images from external sites via api (newsapi.org) and use those as thumbnails, using wp_download_url. Although it doesn’t work on so-called dynamically generated images, e.g.

https://res.cloudinary.com/demo/image/upload/w_400,h_400,c_crop,g_face,r_max/w_200/lady.jpg

Is there a way to make it fetch such images too?

1
  • 1
    Can you drop in a code snippet of how you are using it? – ChristopherJones Jun 21 '19 at 17:24
0

Without more information, it's hard to tell what the reason is, for a specific answer you might want to show your code.

The following uploads the lady to the media library:

$url = 'https://res.cloudinary.com/demo/image/upload/w_400,h_400,c_crop,g_face,r_max/w_200/lady.jpg';
// $post_id = 0 to not attach to specific post
media_sideload_image( $url, 0 );

If you're outside /wp-admin, you'll need:

require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');

Consult the documentation for further information:

0

You can pull down images with PHP if that will work for you. You just need to make sure wherever you are "Putting" the images, needs to have the write permissions.

I got the following snippet of code to work on the image url you placed above.

// Remote image URL
$img_source = 'https://res.cloudinary.com/demo/image/upload/w_400,h_400,c_crop,g_face,r_max/w_200/lady.jpg';

// Image path
// the tmp/ folder needs to have the correct write permissions.
$img_path = 'tmp/lady.jpg';

// Save image 
$results = file_put_contents( $img_path, file_get_contents( $img_source ) );

// returns the number of bytes that were written to the file, or FALSE on failure.
echo '<pre>';
print_r($results);
echo '</pre>';
1
  • 1
    Just as a note, in most cases the use of API functions is preferable. – Nicolai Jun 21 '19 at 18:18

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.