Fast HTTP microservice written in Go for high-level image processing backed by bimg and libvips. imaginary
can be used as private or public HTTP service for massive image processing with first-class support for Docker & Fly.io.
It's almost dependency-free and only uses net/http
native package without additional abstractions for better performance.
Supports multiple image operations exposed as a simple HTTP API, with additional optional features such as API token authorization, URL signature protection, HTTP traffic throttle strategy and CORS support for web clients.
imaginary
can read images from HTTP POST payloads, server local path or remote HTTP servers, supporting JPEG, PNG, WEBP, HEIF, and optionally TIFF, PDF, GIF and SVG formats if libvips@8.3+
is compiled with proper library bindings.
imaginary
is able to output images as JPEG, PNG and WEBP formats, including transparent conversion across them.
imaginary
optionally supports image placeholder fallback mechanism in case of image processing error or server error of any nature, hence an image will be always returned by imaginary even in case of error, trying to match the requested image size and format type transparently. The error details will be provided in the response HTTP header Error
field serialized as JSON.
imaginary
uses internally libvips
, a powerful and efficient library written in C for fast image processing
which requires a low memory footprint
and it's typically 4x faster than using the quickest ImageMagick and GraphicsMagick
settings or Go native image
package, and in some cases it's even 8x faster processing JPEG images.
To get started, take a look the installation steps, usage cases and API docs.
go get -u github.com/h2non/imaginary
Also, be sure you have the latest version of bimg
:
go get -u github.com/h2non/bimg
Run the following script as sudo
(supports OSX, Debian/Ubuntu, Redhat, Fedora, Amazon Linux):
curl -s https://raw.githubusercontent.com/h2non/bimg/master/preinstall.sh | sudo bash -
The install script requires curl
and pkg-config
See Dockerfile for image details.
Fetch the image (comes with latest stable Go and libvips
versions)
docker pull h2non/imaginary
Start the container with optional flags (default listening on port 9000)
docker run -p 9000:9000 h2non/imaginary -cors -gzip
Start the container enabling remote URL source image processing via GET requests and url
query param.
docker run -p 9000:9000 h2non/imaginary -p 9000 -enable-url-source
Start the container enabling local directory image process via GET requests and file
query param.
docker run -p 9000:9000 h2non/imaginary -p 900 -mount /volume/images
Start the container in debug mode:
docker run -p 9000:9000 -e "DEBUG=*" h2non/imaginary
Enter to the interactive shell in a running container
sudo docker exec -it <containerIdOrName> bash
Stop the container
docker stop h2non/imaginary
For more usage examples, see the command line usage.
All Docker images tags are available here.
You can add imaginary
to your docker-compose.yml
file:
version: "3" services: imaginary: image: h2non/imaginary:latest # optionally mount a volume as local image source volumes: - images:/mnt/data environment: PORT: 9000 command: -enable-url-source -mount /mnt/data ports: - "9000:9000"
Deploy imaginary in seconds close to your users in Fly.io cloud by clicking on the button below:
<a href="https://fly.io/docs/app-guides/run-a-global-image-service/"> <img src="testdata/flyio-button.svg?raw=true" width="200"> </a>Fly is a platform for applications that need to run globally. It runs your code close to users and scales compute in cities where your app is busiest. Write your code, package it into a Docker image, deploy it to Fly's platform and let that do all the work to keep your app snappy.
You can learn more about how Fly.io can reduce latency and provide a better experience by serving traffic close to your users location.
Learn more about how to run a custom deployment of imaginary on the Fly.io cloud.
Assuming you have cloudfoundry account, bluemix or pivotal and command line utility installed.
Clone this repository:
git clone https://github.com/h2non/imaginary.git
Push the application
cf push -b https://github.com/yacloud-io/go-buildpack-imaginary.git imaginary-inst01 --no-start
Define the library path
cf set-env imaginary-inst01 LD_LIBRARY_PATH /home/vcap/app/vendor/vips/lib
Start the application
cf start imaginary-inst01
Click to deploy on Google Cloud Run:
Given the multithreaded native nature of Go, in terms of CPUs, most cores means more concurrency and therefore, a better performance can be achieved. From the other hand, in terms of memory, 512MB of RAM is usually enough for small services with low concurrency (<5 requests/second). Up to 2GB for high-load HTTP service processing potentially large images or exposed to an eventual high concurrency.
If you need to expose imaginary
as public HTTP server, it's highly recommended to protect the service against DDoS-like attacks.
imaginary
has built-in support for HTTP concurrency throttle strategy to deal with this in a more convenient way and mitigate possible issues limiting the number of concurrent requests per second and caching the awaiting requests, if necessary.
In production focused environments it's highly recommended to enable the HTTP concurrency throttle strategy in your imaginary
servers.
The recommended concurrency limit per server to guarantee a good performance is up to 20
requests per second.
You can enable it simply passing a flag to the binary:
$ imaginary -concurrency 20
In case you are experiencing any persistent unreleased memory issues in your deployment, you can try passing this environment variables to imaginary
:
MALLOC_ARENA_MAX=2 imaginary -p 9000 -enable-url-source
When you use a cluster, it is necessary to control how the deployment is executed, and it is very useful to finish the containers in a controlled manner.
You can use the next command:
$ ps auxw | grep 'bin/imaginary' | awk 'NR>1{print buf}{buf = $2}' | xargs kill -TERM > /dev/null 2>&1
If you're looking for a large scale solution for massive image processing, you should scale imaginary
horizontally, distributing the HTTP load across a pool of imaginary servers.
Assuming that you want to provide a high availability to deal efficiently with, let's say, 100 concurrent req/sec, a good approach would be using a front end balancer (e.g: HAProxy) to delegate the traffic control flow, ensure the quality of service and distribution the HTTP across a pool of servers:
|==============|
| Dark World |
|==============|
||||
|==============|
| Balancer |
|==============|
| |
/ \
/ \
/ \
/-----------\ /-----------\
| imaginary | | imaginary | (*n)
\-----------/ \-----------/
Feel free to send a PR if you created a client for other language.
libvips is probably the faster open source solution for image processing. Here you can see some performance test comparisons for multiple scenarios:
See benchmark.sh for more details
Environment: Go 1.4.2. libvips-7.42.3. OSX i7 2.7Ghz
Requests [total] 200
Duration [total, attack, wait] 10.030639787s, 9.949499515s, 81.140272ms
Latencies [mean, 50, 95, 99, max] 83.124471ms, 82.899435ms, 88.948008ms, 95.547765ms, 104.384977ms
Bytes In [total, mean] 23443800, 117219.00
Bytes Out [total, mean] 175517000, 877585.00
Success [ratio] 100.00%
Status Codes [code:count] 200:200
imaginary
can deal efficiently with up to 20 request per second running in a multicore machine,
where it crops a JPEG image of 5MB and spending per each request less than 100 ms
The most expensive image operation under high concurrency scenarios (> 20 req/sec) is the image enlargement, which requires a considerable amount of math operations to scale the original image. In this kind of operation the required processing time usually grows over the time if you're stressing the server continuously. The advice here is as simple as taking care about the number of concurrent enlarge operations to avoid server performance bottlenecks.
Usage:
imaginary -p 80
imaginary -cors
imaginary -concurrency 10
imaginary -path-prefix /api/v1
imaginary -enable-url-source
imaginary -disable-endpoints form,health,crop,rotate
imaginary -enable-url-source -allowed-origins http://localhost,http://server.com,http://*.example.org
imaginary -enable-url-source -enable-auth-forwarding
imaginary -enable-url-source -authorization "Basic AwDJdL2DbwrD=="
imaginary -enable-placeholder
imaginary -enable-url-source -placeholder ./placeholder.jpg
imaginary -enable-url-signature -url-signature-key 4f46feebafc4b5e988f131c4ff8b5997
imaginary -enable-url-source -forward-headers X-Custom,X-Token
imaginary -h | -help
imaginary -v | -version
Options:
-a <addr> Bind address [default: *]
-p <port> Bind port [default: 8088]
-h, -help Show help
-v, -version Show version
-path-prefix <value> Url path prefix to listen to [default: "/"]
-cors Enable CORS support [default: false]
-gzip Enable gzip compression (deprecated) [default: false]
-disable-endpoints Comma separated endpoints to disable. E.g: form,crop,rotate,health [default: ""]
-key <key> Define API key for authorization
-mount <path> Mount server local directory
-http-cache-ttl <num> The TTL in seconds. Adds caching headers to locally served files.
-http-read-timeout <num> HTTP read timeout in seconds [default: 60]
-http-write-timeout <num> HTTP write timeout in seconds [default: 60]
-enable-url-source Enable remote HTTP URL image source processing (?url=http://..)
-enable-placeholder Enable image response placeholder to be used in case of error [default: false]
-enable-auth-forwarding Forwards X-Forward-Authorization or Authorization header to the image source server. -enable-url-source flag must be defined. Tip: secure your server from public access to prevent attack vectors
-forward-headers Forwards custom headers to the image source server. -enable-url-source flag must be defined.
-enable-url-signature Enable URL signature (URL-safe Base64-encoded HMAC digest) [default: false]
-url-signature-key The URL signature key (32 characters minimum)
-allowed-origins <urls> Restrict remote image source processing to certain origins (separated by commas). Note: Origins are validated against host *AND* path.
-max-allowed-size <bytes> Restrict maximum size of http image source (in bytes)
-max-allowed-resolution <megapixels> Restrict maximum resolution of the image [default: 18.0]
-certfile <path> TLS certificate file path
-keyfile <path> TLS private key file path
-authorization <value> Defines a constant Authorization header value passed to all the image source servers. -enable-url-source flag must be defined. This overwrites authorization headers forwarding behavior via X-Forward-Authorization
-placeholder <path> Image path to image custom placeholder to be used in case of error. Recommended minimum image size is: 1200x1200
-concurrency <num> Throttle concurrency limit per second [default: disabled]
-burst <num> Throttle burst max cache size [default: 100]
-mrelease <num> OS memory release interval in seconds [default: 30]
-cpus <num> Number of used cpu cores.
(default for current machine is 8 cores)
-log-level Set log level for http-server. E.g:
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、 商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞 星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工 具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。
专业的AI公文写作平台,公文写作神器
AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提 升办公效率,是体制打工人的得力写作神器。