Coder Social home page Coder Social logo

imagesize's Introduction

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 }

imagesize's People

Contributors

leafo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.