struct ImageSize

Defined in:

image_size.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.get(bytes : Bytes) : ImageSize #

Gets image size from binary data.

file = File.new "test.png"
bytes = Bytes.new file.size
file.read bytes
file.close
size = ImageSize.get bytes
pp size.width
pp size.height

[View source]
def self.get(filename : String) : ImageSize #

Gets image size from filename.

size = ImageSize.get "test.png"
pp size.width
pp size.height

[View source]

Class Method Detail

def self.resize(bytes : Bytes, *, width = 0, height = 0) : Bytes #

Resizes an image from binary data.

NOTE At least one of the named arguments must be provided.

When only #width is set, the #height will be automatically calculated to keep the aspect ratio.

bytes = ImageSize.resize data, width: 1024
File.write "1024.png", bytes

When only #height is set, the #width will be automatically calculated to keep the aspect ratio.

bytes = ImageSize.resize data, height: 256
File.write "256.png", bytes

Seting both #width and #height will break the aspect ratio.

bytes = ImageSize.resize data, width: 1024, height: 1024
File.write "1024-1024.png", bytes

[View source]
def self.resize(filename : String, *, width = 0, height = 0) : Bytes #

Same as self.resize but reads the image from a filename.


[View source]

Instance Method Detail

def height : Int32 #

[View source]
def width : Int32 #

[View source]