cheatsheets/chunky_png.md

54 lines
744 B
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: Chunky PNG
2015-11-24 05:02:17 +00:00
category: Ruby libraries
2013-10-14 02:36:58 +00:00
---
2014-06-09 07:12:20 +00:00
### Loading
2013-09-17 10:06:00 +00:00
2017-10-15 06:27:15 +00:00
```ruby
image = ChunkyPNG::Image.from_file('file.png')
```
#### Alternate ways
```ruby
image = ChunkyPNG::Image.from_blob(File.read('file.png'))
image = ChunkyPNG::Image.from_io(io)
```
Loads from `file.png`.
2013-09-17 10:06:00 +00:00
2014-06-09 07:12:20 +00:00
### Saving
2013-09-17 10:06:00 +00:00
2017-10-15 06:27:15 +00:00
```ruby
image.save('filename.png')
```
#### Alternate ways
```ruby
File.open('newfile.png', 'wb') { |io| image.write(io) }
binary_string = image.to_blob
```
Writes an image to `newfile.png`.
2013-09-17 10:06:00 +00:00
2014-06-09 07:12:20 +00:00
### Drawing
2013-09-17 10:06:00 +00:00
2017-10-15 06:27:15 +00:00
```ruby
image[0, 0] = ChunkyPNG::Color.rgba(255, 0,0, 128)
image.line(1, 1, 10, 1, ChunkyPNG::Color.from_hex('#aa007f'))
```
2013-09-17 10:06:00 +00:00
2014-06-09 07:12:20 +00:00
### Canvas
2013-09-17 10:06:00 +00:00
2017-10-15 06:27:15 +00:00
```ruby
crop(x, y, w, h)
```
2013-09-17 10:06:00 +00:00
2014-06-09 07:12:20 +00:00
### Transforms
2013-09-17 10:06:00 +00:00
2017-10-15 06:27:15 +00:00
```ruby
new_image = image.flip_horizontally.rotate_right
```