[分享]unsplash免费图片接口使用与实战
目录 ▼
😀 最近发现一个不错的网站封面制作的网站——《https://coverview.vercel.app/》,通过使用它,我生成了非常多的自定义的图片封面。一次偶然间,发现里面的图片都是 unsplash 提供的。为何我不试试看自己实现一个获取 unsplash 免费图片的接口呢?于是就有了这篇文章。
什么是 unsplash?
网站欣赏——Unsplash
下面我们重点欣赏下这个网站:
Photos for everyone


维基百科
从维基百科上也可以看出:
Unsplash is a website dedicated to proprietary stock photography. Since 2021, it has been owned by Getty Images. The website claims over 330,000 contributing photographers and generates more than 13 billion photo impressions per month on their growing library of over 5 million photos (as of April 2023).[1][2] Unsplash has been cited as one of the world’s leading photography websites by Forbes, Design Hub, CNET, Medium and The Next Web.[3][4][5][6][7]
Unsplash 是一个致力于专有图库摄影的网站。 自 2021 年起,它归盖蒂图片社所有。 该网站声称拥有超过330,000名贡献摄影师,其不断增长的超过500万张照片库(截至2023年4月)每月产生超过130亿张照片展示。Unsplash 被福布斯、Design Hub、CNET、Medium 和 The Next Web 评为世界领先的摄影网站之一。
听听 ChatGPT怎么说
【ChatGPT4-turbo的回答】
如何用起来呢?
这个网站这么强,如何使用起来呢?
很巧,它提供了可供使用的开发者API使用。下面说下如何快速使用unsplash API。
下面是Unsplash API文档
如何快速实现获取免费图片接口
💡 步骤说明:
图片使用
图片的使用方式,可以在附录中看到示例,一般从 urls 字段中获取,可以按需使用,可供选择的有:
【Unsplash API Documentation | Free HD Photo API | Unsplash】
示例说明:
如果您的应用程序需要宽度为 1500px、DPR 为 2 的图像,请获取原始 URL 并添加 w=1500 和 dpr=2 参数来创建新图像:
photo.urls.raw + "&w=1500&dpr=2";
// => https://images.unsplash.com/photo-1461988320302-91bde64fc8e4?ixid=2yJhcHBfaWQiOjEyMDd9&w=1500&dpr=2
如果应用程序的另一部分需要相同的图像,但宽度为一半,您可以轻松构建另一个 URL,而无需再次访问 API:
photo.urls.raw + "&w=750&dpr=2";
// => https://images.unsplash.com/photo-1461988320302-91bde64fc8e4?ixid=2yJhcHBfaWQiOjEyMDd9&w=750&dpr=2
🚫 注意事项
总结
注册好 unsplash 网站的账户之后,新建一个APP用于图片接口的开发,就能自己使用接口来做一些事情了。例如根据自己需要获取相关的图片,使用各种大小格式的图片进行网站的使用,甚至是二次加工使用,都非常方便。
附录
unsplash_client.py 示例代码展示
随机图片返回示例
{
"id": "Dwu85P9SOIk",
"created_at": "2016-05-03T11:00:28-04:00",
"updated_at": "2016-07-10T11:00:01-05:00",
"width": 2448,
"height": 3264,
"color": "#6E633A",
"blur_hash": "LFC$yHwc8^$yIAS$%M%00KxukYIp",
"downloads": 1345,
"likes": 24,
"liked_by_user": false,
"description": "A man drinking a coffee.",
"exif": {
"make": "Canon",
"model": "Canon EOS 40D",
"exposure_time": "0.011111111111111112",
"aperture": "4.970854",
"focal_length": "37",
"iso": 100
},
"location": {
"name": "Montreal, Canada",
"city": "Montreal",
"country": "Canada",
"position": {
"latitude": 45.473298,
"longitude": -73.638488
}
},
"current_user_collections": [ // The *current user's* collections that this photo belongs to.
{
"id": 206,
"title": "Makers: Cat and Ben",
"published_at": "2016-01-12T18:16:09-05:00",
"last_collected_at": "2016-06-02T13:10:03-04:00",
"updated_at": "2016-07-10T11:00:01-05:00",
"cover_photo": null,
"user": null
},
// ... more collections
],
"urls": {
"raw": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d",
"full": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg",
"regular": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=1080&fit=max",
"small": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=400&fit=max",
"thumb": "https://images.unsplash.com/photo-1417325384643-aac51acc9e5d?q=75&fm=jpg&w=200&fit=max"
},
"links": {
"self": "https://api.unsplash.com/photos/Dwu85P9SOIk",
"html": "https://unsplash.com/photos/Dwu85P9SOIk",
"download": "https://unsplash.com/photos/Dwu85P9SOIk/download"
"download_location": "https://api.unsplash.com/photos/Dwu85P9SOIk/download"
},
"user": {
"id": "QPxL2MGqfrw",
"updated_at": "2016-07-10T11:00:01-05:00",
"username": "exampleuser",
"name": "Joe Example",
"portfolio_url": "https://example.com/",
"bio": "Just an everyday Joe",
"location": "Montreal",
"total_likes": 5,
"total_photos": 10,
"total_collections": 13,
"instagram_username": "instantgrammer",
"twitter_username": "crew",
"links": {
"self": "https://api.unsplash.com/users/exampleuser",
"html": "https://unsplash.com/exampleuser",
"photos": "https://api.unsplash.com/users/exampleuser/photos",
"likes": "https://api.unsplash.com/users/exampleuser/likes",
"portfolio": "https://api.unsplash.com/users/exampleuser/portfolio"
}
}
}
搜索图片返回示例
{
"total": 133,
"total_pages": 7,
"results": [
{
"id": "eOLpJytrbsQ",
"created_at": "2014-11-18T14:35:36-05:00",
"width": 4000,
"height": 3000,
"color": "#A7A2A1",
"blur_hash": "LaLXMa9Fx[D%~q%MtQM|kDRjtRIU",
"likes": 286,
"liked_by_user": false,
"description": "A man drinking a coffee.",
"user": {
"id": "Ul0QVz12Goo",
"username": "ugmonk",
"name": "Jeff Sheldon",
"first_name": "Jeff",
"last_name": "Sheldon",
"instagram_username": "instantgrammer",
"twitter_username": "ugmonk",
"portfolio_url": "http://ugmonk.com/",
"profile_image": {
"small": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32&s=7cfe3b93750cb0c93e2f7caec08b5a41",
"medium": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64&s=5a9dc749c43ce5bd60870b129a40902f",
"large": "https://images.unsplash.com/profile-1441298803695-accd94000cac?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128&s=32085a077889586df88bfbe406692202"
},
"links": {
"self": "https://api.unsplash.com/users/ugmonk",
"html": "http://unsplash.com/@ugmonk",
"photos": "https://api.unsplash.com/users/ugmonk/photos",
"likes": "https://api.unsplash.com/users/ugmonk/likes"
}
},
"current_user_collections": [],
"urls": {
"raw": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f",
"full": "https://hd.unsplash.com/photo-1416339306562-f3d12fefd36f",
"regular": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=92f3e02f63678acc8416d044e189f515",
"small": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=263af33585f9d32af39d165b000845eb",
"thumb": "https://images.unsplash.com/photo-1416339306562-f3d12fefd36f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=8aae34cf35df31a592f0bef16e6342ef"
},
"links": {
"self": "https://api.unsplash.com/photos/eOLpJytrbsQ",
"html": "http://unsplash.com/photos/eOLpJytrbsQ",
"download": "http://unsplash.com/photos/eOLpJytrbsQ/download"
}
},
// more photos ...
]
}