7 个版本 (4 个破坏性更新)
0.5.0 | 2024年7月23日 |
---|---|
0.4.0 | 2023年11月27日 |
0.3.0 | 2023年11月18日 |
0.2.2 | 2023年10月23日 |
0.1.0 | 2023年6月3日 |
#426 in 网页编程
每月下载 273 次
110KB
2.5K SLoC
cloudinary
目前仅提供半功能的上传和转换功能,但如果您需要更多,请告诉我。
上传图片
可以从不同来源上传
- 本地文件
- 远程文件
- 数据 URL rfc2397
本地文件
use cloudinary::upload::{UploadOptions, Source, Upload};
let options = UploadOptions::new().set_public_id("file.jpg".to_string());
let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_secret".to_string() );
let result = upload.image(Source::Path("./image.jpg".into()), &options);
远程文件
use cloudinary::upload::{UploadOptions, Source, Upload};
let image_url = "https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png";
let options = UploadOptions::new().set_public_id("1x1.png".to_string());
let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_secret".to_string() );
let result = upload.image(Source::Url(image_url.try_into().unwrap(), &options);
数据 URL
use cloudinary::upload::{UploadOptions, Source, Upload};
let data_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
let options = UploadOptions::new().set_public_id("1x1.png".to_string());
let upload = Upload::new("api_key".to_string(), "cloud_name".to_string(), "api_secret".to_string() );
let result = upload.image(Source::DataUrl(data_url.into()), &options);
转换图片
目前支持的转换
- 缩放
- 裁剪
- 填充
缩放图片
use cloudinary::transformation::{
Transformations::Resize, resize_mode::ResizeMode::ScaleByWidth, Image, aspect_ratio::AspectRatio
};
let image = Image::new("test".into(), "path/name.png".into())
.add_transformation(Resize(ScaleByWidth{ width:100, ar: None, liquid:None}));
assert_eq!(
image.to_string(),
"https://res.cloudinary.com/test/image/upload/c_scale,w_100/path/name.png"
);
裁剪图片
use cloudinary::transformation::{
Transformations::Crop, crop_mode::CropMode::FillByWidth, Image, aspect_ratio::AspectRatio
};
let image = Image::new("test".into(), "path/name.png".into())
.add_transformation(Crop(FillByWidth{ width:100, ar: None, gravity: None}));
assert_eq!(
image.to_string(),
"https://res.cloudinary.com/test/image/upload/c_fill,w_100/path/name.png"
);
填充图片
use cloudinary::transformation::{
Transformations::Pad, pad_mode::PadMode::PadByWidth, Image, aspect_ratio::AspectRatio
};
let image = Image::new("test".into(), "path/name.png".into())
.add_transformation(Pad(PadByWidth{ width:100, ar: None, background: None, gravity: None}));
assert_eq!(
image.to_string(),
"https://res.cloudinary.com/test/image/upload/c_pad,w_100/path/name.png"
);
从 URL 获取图片
非官方 API。这不是 Cloudinary 支持的,可能会随时中断。官方应使用从上传中获取的 public_id。
use cloudinary::transformation::Image;
use url::Url;
let image = Image::try_from(
Url::parse("https://res.cloudinary.com/test/image/upload/path/name.png").unwrap()
).unwrap();
assert_eq!(image.to_string(), "https://res.cloudinary.com/test/image/upload/path/name.png");
获取给定标签的所有资产的列表
use cloudinary::tags::get_tags;
let tags = get_tags("cloud_name".into(), "tag_name".into()).await;
最低支持的 Rust 版本
此 crate 最低支持的 Rust 版本是 1.65
许可证: MIT OR Apache-2.0
依赖项
~8–23MB
~319K SLoC