leafo / imagesize

Detect size & format of image file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

imagesize

test

Detect the size of common image formats while trying to read as few bytes as possible. Byte strings are parsed using LPeg to extract headers from image formats

Supported types:

  • PNG — Typically needs at least 25 bytes
  • JPEG — Typically needs around 160 for optimized images, upwards to around 1k or more with EXIF, etc.
  • GIF — Reads with/height from first image descriptor. Typically needs around 700+ bytes, depends on how palette is stored

This library will only read the head of the string to identify the format and dimensions, then bail out. It will not verify that the file contains a valid image outside of that.

LPeg does not support reading from a stream so you will have to append a buffer into a string yourself and sequentially test it.

Install

luarocks install https://raw.githubusercontent.com/leafo/imagesize/main/imagesize-dev-1.rockspec

Usage

detect_image_from_bytes(byte_string)

Attempts to detect the type and dimensions of an image from the bytes passed in. It's not necessary to pass the whole image, you can pass any amount of bytes and it will attempt to read the front of the file up until where it can find the dimensions.

On success, two values are returned: the type of the image (png, jpeg, gif), and a table containing at least a width and height field.

On error, nil and an error message is returned.

local imagesize = require("imagesize")

-- this will attempt to detect the image from 200 bytes, but you're also
-- welcome to pass the whole file
local bytes = file.open("some_image.png"):read(200)

local kind, dimensions = imagesize.detect_image_from_bytes(bytes)

-- kind --> png
-- dimensions --> { width = 25, height = 100, depth = 8 }

About

Detect size & format of image file


Languages

Language:MoonScript 68.4%Language:Lua 30.7%Language:Makefile 0.9%