您可以在Cloudinary上编辑用户上传的图像的质量吗?

时间:2019-10-18 15:48:28

标签: node.js express cloudinary

我有一个网站,用户可以在其中上传图片。

我希望所有已经上传的图像+任何新上传的图像的质量都相同。

有没有一种方法可以轻松地编辑已经上传的图像,而无需让用户重新上传各自的图像?

谢谢

1 个答案:

答案 0 :(得分:0)

Cloudinary的美丽之处在于能够上传原始文档,然后进行转换。通常,建议看起来是:

  1. quality参数设置为auto(URL中的 q_auto ),以自动调整图像的质量,以在质量和尺寸之间取得适当的平衡。
  2. fetch_format参数设置为auto(URL中的 f_auto ),以根据浏览器自动选择最佳格式。
  3. 将高度和宽度调整为在页面上呈现的必要尺寸。

示例:https://res.cloudinary.com/demo/image/upload/q_auto,f_auto,w_500/bike.jpg

来源(以及更多信息)https://cloudinary.com/documentation/image_optimization)。

但是,如果您需要重新上传内容并保留相同的public_id,最简单的解决方案是使用其API。我建议执行以下操作:

  1. 使用Admin APIs get resources call检索需要重新上传的资产。我建议将其分解为resource_type,并且如果使用的话,还应将参数tags设置为true,将context设置为true注意:管理API受速率限制,因此请检查与计划here关联的使用限制。将它们存储在数组中以备后用。响应应包括public_idurltagscontext等...
  2. 遍历先前的数组,并使用Upload API's upload method使用与先前检索的相同参数(public_idurltagscontext等。例如

    current_assets = [...]
    for (asset in current_assets) {
        result = cloudinary.v2.uploader.upload(
            asset['url'], 
            { public_id: asset['public_id'], tags: asset['tags'], context: assets['context']
            // include any incoming transformations to apply to the asset during upload
            },
            (err, res) => {
                if (err) console.log(err);
            console.log(res);
        })
    }
    

来源https://cloudinary.com/documentation/upload_images?query=incoming&c_query=Incoming%20transformations#incoming_transformations

相关问题