Published on

分享几个基于Golang/Rust的图片处理开源项目

Authors
  • avatar
    Name
    ttyS3
    Twitter

1. imgproxy/imgproxy (golang, 4.6K star)

imgproxy 目前有 4.6K star

https://github.com/imgproxy/imgproxy

Fast and secure standalone server for resizing and converting remote images

imgproxy 是一个快速安全的独立服务器,用于调整远程图像的大小和转换远程图像。 imgproxy 的主要原则是简单,速度和安全性。

imgproxy 只做一件事 —— 调整远程图像的大小 —— 而且做得很好。 当您需要动态调整多个图像的大小以使其与您的应用程序设计匹配时,它非常有用,而无需准备大量缓存的调整大小的图像或每次设计更改时都重新执行。

imgproxy是一个go应​​用程序,准备安装并在任何Unix环境中使用 - 也可以使用Docker容器化。

简单

"No code is better than no code."

这句话我保持原样不做翻译,任何翻译都会导致变味。简单来说,imgproxy 只会包含那些必须的功能,而一些补充性可有可无的功能就不会包含进来。

imgproxy 仅包括用于图像处理,微调和安全性的必须具有的功能。

它的简单体现在哪里呢?

  1. 能方很容易使用CSS3完成功能的,坚决不集成,比如 rotate, flip  和应用蒙板层这类操作。

  2. 不内置HTTP缓存功能,因为这个目的很容易通过 CDN 或者 专门的缓存代理服务器 来实现

  3. 不会试图内置实现所有功能,比如 https 支持,因为这个很容易就可以通过一些专业的 http 代理服务器来实现, 比如 nginx

拥有内置的所有内容 - 例如HTTPS支持可能是有用的 - 但是解决这一问题的简单方法是使用诸如nginx等代理HTTP服务器。

高效

高效处理图片的主要原因是,它的图片处理并不是通过原生golang来实现的,而是通过调用C库libvipslibvips号称是所有图片处理库里速度最快的。(没错, 在缩放图片这种操作上, ImageMagick 或 GraphicsMagick 也 PK 不过 libvips)

官方甚至提供了一个 benchmark 可供参考:

ToolTime taken for tests
(sec)
Requests per secondTime per request
(ms, mean)
Memory peak usage
(MB)
Result file size
(KB)
imgproxy103.4059.67413.61819443.51
thumbor160.5056.23642.02146145.10
imaginary104.8739.54419.49456292.93
Pilbox179.4825.57717.927106095.64
picfit1220.4120.824881.646193498.67
imageproxy1209.3610.834837.443239298.74

安全

在安全方面,大量处理远程图像是一件潜在的危险事情。

* imgproxy 在下载时会检查图像类型和“真实”尺寸,因此如果图像格式未知或尺寸太大(有一个设置),则不会完全下载图像。 这就是 imgproxy 如何保护您免受所谓的“图像炸弹”的侵害,例如在 https://www.bamsoftware.com/hacks/deflate.html 中描述的那些。

* imgproxy 使用签名保护图像 URL,因此攻击者无法通过请求多个图像调整大小来引发拒绝服务攻击。

* imgproxy 支持通过 HTTP header 进行授权。 这可以防止攻击者直接使用 imgproxy,但允许通过 CDN 或缓存服务器使用它——只需向代理或 CDN 配置添加 header 即可。

--------------------------------------------------------------------------------------

相关文档:

* https://docs.imgproxy.net/

* https://evilmartians.com/chronicles/introducing-imgproxy

2. imazen/imageflow (rust, 3.5K star)

https://docs.imageflow.io/

https://github.com/imazen/imageflow

https://docs.imageflow.io/querystring/introduction.html

高性能图片处理服务器,包含imageflow_server, imageflow_tool, and libimageflow

Docker Pulls

Docker Pulls

view releases

license: Choose AGPLv3 or Commercial

  • imageflow_tool is a command-line tool for experimenting, running batch jobs, or when you want process isolation. Up to 17x faster than ImageMagick. Also produces smaller files at higher quality.
  • imageflow_server can run JSON jobs or manipulate images in-flight (e.g./bucket/img.jpg?w=200) for direct use from HTML. Source images can reside in blob storage, on another server, or on the filesystem. However, for production use, we recommend using Imageflow.NET Server, which is far more feature-rich and doesn't need a reverse proxy in front for security.
  • libimageflow is for direct (in-process) use from your programming language. See our Node bindings, Go bindings, Scala bindings, Elixir bindings, or .NET bindings. If we don't already have bindings for your language, consider spending a day to add them. Imageflow has a simple C-compatible ABI, of which only 4 methods are needed to implement bindings.

3. h2non/imaginary (golang, 3.7K star)

https://github.com/h2non/imaginary

https://fly.io/launch/github/h2non/imaginary

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

imaginary 也是用的 libvips

容器化支持较好。

Supported image operations

  • Resize
  • Enlarge
  • Crop
  • SmartCrop (based on libvips built-in algorithm)
  • Rotate (with auto-rotate based on EXIF orientation)
  • AutoRotate with further image transformations (based on EXIF metadata orientation)
  • Flip (with auto-flip based on EXIF metadata)
  • Flop
  • Zoom
  • Thumbnail
  • Fit
  • Pipeline of multiple independent image transformations in a single HTTP request.
  • Configurable image area extraction
  • Embed/Extend image, supporting multiple modes (white, black, mirror, copy or custom background color)
  • Watermark (customizable by text)
  • Watermark image
  • Custom output color space (RGB, black/white...)
  • Format conversion (with additional quality/compression settings)
  • Info (image size, format, orientation, alpha...)
  • Reply with default or custom placeholder image in case of error.
  • Blur

4. thoas/picfit (golang, 1.5K star)

picfit is a reusable Go server to manipulate images (resize, thumbnail, etc.).

It will act as a proxy on your storage engine and will be served ideally behind an HTTP cache system like varnish.

It supports multiple storage backends and multiple key/value stores.

[KVSTORE] can be:

  • redis - generated keys stored in Redis, see below how you can customize connection parameters
  • cache - generated keys stored in an in-memory cache
  • redis-cluster - generated keys stored in Redis cluster

[STORAGE] can be:

  • fs - generated images stored in your File system
  • http+fs - generated images stored in your File system and loaded using HTTP protocol
  • s3 - generated images stored in Amazon S3
  • dos3 - generated images stored in DigitalOcean S3
  • gcs - generated images stored in Google Cloud Storage
  • http+s3 - generated images stored in Amazon S3 and loaded using HTTP protocol
  • http+dos3 - generated images stored in DigitalOcean S3 and loaded using HTTP protocol

5. pierrre/imageserver (golang, 1.9K star)

imageserver 并不是一个到手就能用的服务,而是一个golang module.

Features

不过集成使用它也比较简单:

// https://github.com/pierrre/imageserver/blob/master/examples/simple/simple.go
// Package simple provides a simple example.
package main

import (
	"net/http"https://github.com/imgproxy/imgproxy

	"github.com/pierrre/imageserver"
	imageserver_http "github.com/pierrre/imageserver/http"
	imageserver_http_gift "github.com/pierrre/imageserver/http/gift"
	imageserver_http_image "github.com/pierrre/imageserver/http/image"
	imageserver_image "github.com/pierrre/imageserver/image"
	_ "github.com/pierrre/imageserver/image/gif"
	imageserver_image_gift "github.com/pierrre/imageserver/image/gift"
	_ "github.com/pierrre/imageserver/image/jpeg"
	_ "github.com/pierrre/imageserver/image/png"
	imageserver_testdata "github.com/pierrre/imageserver/testdata"
)

func main() {
	http.Handle("/", &imageserver_http.Handler{
		Parser: imageserver_http.ListParser([]imageserver_http.Parser{
			&imageserver_http.SourceParser{},
			&imageserver_http_gift.ResizeParser{},
			&imageserver_http_image.FormatParser{},
			&imageserver_http_image.QualityParser{},
		}),
		Server: &imageserver.HandlerServer{
			Server: imageserver_testdata.Server,
			Handler: &imageserver_image.Handler{
				Processor: &imageserver_image_gift.ResizeProcessor{},
			},https://github.com/imgproxy/imgproxy
		},
	})
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}
}