All Projects β†’ shinsenter β†’ docker-imgproxy

shinsenter / docker-imgproxy

Licence: other
🌐 An ultra fast, production-grade on-the-fly image processing web server. Designed for high throughput with Nginx caching. Powered by imgproxy.

Programming Languages

Dockerfile
14818 projects

Projects that are alternatives of or similar to docker-imgproxy

Tinify Java
Java client for the Tinify API.
Stars: ✭ 107 (+137.78%)
Mutual labels:  image-compression, image-optimization
Aws S3 Proxy
Reverse proxy for AWS S3 with basic authentication.
Stars: ✭ 227 (+404.44%)
Mutual labels:  http2, proxy-server
Optimize Images
A command-line interface (CLI) utility written in pure Python to help you reduce the file size of images.
Stars: ✭ 141 (+213.33%)
Mutual labels:  image-compression, image-optimization
Image Resizer
On-the-fly image resizing using Node.js and libvips. Heroku Ready!
Stars: ✭ 59 (+31.11%)
Mutual labels:  image-compression, image-optimization
tinify-net
.NET client for the Tinify API.
Stars: ✭ 45 (+0%)
Mutual labels:  image-compression, image-optimization
Tinify Python
Python client for the Tinify API.
Stars: ✭ 95 (+111.11%)
Mutual labels:  image-compression, image-optimization
Tinify Php
PHP client for the Tinify API.
Stars: ✭ 204 (+353.33%)
Mutual labels:  image-compression, image-optimization
Compress Images
Minify size your images. Image compression with extension: jpg/jpeg, svg, png, gif. NodeJs
Stars: ✭ 331 (+635.56%)
Mutual labels:  image-compression, image-optimization
cover-thumbnailer
Generates folder thumbnails for various file browser on Linux to display music album covers, preview of pictures which are in a folder and more.
Stars: ✭ 56 (+24.44%)
Mutual labels:  thumbnailer, thumbnail-generator
lamba-thumbnailer
AWS S3 Video Thumbnailer with Lambda
Stars: ✭ 21 (-53.33%)
Mutual labels:  thumbnailer, thumbnail-generator
Imgp
πŸ“Έ High-performance cli batch image resizer and rotator
Stars: ✭ 744 (+1553.33%)
Mutual labels:  image-compression, image-optimization
tinify-ruby
Ruby client for the Tinify API.
Stars: ✭ 41 (-8.89%)
Mutual labels:  image-compression, image-optimization
Imgbot
An Azure Function solution to crawl through all of your image files in GitHub and losslessly compress them. This will make the file size go down, but leave the dimensions and quality untouched. Once it's done, ImgBot will open a pull request for you to review and merge. [emailΒ protected]
Stars: ✭ 732 (+1526.67%)
Mutual labels:  image-compression, image-optimization
Emage
πŸ§™β€β™‚οΈ From developers to developers: a cross-platform tool for losslessly image compression.
Stars: ✭ 99 (+120%)
Mutual labels:  image-compression, image-optimization
Imageflow
High-performance image manipulation for web servers. Includes imageflow_server, imageflow_tool, and libimageflow
Stars: ✭ 3,643 (+7995.56%)
Mutual labels:  image-compression, image-server
Essential Image Optimization
Essential Image Optimization - an eBook
Stars: ✭ 1,950 (+4233.33%)
Mutual labels:  image-compression, image-optimization
Imager
Automated image compression for efficiently distributing images on the web.
Stars: ✭ 266 (+491.11%)
Mutual labels:  image-compression, image-optimization
pngloss
Lossy compression of PNG images
Stars: ✭ 73 (+62.22%)
Mutual labels:  image-compression, image-optimization
Imgbot
An Azure Function solution to crawl through all of your image files in GitHub and losslessly compress them. This will make the file size go down, but leave the dimensions and quality untouched. Once it's done, ImgBot will open a pull request for you to review and merge. [email protected]
Stars: ✭ 1,017 (+2160%)
Mutual labels:  image-compression, image-optimization
zImageOptimizer
Simple image optimizer for JPEG, PNG and GIF images on Linux, MacOS and FreeBSD.
Stars: ✭ 108 (+140%)
Mutual labels:  image-compression, image-optimization

Ultra Image Server

www/noimage_thumb.jpg

🌐 A production grade on-the-fly image processing server setup using imgproxy and Nginx caching.

Table of contents


docker-imgproxy

Support my activities

If you like this repository, please hit the star button to follow further updates, or buy me a coffee πŸ˜‰.

Donate via PayPal Become a sponsor Become a stargazer Report an issue

I really appreciate your love and supports.


Why it's good

  • Just download this project and run (*).
  • Fast on-the-fly generating thumbnails thanks to imgproxy.
  • High availability and performance thanks to Nginx content caching.
  • Forget troubles of Image URL signature, but still ensure the security of the service.
  • Easily serve local files as well as from other origin servers.
  • Flexible in customizing SEO-friendly URLs without exposing imgproxy options (which are long and hard to remember).
  • Flexible in selecting origin server for the request (**).
  • Flexible in creating your own image presets for any request.
  • Flexible in serving fallback image when the source file is not available.
  • Easy SSL configuration (with your own certificates) with built-in HTTP/2 support.
  • Automatically detect and convert images to WebP or AVIF format if the browser supports.

(*) using Docker Compose. (**) which is supported by imgproxy.


Getting started

Prepare your files

Please put your files to be served in the folder www/.

There are some sample files available (a sample image cacti.jpg, a watermark, and some fallback images).

Start the server

docker-compose up -d --build --remove-orphans --force-recreate

That's all! πŸ˜‰

Note: This setup requires Docker Compose installed. If you have already installed Docker Desktop or Docker Toolbox, then no need of installation for Docker Compose.

If you like this setup, please support my works πŸ˜‰.


Examples

Local images

Assuming that we want to generate various thumbnail images from the original file named cacti.jpg.

I already created some preset names, such as _thumb or _w200, and I add preset names to the original URL to get thumbnails from it.

Image with no preset (it is resized to max-width=1600 as default).
http://localhost/cacti.jpg

The image with preset _w200 applied (200 is a dynamic number).
http://localhost/_w200/cacti.jpg

The image with preset _blurry applied.
http://localhost/_blurry/cacti.jpg

The image with preset _small applied.
http://localhost/_small/cacti.jpg

The image with preset _medium applied.
http://localhost/_medium/cacti.jpg

The image with preset _thumb applied.
http://localhost/_thumb/cacti.jpg

The image with preset _square applied.
http://localhost/_square/cacti.jpg

See my configurations to know how it works.

Remote images

With the same presets as above examples, we are going to serve an image by NASA using the alias @nasa, that will be added in these URLs.

Note: the image source is from NASA, it may be unavailable in the future.

Image with no preset (it is resized to max-width=1600 as default).
http://localhost/@nasa/esp_040663_1415.jpg

The image with preset _w200 applied (200 is a dynamic number).
http://localhost/@nasa/_w200/esp_040663_1415.jpg

The image with preset _blurry applied.
http://localhost/@nasa/_blurry/esp_040663_1415.jpg

The image with preset _small applied.
http://localhost/@nasa/_small/esp_040663_1415.jpg

The image with preset _medium applied.
http://localhost/@nasa/_medium/esp_040663_1415.jpg

The image with preset _thumb applied.
http://localhost/@nasa/_thumb/esp_040663_1415.jpg

The image with preset _square applied.
http://localhost/@nasa/_square/esp_040663_1415.jpg

Supported origin servers

This setup serve images from other public origin servers, as well as from Amazon S3 buckets, Google Cloud and Azure Blob.

You can learn how to serve files from private storage in the configurations section.


Debugging

Debugging rewrite rule:

When you make a request with the query component debug=1, you will see an X-Debug header contains its internal imgproxy's options.

Example 1 (local file with preset _small):

curl -Isk 'http://localhost/_small/cacti.jpg?debug=1'

HTTP/1.1 200 OK
Server: nginx
Content-Type: image/webp
X-Debug: /unsafe/size:320:320:0:0/sharpen:0.3/preset:logo/plain/local:///cacti.jpg@webp

Example 2 (remote file with preset _w640):

curl -Isk 'http://localhost/@nasa/_w640/esp_040663_1415.jpg?debug=1'

HTTP/1.1 200 OK
Server: nginx
Content-Type: image/webp
X-Debug: /unsafe/size:640:0:0:0/preset:logo/plain/https://www.nasa.gov/sites/default/files/thumbnails/image/esp_040663_1415.jpg@webp

Debugging fallback image:

When you requested a file which is not available, a fallback image will be served. There will be a 404 response with a header called X-Fallback, which contains the fallback image's path.

curl -Isk 'http://localhost/i-am-a-teacup.jpg'

HTTP/1.1 404 Not Found
Server: nginx
Content-Type: image/jpeg
X-Fallback: /noimage.jpg

Other helper URL parameters:

We can use these URL query components to modify some requests.

skip=1
Skip imgproxy processing for current request. The original file will be served.
E.g. http://localhost/cacti.jpg?skip=1

nocache=1
Disable Nginx caching for current request. The response will not be saved to a cache.
E.g. http://localhost/cacti.jpg?nocache=1

bypass=1
By pass Nginx caching for current request. The response will not be taken from a cache.
E.g. http://localhost/cacti.jpg?bypass=1

If you like this setup, please support my works πŸ˜‰.


Configurations

Enabling SSL (and HTTP/2)

Create a folder certs/ in the same place with the docker-compose.yml file, then rename and put your SSL certificates server.crt and server.key to that certs/ folder.

Open the file at nginx/nginx.conf and uncomment 4 lines right after the # SSL line, like this:

# SSL
listen              443 ssl http2 reuseport;
listen              [::]:443 ssl http2 reuseport;
ssl_certificate     /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server.key;

Then run the command in the Start the server section to recreate and restart the service.

Serving files from private storage

Please uncomment settings in docker-compose.yml file to enable serving files from Amazon S3 buckets, Google Cloud or Azure Blob. Then run the command in the Start the server section to recreate and restart the service.

You can find more details on imgproxy documentation.

Flushing cache files

Just remove the folder cache/.

rm -rf cache/

Then run the command in the Start the server section to restart the service.

Advanced settings

All settings for handling image URLs are written in the imgproxy.conf file using Nginx's map directives.

I keep all configurations in very simple variables. You can also make your own version from this base.

$use_imgproxy
This flag indicates that the request will be proceeded by imgproxy.

map $uri $use_imgproxy
{
    default 0;

    # Add any rules that you want to skip image processing.
    #> E.g. this line excludes files under "hq-cactus" folder.
    ~^/hq-cactus/ 0;

    # Else, process all image files with these file extensions
    ~*\.(jpe?g|png|gif|tiff?|bmp)$  1;
}

$origin_server
Define origin base URL from the request. This setup assumes that an origin server starts with an @ symbol (such as @nasa, @pinterest, etc.). You can also add your own origin servers using regular expressions.

map $uri $origin_server
{
    default         'local://';

    # Put your rewrite rules for origin servers from here.
    #> E.g.
    ~^/@mybucket/   's3://my-bucket';
    ~^/@myhost/     'http://myhost.com';
    ~^/@nasa/       'https://www.nasa.gov/sites/default/files/thumbnails/image';
    ~^/@pinterest/  'https://i.pinimg.com/originals';
}

$origin_uri
Parse real origin URI of the file. This setup just omits origin server and preset name in the URI if they exist, but you can also rewrite requested URI using regular expressions.

map $uri_omitted_origin_preset $origin_uri
{
    default '$uri_omitted_origin_preset';

    # Put your rewrite rules for origin URI from here.
    #> E.g. this line rewrites cactus.jpg to the real path cacti.jpg.
    ~*^/cactus\.jpe?g$  '/cacti.jpg';
}

$preset_name
Parse preset name from requested URI. This setup assumes that preset name starts with an underscore (_) symbol (such as _thumb or _w200). You can define your own presets using regular expressions.

map $uri_omitted_origin $preset_name
{
    default '';

    # You can define dynamic presets,
    #> but beware that dynamic presets are able to cause a denial-of-service attack
    #> by allowing an attacker to request multiple different image resizes.
    #> E.g. a dynamic preset with a variable $width.
    # ~^/_w(?<parsed_width>[0-9_-]+)/  'max_w:${parsed_width}';

    # This is a better version for above dynamic preset.
    #> It allows only certain image sizes,
    #> and fallbacks other undefined image sizes to max_w:200
    ~^/_w(?<parsed_width>(200|640|800|1200|1600))/  'max_w:${parsed_width}';
    ~^/_w(?<parsed_width>([0-9_-]+))/               'max_w:200';

    # Get static preset name from the URI
    ~^/_(?<parsed_name>[a-z0-9_-]+)/ '$parsed_name';
}

$imgproxy_preset
Define imgproxy options for each preset name.

map $preset_name $imgproxy_preset
{
    default 'size:1600:0:0:0/preset:logo';

    # Dynamic preset
    ~^max_w:(?<width>[0-9]+)$ 'size:${width}:0:0:0/preset:logo';

    # Static presets
    blurry  'size:320:320:1:0/blur:10/quality:50';
    small   'size:320:320:0:0/sharpen:0.3/preset:logo';
    medium  'size:640:640:0:0/preset:logo';
    thumb   'size:160:160:1:1/bg:ffffff/resizing_type:fill/sharpen:0.3';
    square  'size:500:500:0:1/bg:ffffff/resizing_type:fill/preset:center_logo';
}

$imgproxy_extension
Detect WebP or AVIF supports from the request header Accept.

map $http_accept $imgproxy_extension
{
    default '';
    ~*webp  '@webp';
    ~*avif  '@avif';
}

$imgproxy_options
Generate final URL for imgproxy following their documentation. When URL query ?skip=1 is set, use another rule to skip imgproxy processing.

map $arg_skip $imgproxy_options
{
    default '/unsafe/${imgproxy_preset}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
    ~.+     '/unsafe/plain/${origin_server}${origin_uri}';
}

$fallback_uri
Define fallback file to serve when the requested file is unavailable. E.g. /noimage.jpg or /noimage_thumb.jpg, which is stored in the folder www/.

map $preset_name $fallback_uri
{
    default '/noimage.jpg';
    thumb   '/noimage_thumb.jpg';
    # small   '/noimage_small.jpg';
    # medium  '/noimage_medium.jpg';
    # square  '/noimage_square.jpg';
}

I think you can even make a better version than mine 😊.


If you like this project, please support my works πŸ˜‰.

From Vietnam πŸ‡»πŸ‡³ with love.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].