• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    C
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby wrapper for libwebp

Webp-ffi

Build Status Code Climate

Ruby wrapper for libwebp. What is WebP?

WebP is a new image format that provides lossless and lossy compression for images on the web. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller in size compared to JPEG images at equivalent SSIM index. WebP supports lossless transparency (also known as alpha channel) with just 22% additional bytes. Transparency is also supported with lossy compression and typically provides 3x smaller file sizes compared to PNG when lossy compression is acceptable for the red/green/blue color channels.

WebP Gallery

Installation

Requirements

First of all you should have install libraries: libpng, libjpeg and libtiff.

For Ubuntu, Debian:

sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libwebp-dev

For Fedora, CentOS:

sudo dnf install libjpeg-devel libpng-devel libtiff-devel libwebp-devel

For Mac OS:

brew install libjpg libpng libtiff webp

or (for MacPorts):

sudo port install jpeg libpng tiff

Next, you should install libwebp (if you didn't install it by brew in Mac OS or by apt-get in Ubuntu or Debian). Webp library version should be >= 0.3.0. This gem is not support Windows systems.

Final part

Add this line to your application's Gemfile:

gem 'webp-ffi'

And then execute:

$ bundle

Or install it yourself as:

$ gem install webp-ffi

Usage

Encoder end Decoder versions

Basic info about libwebp (encoder and decoder versions):

$ irb
2.0.0p0 :001 > require 'webp_ffi'
 => true
2.0.0p0 :002 > WebP.decoder_version
 => "0.3.1"
2.0.0p0 :003 > WebP.encoder_version
 => "0.3.1"

WebP size (width and height)

Get size (width and height) from webp image:

filename = File.expand_path(File.join(File.dirname(__FILE__), "spec/factories/4.webp"))
WebP.webp_size(File.open(filename, "rb").read)
 => [2000, 2353]

Encode WebP image

Encode png, jpg or tiff image to webp:

filename = File.expand_path(File.join(File.dirname(__FILE__), "spec/factories/4.png"))
out_filename = File.expand_path(File.join(File.dirname(__FILE__), "tmp/4.webp"))
WebP.encode(filename, out_filename)

Encode png, jpg or tiff image to webp with options:

WebP.encode(filename, out_filename, quality: 50, resize_w: 100, resize_h: 200)
WebP.encode(filename, out_filename, quality: 75, crop_x: 0, cropt_y: 0, crop_w: 100, crop_h: 100)

Possible encode options:

  • lossless (int) - Lossless encoding (0=lossy(default), 1=lossless)
  • near_lossless (int) - Use near-lossless image preprocessing (0=maximum preprocessing, 100=no preprocessing(default))
  • quality (float) - between 0 (smallest file) and 100 (biggest)
  • method (int) - quality/speed trade-off (0=fast, 6=slower-better)
  • target_size (int) - if non-zero, set the desired target size in bytes. Takes precedence over the 'compression' parameter
  • target_PSNR (float) - if non-zero, specifies the minimal distortion to try to achieve. Takes precedence over target_size
  • segments (int) - maximum number of segments to use, in [1..4]
  • sns_strength (int) - Spatial Noise Shaping. 0=off, 100=maximum
  • filter_strength (int) - range: [0 = off .. 100 = strongest]
  • filter_sharpness (int) - range: [0 = off .. 7 = least sharp]
  • filter_type (int) - filtering type: 0 = simple, 1 = strong (only used if filter_strength > 0 or autofilter > 0)
  • autofilter (int) - Auto adjust filter's strength [0 = off, 1 = on]
  • alpha_compression (int) - Algorithm for encoding the alpha plane (0 = none, 1 = compressed with WebP lossless). Default is 1
  • alpha_filtering (int) - Predictive filtering method for alpha plane. 0: none, 1: fast, 2: best. Default if 1
  • alpha_quality (int) - Between 0 (smallest size) and 100 (lossless). Default is 100
  • pass (int) - number of entropy-analysis passes (in [1..10])
  • show_compressed (int) - if true, export the compressed picture back. In-loop filtering is not applied
  • preprocessing (int) - preprocessing filter (0=none, 1=segment-smooth)
  • partitions (int) - log2(number of token partitions) in [0..3]. Default is set to 0 for easier progressive decoding
  • partition_limit (int) - quality degradation allowed to fit the 512k limit on prediction modes coding (0: no degradation, 100: maximum possible degradation)
  • width (int), height (int) - Input size (width x height) for YUV
  • crop_x (int), crop_y (int), crop_w (int), crop_h (int) - crop picture with the given rectangle
  • resize_w (int), resize_h (int) - resize picture (after any cropping)

Decode WebP image

Decode webp image (default format is png):

filename = File.expand_path(File.join(File.dirname(__FILE__), "spec/factories/4.webp"))
out_filename = File.expand_path(File.join(File.dirname(__FILE__), "tmp/4.png"))
WebP.decode(filename, out_filename)

Decode webp image to pam, ppm, pgm, bmp, tiff or yuv format of image:

filename = File.expand_path(File.join(File.dirname(__FILE__), "spec/factories/4.webp"))
out_filename = File.expand_path(File.join(File.dirname(__FILE__), "tmp/4.png"))
WebP.decode(filename, out_filename, output_format: :pam)
WebP.decode(filename, out_filename, output_format: :ppm)
WebP.decode(filename, out_filename, output_format: :pgm)
WebP.decode(filename, out_filename, output_format: :bmp)
WebP.decode(filename, out_filename, output_format: :tiff)
WebP.decode(filename, out_filename, output_format: :yuv)

Decode webp image with options:

WebP.encode(filename, out_filename, resize_w: 100, resize_h: 200)
WebP.encode(filename, out_filename, crop_x: 0, cropt_y: 0, crop_w: 100, crop_h: 100)

Possible decode options:

  • bypass_filtering (bool) - disable in-loop filtering
  • no_fancy_upsampling (bool) - don't use the fancy YUV420 upscaler
  • use_threads (bool) - use multi-threading
  • crop_x (int), crop_y (int), crop_w (int), crop_h (int) - crop picture with the given rectangle
  • resize_w (int), resize_h (int) - resize picture (after any cropping)

Rails assets pipeline integration

For integration with Rails 3+ you can use very simple rake task:

# Place this code in lib/tasks/assets.rake
require 'webp-ffi'

namespace :assets do
  desc "Create .webp versions of assets"
  task :webp => :environment do
    image_types = /\.(?:png|jpe?g)$/

    public_assets = File.join(
      Rails.root,
      "public",
      Rails.application.config.assets.prefix)

    Dir["#{public_assets}/**/*"].each do |filename|
      next unless filename =~ image_types

      mtime = File.mtime(filename)
      webp_file = "#{filename}.webp"
      next if File.exist?(webp_file) && File.mtime(webp_file) >= mtime
      begin
        WebP.encode(filename, webp_file)
        File.utime(mtime, mtime, webp_file)
        puts "Webp converted image #{webp_file}"
      rescue => e
        puts "Webp convertion error of image #{webp_file}. Error info: #{e.message}"
      end
    end
  end

  # Hook into existing assets:precompile task
  Rake::Task["assets:precompile"].enhance do
    Rake::Task["assets:webp"].invoke
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

More Repositories

1

pgtune

Pgtune - tuning PostgreSQL config by your hardware
JavaScript
1,826
star
2

postgresql_book

Book about PostgreSQL (russian)
TeX
798
star
3

sql-joins-app

SQL JOINS visualizer
Svelte
252
star
4

mongodb_logger

MongoDB logger for Rails
Ruby
141
star
5

chef_book

Cooking Infrastructure by Chef
TeX
122
star
6

chef-solo-example

Repo with Chef solo example
Ruby
92
star
7

elixir_v8

V8 engine for Elixir with pools
Elixir
67
star
8

rwbox

Vagrant images for Ruby and ROR developers
Ruby
45
star
9

cable-shared-worker

ActionCable and AnyCable Shared Worker support
JavaScript
40
star
10

vmail

VMail - check the markup (HTML, CSS) of HTML email template compatibility with email clients
HTML
23
star
11

webp-wasm

Webp image convertor (webassembly, works offline in browser)
JavaScript
23
star
12

redis_pool

Redis pool for Elixir lang
Elixir
21
star
13

st_rails_example

Demo app for shared templates in rails 3
Ruby
20
star
14

storybook-addon-root-attribute

Storybook addon, which provide ability to change html, body or some element attribute
JavaScript
15
star
15

omniauth-yammer

OmniAuth strategy for Yammer
Ruby
14
star
16

zopfli-ffi

Ruby wrapper for zopfli
Ruby
10
star
17

chef-server-example

Chef server example
Ruby
7
star
18

PsyRadioIOS

PsyRadio app for iOS
Objective-C
6
star
19

my_little_nosql

My Little NoSQL
5
star
20

chef-tdd-monit

Example cookbook with monit
Ruby
5
star
21

le0pard.github.io

My Blog
Sass
5
star
22

sidekiq-crond

Scheduler / Cron for Sidekiq jobs
Ruby
5
star
23

Symfony_films

Films CMS writen on Symfony
PHP
4
star
24

LibreOfficeCSVGenerator

Fill the documents by LibreOffice API and CSV
Java
4
star
25

cv

My Curriculum Vitae
TeX
3
star
26

mpw.js

MPW.js is a JavaScript implementation of the Master Password App algorithm
JavaScript
3
star
27

PsyRadioAndroid

PsyRadio app for Android
Java
3
star
28

ShakeForMute

ShakeForMute for android
Java
3
star
29

mongodb_logger_example_heroku

Example app with mongodb_logger on heroku
CSS
3
star
30

pg_web

PostgreSQL extension which provide web interface for database
C
2
star
31

smartme_backbone_2012

SmartMe Backbone 2012
Ruby
2
star
32

android_coocoo_afisha

Android coocoorooza app
Java
2
star
33

js-api-service

Elixir Web API to execute JavaScript code
CSS
2
star
34

hatakon_16

hakaton android
Objective-C
1
star
35

monkey_pivotal_updater

Monkey update pivotal google docs
JavaScript
1
star
36

catware

catware site
JavaScript
1
star
37

le0pard

Intro
1
star
38

RWProductManager-Android

RWProductManager for android
Java
1
star
39

restauranteers_iphone

Objective-C
1
star
40

browserlist

Browserlist Wasm - display compatible browsers from browserslist string
Svelte
1
star
41

RWProductManager-iOS

RWProductManager
JavaScript
1
star
42

storybook-addon-root-attribute-demo

Demo for storybook-addon-root-attribute
JavaScript
1
star
43

popcornua_web

popcornua web app
Ruby
1
star
44

presentations

my presentations
HTML
1
star
45

Comics-CMS

Comics CMS writen on Ruby (use Sinatra, DataMapper, HAML and SASS)
Ruby
1
star
46

diploma_program_test

diploma_program_test
Ruby
1
star
47

diploma_fileshare

diploma_fileshare
Ruby
1
star
48

iphone_popcornua

popcornua for iphone
Objective-C
1
star
49

whatiswrongwith

whatiswrongwith
Ruby
1
star
50

android_cat_simple

Java
1
star
51

monkey_notification

Monkey Notification Gem
Ruby
1
star