First let us see the code in operation:

$ ./thumbnail.lua *jpg
thumb-aurobindo-ghose.jpg
thumb-brahma_vishnu_shiva.jpg
thumb-durga_hanuman_skanda.jpg
thumb-hanuman.jpg

$ qiv thumb-aurobindo-ghose.jpg

shows his photograph scaled down to 200x167.

It is also confirmed by the ImageMagick identify(1) command:

$ identify thumb-aurobindo-ghose.jpg
thumb-aurobindo-ghose.jpg JPEG 200x167 200x167+0+0 DirectClass 8-bit 5.97656kb


Now for the program:

-----------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env lua
-- gd thumbnail
-- Create a jpg thumbnail from gif/jpg/png image
-- (C) 2007 by Alex Kloss [http://www.it-rfc.de]
-- licensed under the terms of the LGPL2:
http://www.fsf.org/licensing/licenses/lgpl.html

-- uses lua-gd: http://lua-gd.luaforge.net
require('gd')

-- thumbnail(imagein,imageout,maxsize) imageout and maxsize are optional
-- returns the output image name on success, nil on failure
function thumbnail(imagein,imageout,maxsize)
        -- sanitizing input options
        local maxsize = maxsize or 200
        local imageout = imageout or "thumb-"..imagein
        -- loading input image
        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))
        -- getting the size right
        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
        -- creating the thumbnail
        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

-- command line if not called as library
if (arg ~= nil) then
        for n,i in ipairs(arg) do
                print(thumbnail(i) or "thumbnail creation failed")
        end
end
----------------------------------------------------------------------------------------------------

It is not too long. Let us see.

#!/usr/bin/env lua
-- gd thumbnail
-- Create a jpg thumbnail from gif/jpg/png image
-- (C) 2007 by Alex Kloss [http://www.it-rfc.de]
-- licensed under the terms of the LGPL2:
http://www.fsf.org/licensing/licenses/lgpl.html

-- uses lua-gd: http://lua-gd.luaforge.net
require('gd')

<comment> The first line is the shebang line to invoke the lua interpreter. NBD.

<comment> The rest of the lines are comments/copyright notices
<comment> the require('gd') line is critical. It imports the cool GD library.

<comment> This means that you have to install lua GD binding module
for this to work. That
<comment> may not be easy but you can try. Or you can download,
compile and install...

-- thumbnail(imagein,imageout,maxsize) imageout and maxsize are optional
-- returns the output image name on success, nil on failure
function thumbnail(imagein,imageout,maxsize)
        -- sanitizing input options
        local maxsize = maxsize or 200
        local imageout = imageout or "thumb-"..imagein
        -- loading input image
        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))

<comment> More comments
<comment> We start a function called thumbnail with 3 arguments
imagein, imageout and maxsize
<comment> Like all scripting languages we care two hoots about
datatypes. lua is weakly typed.

<comment> another comment and then we have the same construct we saw yesterday
<comment> local maxsize = maxsize or 200 is set to a default value of
200 in case overridden explicitly
<comment> local imageout = imageout or "thumb-" .. imagein is
interesting. We construct the
<comment> the output file name by concatenating "thumb-" string with
the imagein variable. Hence the
<comment> the output you see above.

<comment> local im = (imagein:match('%.jpe?g$') and
gd.createFromJpeg(imagein) or ...

<comment> the entire block does a pattern match on the image string
variable and invokes the appropriate
<comment> gd command to read from png, jpg or gif formats into gd's
internal buffer.


-- getting the size right
        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


<comment> We are doing some processing now. local x,y = im:sizeXY()
<comment> im now contains gd's internal representation of the image,
note the above
<comment> command in the previous block.

<comment> this line assigns two variables x and y to values returned
by the sizeXY() function
<comment> in the gd module. You can assign two or more variables
simultaneously unlike C.

<comment> local tx = y > x and x/y*maxsize or maxsize and the next
line obtains the thumbnail
<comment> X and Y values for scaling down. You could instead have used
the value of 200 pixels
<comment> for both X and Y. This math is done to ensure that we don't
exceed the thumbnail size
<comment> as x/y when y is greater than x will be a value less than 1
and multiplying by it will
<comment> will give us a lower value than maxsize = 200. Anyway we are
digressing..

 -- creating the thumbnail
        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



<comment> local tn = gd.createTrueColor(tx,ty)
<comment> What does it do? God alone knows. I guess it creates a new
image in tn variable with
<comment> scaled height and width(X and Y values).

<comment> gd.copyResampled(tn,im,0,0,0,0,tx,ty,x,y)
<comment> This command is also going above my head. im is the input,
tn is the sampled image and
<comment>  I think the 4 0s are the 4 corners of the rectangle for
offsets.I dunno...need to check the gd
<comment> documentation. I am sure we will have equivalent commands in
C or other bindings.

<comment> if tn:jpeg(imageout,75) Ah. I understand this one. The
output is always a JPEG image with
 <comments> quality 75 even if the input is gif or png. This command
prints the image to the imageout
<comments> file on the file system.

      else return nil end
end
<comments> Otherwise return null and end of function.

-- command line if not called as library
if (arg ~= nil) then
        for n,i in ipairs(arg) do
                print(thumbnail(i) or "thumbnail creation failed")
        end
end

<comment> This is cool stuff. We are checking for the command line
arguments stored in arg[1] to arg[n].
<comment> You are doing a pattern match against 'nil' in if (arg ~=
nil) to ensure that an at least one argument
<comment> is given.

<comment> Then for n,i in ipairs(arg) do will assign the array index
to n and array value to i
<comment> print(thumbnail(i)) prints the filename of the thumbnail to
STDOUT. If you remove the print
<comment> and say thumbnail(i) , then the thumbnails will be created
silently without printing
<comment> anything to STDOUT.

Phew! This was too much. Right?

-Girish
-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com
_______________________________________________
To unsubscribe, email [email protected] with 
"unsubscribe <password> <address>"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to