Module:ImageDimensions

From Soyjak Wiki, the free ensoyclopedia
Jump to navigationJump to search

local p = {}
	
local function getFile(frame)
	local filename = frame.args[1]
		or frame:getParent() and frame:getParent().args[1]

	if not filename or filename == '' then
		return nil
	end

	if not filename:match('^File:') then
		filename = 'File:' .. filename
	end

	local title = mw.title.new(filename)
	if not title or not title.file then
		return nil
	end

	return title.file
end

function p.main(frame)
	local file = getFile(frame)
	if not file or not file.width or not file.height then
		return ''
	end

	return string.format('%dx%d', file.width, file.height)
end

function p.width(frame)
	local file = getFile(frame)
	if not file or not file.width then
		return ''
	end

	return file.width
end

function p.height(frame)
	local file = getFile(frame)
	if not file or not file.height then
		return ''
	end

	return file.height
end

function p.sizeKB(frame)
	local file = getFile(frame)
	if not file or not file.size then
		return ''
	end

	local kb = math.floor((file.size / 1024) + 0.5)
	return kb
end

function p.mimetype(frame)
	local file = getFile(frame)
	if not file or not file.mimeType then
		return ''
	end
	return file.mimeType
end

function p.size(frame)
	local file = getFile(frame)
	if not file or not file.size then
		return ''
	end
	return file.size
end

return p