All Projects → mtgrosser → rszr

mtgrosser / rszr

Licence: MIT license
Fast image resizer for Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language
c
50402 projects - #5 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to rszr

seam-carving
An implementation of the seam carving algorithm, designed to resize images while preventing distortion.
Stars: ✭ 30 (+42.86%)
Mutual labels:  image-resizer, image-resizing
reImage
Fast image resizing backend based on libvips, mozjpeg and pngquant
Stars: ✭ 107 (+409.52%)
Mutual labels:  image-resizer, image-resizing
Fast-Image-Resizer
Javafx application to resize images and add them into folders automatically.
Stars: ✭ 27 (+28.57%)
Mutual labels:  image-resizer
ImageResizer.Plugins.EPiServerBlobReader
EPiServer Blob provider for ImageResizer.Net
Stars: ✭ 14 (-33.33%)
Mutual labels:  image-resizer
conky-for-macOS
macOS port of the Light-weight system monitor for X.
Stars: ✭ 139 (+561.9%)
Mutual labels:  imlib2
tensorflow-bicubic-downsample
tf.image.resize_images has aliasing when downsampling and does not have gradients for bicubic mode. This implementation fixes those problems.
Stars: ✭ 23 (+9.52%)
Mutual labels:  image-resizing
seam-carving
A fast Python implementation of Seam Carving for Content-Aware Image Resizing.
Stars: ✭ 182 (+766.67%)
Mutual labels:  image-resizing
Lilliput
Resize images and animated GIFs in Go
Stars: ✭ 1,690 (+7947.62%)
Mutual labels:  image-resizer
Thumbnailator
Thumbnailator - a thumbnail generation library for Java
Stars: ✭ 3,845 (+18209.52%)
Mutual labels:  image-resizer
oc-imageresize-plugin
October Plugin - Image Resize
Stars: ✭ 13 (-38.1%)
Mutual labels:  image-resizer
oc-imageresizer-plugin
October CMS Plugin to resize and compress images
Stars: ✭ 44 (+109.52%)
Mutual labels:  image-resizer
imgwizard
Simple server for On-the-Fly image processing in Go
Stars: ✭ 51 (+142.86%)
Mutual labels:  image-resizer
Conky
Light-weight system monitor for X.
Stars: ✭ 5,123 (+24295.24%)
Mutual labels:  imlib2
i3lock-next
Create a fancy image to use with i3lock.
Stars: ✭ 64 (+204.76%)
Mutual labels:  imlib2
Bicubic-interpolation
Bicubic interpolation for images (Python)
Stars: ✭ 88 (+319.05%)
Mutual labels:  image-resizing
docker-imgproxy
🌐 An ultra fast, production-grade on-the-fly image processing web server. Designed for high throughput with Nginx caching. Powered by imgproxy.
Stars: ✭ 45 (+114.29%)
Mutual labels:  image-resizing
image-filtering
image filtering techniques in python with examples
Stars: ✭ 19 (-9.52%)
Mutual labels:  image-filtering
active storage-send zip
Create a zip from one or more Active Storage objects
Stars: ✭ 39 (+85.71%)
Mutual labels:  activestorage
active-storage-base64
Base64 support for ActiveStorage
Stars: ✭ 121 (+476.19%)
Mutual labels:  activestorage

Gem Version build

Rszr - fast image resizer for Ruby

Rszr is an image resizer for Ruby based on the Imlib2 library. It is faster and consumes less memory than MiniMagick, GD2 and VIPS, and comes with an optional drop-in interface for Rails ActiveStorage image processing.

Installation

In your Gemfile:

gem 'rszr'

Imlib2

Rszr requires the Imlib2 library to do the heavy lifting.

OS X

Using homebrew:

brew install imlib2

Linux

Using your favourite package manager:

RedHat-based
yum install imlib2 imlib2-devel
Debian-based
apt-get install libimlib2 libimlib2-dev

Usage

# bounding box 400x300
image = Rszr::Image.load('image.jpg')
image.resize(400, 300)

# save it
image.save('resized.jpg')

# save it as PNG
image.save('resized.png')

# save without extension in given format
image.save('resized', format: 'png')

Image info

image.width => 400
image.height => 300
image.dimensions => [400, 300]
image.format => "jpeg"
image.alpha? => false
image[0, 0] => <Rszr::Color::RGBA @red=38, @green=115, @blue=141, @alpha=255>
image[0, 0].to_hex => "#26738dff"
image[0, 0].to_hex(alpha: false) => "#26738d"

Transformations

For each transformation, there is a bang! and non-bang method. The bang method changes the image in place, while the non-bang method creates a copy of the image in memory.

Resizing

# auto height
image.resize(400, :auto)

# auto width
image.resize(:auto, 300)

# scale factor
image.resize(0.5)

# resize to fill
image.resize(400, 300, crop: true)

# resize to fill with gravity
# where gravity in [:n, :nw, :w, :sw, :w, :se, :e, :ne, :center]
image.resize(400, 300, crop: gravity)

# save memory, do not duplicate instance
image.resize!(400, :auto)

Check out the full list and demo of resize options!

Other transformations

# crop
image.crop(200, 200, 100, 100)

# rotate three times 90 deg clockwise
image.turn!(3)

# rotate one time 90 deg counterclockwise
image.turn!(-1)

# rotate by arbitrary angle
image.rotate(45)

# flip vertically
image.flip

# flop horizontally
image.flop

# initialize copy
image.dup

Image generation

# generate new image with transparent background
image = Rszr::Image.new(500, 500, alpha: true, background: Rszr::Color::Transparent)

# fill image with 50% opacity
image.fill!(Rszr::Color::RGBA.new(0, 206, 209, 50))

# define a color gradient
gradient = Rszr::Color::Gradient.new do |g|
  g.point 0, 255, 250, 205, 50
  g.point 0.5, 135, 206, 250
  g.point 1, Rszr::Color::White
end

# draw a rectangle and fill it using the gradient with 45°
image.rectangle!(gradient.to_fill(45), 100, 100, 300, 300)

Colors

# pre-defined colors
Rszr::Color::White
Rszr::Color::Black
Rszr::Color::Transparent

# RGB
color = Rszr::Color.rgba(255, 250, 50)
color.red => 255
color.green => 250
color.blue => 50
color.alpha => 255
color.cyan => 0
color.magenta => 5
color.yellow => 205

# RGBA
Rszr::Color.rgba(255, 250, 50, 255)

# CMY
Rszr::Color.cmya(0, 5, 205)

# CMYA
Rszr::Color.cmya(0, 5, 205, 255)

Color gradients

# three-color linear gradient with changing opacity
gradient = Rszr::Color::Gradient.new do |g|
  g.point 0, 255, 250, 205, 50
  g.point 0.5, 135, 206, 250
  g.point 1, Rszr::Color::White
end

# alternative syntax
gradient = Rszr::Color::Gradient.new(0 => "#fffacd32", 0.5 => "#87cefa", 1 => "#fff")

# generate fill with 45° angle
fill = gradient.to_fill(45)

# use as image background
image = Rszr::Image.new(500, 500, background: fill)

Watermarking and image blending

# load logo
logo = Rszr::Image.load('logo.png')

# load image
image = Rszr::Image.load('image.jpg')

# enable alpha channel
image.alpha = true

# blend it onto the image at position (10, 10)
image.blend!(logo, 10, 10)

# blending modes:
# - copy (default)
# - add
# - subtract
# - reshade
image.blend(logo, 10, 10, mode: :subtract)

Filters

Filters also support bang! and non-bang methods.

# sharpen image by pixel radius
image.sharpen!(1)

# blur image by pixel radius
image.blur!(1)

# brighten
image.brighten(0.1)

# darken
image.brighten(-0.1)

# contrast
image.contrast(0.5)

# gamma
image.gamma(1.1)

Image auto orientation

Auto-rotation is supported for JPEG and TIFF files that include the necessary EXIF metadata.

# load and autorotate
image = Rszr::Image.load('image.jpg', autorotate: true)

To enable autorotation by default:

# auto-rotate by default, for Rails apps put this into an initializer
Rszr.autorotate = true

Creating interlaced PNG and progressive JPEG images

In order to save interlaced PNGs and progressive JPEGs, set the interlace option to true:

image.save('interlaced.png', interlace: true)

Saving progressive JPEG images requires imlib2 >= 1.8.1.

For EL8, there are pre-built RPMs provided by the onrooby repo.

Rails / ActiveStorage interface

Rszr provides a drop-in interface to the image_processing gem. It is faster than both mini_magick and vips and way easier to install than the latter.

# Gemfile
gem 'image_processing'
gem 'rszr'

# config/initializers/rszr.rb
require 'rszr/image_processing'

# config/application.rb
config.active_storage.variant_processor = :rszr

When creating image variants, you can use all of Rszr's transformation methods:

<%= image_tag user.avatar.variant(resize_to_fit: [300, 200]) %>

Loading from and saving to memory

The Imlib2 library is mainly file-oriented and doesn't provide a way of loading the undecoded image from a memory buffer. Therefore, the functionality is implemented on the Ruby side of the gem, writing the memory buffer to a Tempfile. Currently, this local write cannot be avoided.

image = Rszr::Image.load_data(binary_data)

data = image.save_data(format: :jpeg)

Thread safety

As of version 0.5.0, Rszr is thread safe through Ruby's global VM lock. Use of any previous versions in a threaded environment is discouraged.

Speed

Resizing a 1500x997 JPEG image to 800x532, 500 times: Speed

Library Time
MiniMagick 27.0 s
GD2 28.2 s
VIPS 13.6 s
Rszr 7.9 s
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].