Gd 缩略图

lua-users home
wiki

以下代码描述了一个使用 lua-gd 的小型 JPEG/PNG/GIF 缩略图调整器,可用于网页画廊或类似应用程序 - 它可以作为命令行程序或库使用

#!/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 40
	local imageout = imageout or "t"..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
Vega++
最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2013 年 5 月 23 日下午 1:04 GMT (差异)