10 个版本
0.3.0 | 2024年6月1日 |
---|---|
0.2.0 | 2024年5月3日 |
0.1.7 | 2024年4月15日 |
0.1.3 | 2024年3月30日 |
114 在 图像 中排名
910 每月下载量
120KB
1.5K SLoC
Respicta - 图像调整大小
Respicta 是一个多才多艺的项目,提供用于无缝调整图像大小和更改格式的库、命令行界面 (CLI) 和网络服务。
特性
调整图像大小:轻松调整图像到所需尺寸。
更改格式:将图像转换为不同的格式,如 JPEG、PNG 等。
CLI:直观的命令行界面,用于快速调整大小和格式转换。
网络服务:托管网络服务以实时调整图像大小。
支持的转换
- 将 Gif 转换为 WebP
- 将 Jpeg 转换为 WebP
- 将 Png 转换为 Jpeg
- 将 Png 转换为 WebP
CLI
转换
docker run --rm -v ./:/images rayros/respicta convert --help
Convert images from one format to another
Usage: respicta convert [OPTIONS] <INPUT_PATH> <OUTPUT_PATH>
Arguments:
<INPUT_PATH> Input image path
<OUTPUT_PATH> Output image path
Options:
-w, --width <WIDTH> Width of the output image If not set, the width will be the same as the input image
-h, --height <HEIGHT> Height of the output image If not set, the height will be the same as the input image
-q, --quality <QUALITY> Quality of the output image. If not set, the quality will be the same as the input image. The value must be between 1 and 100. The higher the value, the better the quality
--help
Examples:
respicta convert --width 100 --height 100 --quality 75 input.jpg output.jpg
服务器
docker run --rm rayros/respicta server --help
Start a server to convert images
Usage: respicta server [OPTIONS]
Options:
-a, --address <ADDRESS> Address to bind the server (default: 0.0.0.0:3000)
-l, --limit <LIMIT> Maximum file size in bytes (default: 10MB)
-h, --help Print help
客户端示例
const fs = require('fs');
const { Readable } = require('stream');
const { finished } = require('stream/promises');
const filePath = './images/logo.jpeg';
const url = 'http://0.0.0.0:3000/?width=200&height=200&extension=jpeg';
fs.readFile(filePath, async (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
const blob = new Blob([data], { type: 'image/jpeg' });
const formData = new FormData();
formData.append('file', blob, 'logo_small.jpeg');
const response = await fetch(url, {
method: 'POST',
body: formData,
})
if (!response.ok) {
const text = await response.text();
console.error('Error:', text);
return;
}
const fileWriteStream = fs.createWriteStream('./images/logo_small.jpeg');
await finished(Readable.fromWeb(response.body).pipe(fileWriteStream));
});
命令服务器 - 通过 http 发送 CLI 命令
Start a command server
Usage: respicta command-server [OPTIONS]
Options:
-a, --address <ADDRESS> Address to bind the server (default: 0.0.0.0:3000)
-h, --help Print help
客户端示例
const run = async () => {
const response = await fetch('http://0.0.0.0:3000/', {
method: 'POST',
body: JSON.stringify({
input_path: './images/logo.jpeg',
output_path: './images/logo_small.jpeg',
width: 200,
height: 200,
}),
headers: {
'Content-Type': 'application/json',
}
})
if (!response.ok) {
const text = await response.text();
console.error('Error:', text);
return;
}
};
run().catch(console.error);
Postman 客户端
作为一个库
use respicta::convert;
fn main() {
convert(&respicta::Config::new(
"images/logo.jpeg",
"images/logo_small.jpeg",
Some(200),
Some(200),
))
.unwrap();
}
Docker compose
version: '3.9'
services:
respicta:
image: rayros/respicta:latest
restart: always
command: command-server
volumes:
- ./data/respicta:/data
main-app:
build:
context: .
dockerfile: ./Dockerfile
volumes:
- ./data/respicta:/data
restart: always
environment:
RESPICTA_HREF: http://respicta:3000
Kubernetes
服务器部署
如何在 pod 中使用 respicta 来为您的自定义调整大小服务。
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-resizer-service
name: my-resizer-service
spec:
replicas: 1
selector:
matchLabels:
app: my-resizer-service
template:
metadata:
labels:
app: my-resizer-service
spec:
containers:
- image: rayros/respicta
name: respicta
args: ["server", "--address", "0.0.0.0:4000"]
- image: main-app-image:latest
name: main-app
ports:
- containerPort: 2137
env:
- name: RESPICTA_HREF
value: https://127.0.0.1:4000
命令服务器部署
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-resizer-service
name: my-resizer-service
spec:
replicas: 1
selector:
matchLabels:
app: my-resizer-service
template:
metadata:
labels:
app: my-resizer-service
spec:
containers:
- image: rayros/respicta:latest
name: respicta
args: ["command-server", "--address", "0.0.0.0:4000"]
volumeMounts:
- name: data-volume
mountPath: /data
- image: main-app-image:latest
name: main-app
ports:
- containerPort: 2137
env:
- name: RESPICTA_HREF
value: https://127.0.0.1:4000
volumeMounts:
- name: data-volume
mountPath: /data
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: my-resizer-storage-pvc
程序被用于
依赖项
~13–27MB
~415K SLoC