#!/usr/bin/env lua
require('gd')
function thumbnail(imagein,imageout,maxsize)
local maxsize = maxsize or 40
local imageout = imageout or "t"..imagein
local im = (imagein:match('%.jpe?g$') and gd.createFromJpeg(imagein) or
imagein:match('%.png$') and gd.createFromPng(imagein) or
imagein:match('%.gif$') and gd.createFromGif(imagein))
local x,y = im:sizeXY()
local tx = y > x and x/y*maxsize or maxsize
local ty = x > y and y/x*maxsize or maxsize
local tn = gd.createTrueColor(tx,ty)
gd.copyResampled(tn,im,0,0,0,0,tx,ty,x,y)
if tn:jpeg(imageout,75) then return imageout
else return nil end
end
if (arg ~= nil) then
for n,i in ipairs(arg) do
print(thumbnail(i) or "thumbnail creation failed")
end
end