Kurzreferenz

ChunkyPNG Ruby Bildbibliothek Spickzettel

Kurzreferenz für ChunkyPNG, die reine Ruby-PNG-Bibliothek: Laden, Speichern, Pixelmanipulation, Zuschneiden und Canvas-Transformationen.

· Veröffentlicht

ChunkyPNG ist eine reine Ruby-Bibliothek zum Lesen, Schreiben und Bearbeiten von PNG-Bildern ohne externe C-Erweiterungen oder ImageMagick.

Bilder laden (Loading Images)

require 'chunky_png'

# Load directly from filesystem
image = ChunkyPNG::Image.from_file('input.png')

# Load from binary in-memory blob or IO stream
image = ChunkyPNG::Image.from_blob(File.binread('input.png'))
image = ChunkyPNG::Image.from_io(io_stream)

Bilder speichern und exportieren (Saving Images)

# Save to file
image.save('output.png')

# Export to binary string blob
binary_blob = image.to_blob

# Write to stream
File.open('stream.png', 'wb') { |io| image.write(io) }

Pixel- und Formenzeichnung (Pixel Drawing)

# Set individual pixel RGBA values
image[0, 0] = ChunkyPNG::Color.rgba(255, 0, 0, 128)

# Color constructors
red  = ChunkyPNG::Color.from_hex('#ff0000')
cyan = ChunkyPNG::Color.rgba(0, 255, 255, 255)

# Draw geometric primitives
image.line(10, 10, 100, 10, red)
image.rect(20, 20, 80, 80, red, cyan) # border, fill
image.circle(50, 50, 25, red)

Transformationen und Zuschneiden (Transforms)

# Crop sub-rectangle: crop(x, y, width, height)
cropped = image.crop(10, 10, 200, 200)

# Flipping and rotation
transformed = image.flip_horizontally.rotate_right
flipped_v   = image.flip_vertically

Verwandte Themen