Modulo:Immagine

Da Wikivoyage.
Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Immagine/man (modifica · cronologia)
Sandbox: Modulo:Immagine/sandbox (modifica · cronologia) · Test: Modulo:Immagine/test (modifica · cronologia · Esegui)

Modulo che implementa i template {{Altezza immagine}}, {{Larghezza immagine}} e {{Dimensioni immagine}}.


local p = {}
--=====================================================
-- Ritorna le dimensioni dell'immagine (larghezza x altezza)
-- Se queste non sono passate dai parametri tenta di recuperarle
-- dalle informazioni del file immagine, altrimenti ricade su
-- una larghezza pari a 350 e un'altezza pari alla larghezza
--=====================================================

local function get_dimensions(name)
	local page = mw.title.new(name, 'Media')
	width = page.file and page.file.width or 350
	height = page.file and page.file.height or wr
	return width, height
end

--interfacce
function p.get_dimensions( frame )
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local filename = args[1] or args['filename'] or ""
	local width, height = get_dimensions(filename)
	return width .. "x" .. height
end

function p.get_width( frame )
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local filename = args[1] or args['filename'] or ""
	local width, height = get_dimensions(filename)
	local requestedHeight = args[2] or args['height'] or ''
	if requestedHeight ~= '' then
		width = math.floor(width * requestedHeight / height +0.5) --round function do not exist in LUA
	end
	return width
end

function p.get_height( frame )
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	local filename = args[1] or args['filename'] or ""
	local width, height = get_dimensions(filename)
	local requestedWidth = args[2] or args['width'] or ''
	if requestedWidth ~= '' then
		height = math.floor(height * requestedWidth / width +0.5) --round function do not exist in LUA
	end
	return height
end

return p