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
2 Answers
Reset to default 0Without 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
版权声明:本文标题:wp_download_url() for dynamically generated image 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745380441a2656134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论