• Stars
    star
    1,043
  • Rank 44,174 (Top 0.9 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

Read/write access to PNG images in pure Ruby.

ChunkyPNG

This library can read and write PNG files. It is written in pure Ruby for maximum portability. Let me rephrase: it does NOT require RMagick or any other memory leaking image library.

Features

  • Decodes any image that the PNG standard allows. This includes all standard color modes, all bit depths, all transparency, and interlacing and filtering options.
  • Encodes images supports all color modes (true color, grayscale, and indexed) and transparency for all these color modes. The best color mode will be chosen automatically, based on the amount of used colors.
  • R/W access to the image's pixels.
  • R/W access to all image metadata that is stored in chunks.
  • Memory efficient (uses a Fixnum, i.e. 4 or 8 bytes of memory per pixel, depending on the hardware)
  • Reasonably fast for Ruby standards, by only using integer math and a highly optimized saving routine.
  • Works on every currently supported Ruby version (2.5+)
  • Interoperability with RMagick if you really have to.

Also, have a look at OilyPNG which is a mixin module that implements some of the ChunkyPNG algorithms in C, which provides a massive speed boost to encoding and decoding.

Usage

require 'chunky_png'

# Creating an image from scratch, save as an interlaced PNG
png = ChunkyPNG::Image.new(16, 16, ChunkyPNG::Color::TRANSPARENT)
png[1,1] = ChunkyPNG::Color.rgba(10, 20, 30, 128)
png[2,1] = ChunkyPNG::Color('black @ 0.5')
png.save('filename.png', :interlace => true)

# Compose images using alpha blending.
avatar = ChunkyPNG::Image.from_file('avatar.png')
badge  = ChunkyPNG::Image.from_file('no_ie_badge.png')
avatar.compose!(badge, 10, 10)
avatar.save('composited.png', :fast_rgba) # Force the fast saving routine.

# Accessing metadata
image = ChunkyPNG::Image.from_file('with_metadata.png')
puts image.metadata['Title']
image.metadata['Author'] = 'Willem van Bergen'
image.save('with_metadata.png') # Overwrite file

# Low level access to PNG chunks
png_stream = ChunkyPNG::Datastream.from_file('filename.png')
png_stream.each_chunk { |chunk| p chunk.type }

Also check out the screencast on the ChunkyPNG homepage by John Davison, which illustrates basic usage of the library on the ChunkyPNG website.

For more information, see the project wiki or the RDOC documentation.

Security warning

ChunkyPNG is vulnerable to decompression bombs, which means that ChunkyPNG is vulnerable to DOS attacks by running out of memory when loading a specifically crafted PNG file. Because of the pure-Ruby nature of the library it is very hard to fix this problem in the library itself.

In order to safely deal with untrusted images, you should make sure to do the image processing using ChunkyPNG in a separate process, e.g. by using fork or a background processing library.

About

The library is written by Willem van Bergen for Floorplanner.com, and released under the MIT license (see LICENSE). Please contact me for questions or remarks.

I generally consider this library to be feature complete. I will gladly accept patches to fix bugs and improve performance, but I will generally be hesitant to accept new features or API endpoints. Before contributing, please read CONTRIBUTING.rdoc that explains this in more detail.

Please check out CHANGELOG.rdoc to see what changed in all versions.

P.S.: The name of this library is intentionally similar to Chunky Bacon and Chunky GIF. Use Google if you want to know why. :-)

More Repositories

1

request-log-analyzer

Create reports based on your log files. Supports Rails, Apache, MySQL, Delayed::Job, and other formats.
Ruby
2,245
star
2

kafka

Load-balancing, resuming Kafka consumer for go, backed by Zookeeper.
Go
373
star
3

state_machine-audit_trail

Log transitions on a state machine to support business process analytics. Superseded by
Ruby
277
star
4

scoped_search

Easily search you ActiveRecord models with a simple query language that converts to SQL.
Ruby
265
star
5

oily_png

Native mixin to speed up ChunkyPNG.
C
175
star
6

adyen

Package to simplify including the Adyen payments services into a Ruby application.
Ruby
132
star
7

kazoo-go

Go library to access Kafka metadata in Zookeeper
Go
100
star
8

vertica

Ruby adapter for Vertica databases
Ruby
79
star
9

active_olap

OLAP extensions for ActiveRecord
Ruby
55
star
10

node-vertica

Pure javascript Vertica database client. Except it is written in CoffeeScript.
CoffeeScript
44
star
11

http_status_exceptions

A Rails plugin to use exceptions for generating HTTP status responses.
Ruby
28
star
12

activerecord-databasevalidations

Add validations to your ActiveRecord models based on your database constraints.
Ruby
26
star
13

clieop

A pure Ruby implementation for writing CLIEOP files to submit payments or direct charges to Dutch banks.
Ruby
14
star
14

sql_tree

Outdated, replaced by a much more complete SQL toolkit: https://github.com/wvanbergen/sql_toolkit
Ruby
14
star
15

kafka-consumer

High-level Ruby consumer for Kafka.
Ruby
12
star
16

ottawa-trash

This is for hoping that Ottawa's Garbage Collection works out better than Ruby's.
Ruby
6
star
17

schema_registry

Ruby client for Confluent Inc.'s schema-registry
Ruby
6
star
18

uuml

Instant Germanification of your web app!
Ruby
6
star
19

SysVIPC

Fork of the SysVIPC gem to fix packaging issues
C
6
star
20

dropbox-sync

Tool to synchronize folders between machiens using Dropbox and symbolic links.
Ruby
6
star
21

selekt

Toolkit to parse an manipulate SQL queries for testing and monitoring purposes.
Ruby
5
star
22

chunky_bits

Collection of image processing algorithms built on top of ChunkyPNG
Ruby
5
star
23

kazoo

Ruby library to access Kafka metadata in Zookeeper
Ruby
4
star
24

blogposts

Staging area for my blogposts
4
star
25

newrelic-vertica

Enable SQL monitoring in NewRelic RPM for the Vertica driver
Ruby
3
star
26

love

Ruby library to access the Tender REST API
Ruby
3
star
27

HackReduce2

3
star
28

tros

Cleaned up AVRO implementation
Ruby
2
star
29

guillaume-consulting

Guillaume Consulting website
Ruby
2
star
30

dreamcatcher

Simple gem to catch exceptions in Ruby code and send notifications.
Ruby
2
star
31

scoped_search-h2

H2 database support for scoped_search
Ruby
1
star
32

scoped_search-oracle_enhanced

Oracle enhanced support for scoped_search
Ruby
1
star