Cloudinary API转换问题

时间:2019-05-31 10:16:59

标签: javascript cloudinary

我将Cloudinary用作应用程序的图像存储,但是在转换图像时遇到问题。

我想要实现的是:

  • 如果图像的宽度大于1920像素(将其视为风景图像):宽度应裁剪为1920像素(保持比例);
  • 如果图像的高度大于1920像素(将其视为人像图像):高度应裁剪为1920像素(保持比例);
  • 格式始终为jpg;
  • 质量降低到80以减轻图像重量。

我目前的积分是:

const transformation = {
  width: 1920, max_height: 1920, format: 'jpg', quality: 80
};

但不幸的是,此输出是方形图像...

我正在尝试使用if,但我不知道该如何使用...看来编写文档始终是最难的事情...

希望您能帮助我找到正确的方法。我使用的是SDK,因此都是纯JavaScript。

1 个答案:

答案 0 :(得分:0)

There isn't a max_height parameter in the SDK when creating transformations, but you can achieve what you're looking for by setting both width and height to the maximum value you want to allow.

If you're getting a square output, it's because the default crop/resize mode is scale, which produces an output in exactly the dimensions requested, but may result in the image being 'squashed' or 'stretched'.

What I think you're looking for here is something like this:

const transformation = {
  width: 1920, height: 1920, format: 'jpg', quality: 'auto', crop: 'limit'
};

Which gives you a URL like this: https://res.cloudinary.com/demo/image/upload/w_1920,h_1920,f_jpg,q_auto,c_limit/bike.jpg

The 'limit' crop mode will scale the image down (not up) until it's smaller than 1920x1920, and will keep the original aspect ratio