leafo / magick

Lua bindings to ImageMagick for LuaJIT using FFI
401 stars 79 forks source link

How to implement a hidden pixel #49

Closed CriztianiX closed 3 years ago

CriztianiX commented 7 years ago

Using lapis framework i need to send an image created on-the-fly

local magick = require("magick")
local ngx = ngx
return function(self)
    local str_img = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8z8BQz0AEYBxVSF+FABJADveWkH6oAAAAAElFTkSuQmCC"
    local img = magick.load_image_from_blob(ngx.decode_base64(str_img))
    ngx.header["content-type"] = "image/png"
   // HERE, HOW TO SEND THE  IMAGE TO BROWSER ?
    img:destroy()
end
leafo commented 7 years ago

You can convert the image to a string blob using get_blob, then just write it as the output. You'll also want to set the correct content type.

Keep in mind, if the image is static file, I recommend just serving it from disk using try_files, I don't think it's wroth dynamically creating the image in memory.