admin管理员组

文章数量:1426222

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

,h_400,c_crop,g_face,r_max/w_200/lady.jpg

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

I'm trying to download images from external sites via api (newsapi) 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/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?

Share Improve this question asked Jun 21, 2019 at 15:15 RunnickRunnick 1,0593 gold badges14 silver badges26 bronze badges 1
  • 1 Can you drop in a code snippet of how you are using it? – ChristopherJones Commented Jun 21, 2019 at 17:24
Add a comment  | 

2 Answers 2

Reset to default 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/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:

  • https://codex.wordpress/Function_Reference/media_sideload_image
  • https://developer.wordpress/reference/functions/media_sideload_image/

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/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>';

本文标签: wpdownloadurl() for dynamically generated image