
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
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.
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.
Add this line to your application's Gemfile:
gem 'webp-ffi'
And then execute:
$ bundle
Or install it yourself as:
$ gem install webp-ffi
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"
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 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:
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:
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
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that webp-ffi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.