首页 > 解决方案 > Unsplash Api 下载特定大小的图像

问题描述

当调用 Unsplash API 时https://api.unsplash.com/photos/?client_id=#{dummy_client_id},它会返回以下格式的 JSON

[
  {
    "id": "kmTq5GU6c-0",
    "created_at": "2019-05-27T14:07:14-04:00",
    "updated_at": "2020-08-21T01:25:29-04:00",
    "promoted_at": null,
    "width": 5472,
    "height": 3648,
    "color": "#FFFFFF",
    "description": null,
    "alt_description": "man riding black motorcycle",
    "urls": {
      "raw": "https://images.unsplash.com/photo-1558980394-da1f85d3b540?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEzMDkxNX0",
      "full": "https://images.unsplash.com/photo-1558980394-da1f85d3b540?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjEzMDkxNX0",
      "regular": "https://images.unsplash.com/photo-1558980394-da1f85d3b540?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEzMDkxNX0",
      "small": "https://images.unsplash.com/photo-1558980394-da1f85d3b540?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjEzMDkxNX0",
      "thumb": "https://images.unsplash.com/photo-1558980394-da1f85d3b540?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&ixid=eyJhcHBfaWQiOjEzMDkxNX0"
    },
    "links": {
      "self": "https://api.unsplash.com/photos/kmTq5GU6c-0",
      "html": "https://unsplash.com/photos/kmTq5GU6c-0",
      "download": "https://unsplash.com/photos/kmTq5GU6c-0/download",
      "download_location": "https://api.unsplash.com/photos/kmTq5GU6c-0/download"
    },
    "categories": [
      
    ],
    "likes": 409,
    "liked_by_user": false,
    "current_user_collections": [
      
    ],
    "sponsorship": {
      
    },
    "user": {
      
    }
  }
]

我需要下载图像,并且 unsplash 指南指出您需要使用links['download']来下载图像。下载的图像总是大于 4MB。

如何下载较小尺寸/宽度/高度的图像。?

标签: javascriptimageperformanceapidownload

解决方案


推荐阅读