Module:ImageInfo
Jump to navigation
Jump to search
Module documentation
This documentation is transcluded from Module:ImageInfo/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:ImageInfo/doc. [edit]
Module:ImageInfo requires Module:Clean image.
Module:ImageInfo is required by Module:Growth stages/sandbox.
Module:ImageInfo is required by Module:Sandbox/User:Aescopalus.
| Function list |
|---|
| L 6 — get_file L 21 — parseImageName L 39 — get_or_default L 56 — ImageInfo:new L 71 — ImageInfo:getWidth L 83 — ImageInfo:getHeight L 91 — ImageInfo:setScale L 98 — ImageInfo:getScale L 102 — ImageInfo:setOverrideWidth L 109 — ImageInfo:hasOverriddenWidth L 113 — ImageInfo:hasFile L 117 — ImageInfo:scaleToImage L 126 — ImageInfo:getScaledWidth L 130 — ImageInfo:getScaledHeight L 134 — ImageInfo:getImageLink L 139 — ImageInfo:getRawImageLink L 148 — ImageInfo._scaleToImage L 154 — ImageInfo._overrideWidth L 160 — ImageInfo._getWidestImage |
local cleanimage = require("Module:Clean image").clean
--
-- Helper function to grab a file object from a file name/link
--
local function get_file(file_name, load_file)
if load_file == true and file_name ~= nil then
local entry = mw.title.makeTitle('File', file_name)
if entry ~= nil and entry.file ~= nil and entry.file.width ~= nil and entry.file.height ~=nil then
return entry.file
end
end
return nil
end
--
-- Allows backwards compatibility for file names that contain either a # for a width parameter
-- And/or do not contain a .png extension
--
local function parseImageName(imageName)
-- Don't parse anything if the parameter doesn't exist
if not string.find(imageName or '', '%S') then
return nil
end
-- [[File:Marigold (stage 1).png|104px]]
if string.find(imageName, '%[%[File:') then
local s, e = string.find(imageName, '.png')
return string.sub(imageName, 8, e)
end
return imageName
end
--
-- Null coalesce helper
--
local function get_or_default(value, default)
if value ~= nil then return value end return default
end
local ImageInfo = {
fileName = nil,
file = nil,
scale = 1,
overridenWidth = nil,
}
--
-- ImageInfo constructor
--
-- @param file_name {string} The fill name (url) of the file
-- @param load_file {boolean} True to include the File object (Expensive function call)
--
function ImageInfo:new(file_name, load_file)
local o = {}
self.__index = self
setmetatable(o, self)
if file_name == nil then
return nil
end
o.fileName = parseImageName(file_name)
o.file = get_file(o.fileName, load_file)
return o
end
function ImageInfo:getWidth()
if self:hasOverriddenWidth() then
return self.overridenWidth
end
if self.file then
return get_or_default(self.file.width, 0)
end
return 0
end
function ImageInfo:getHeight()
if self.file ~= nil then
return get_or_default(self.file.height, 0)
end
return 0
end
function ImageInfo:setScale(scale)
if scale == nil then
error("scale argument may not be nil")
end
self.scale = scale
end
function ImageInfo:getScale()
return self.scale
end
function ImageInfo:setOverrideWidth(width)
if width == nil then
error("width argument may not be nil")
end
self.overridenWidth = width
end
function ImageInfo:hasOverriddenWidth()
return self.overridenWidth ~= nil
end
function ImageInfo:hasFile()
return self.file ~= nil
end
function ImageInfo:scaleToImage(otherImage)
if otherImage == nil then
self.scale = 1
end
local scale = get_or_default(otherImage:getScale(), 1)
self:setScale(scale)
end
function ImageInfo:getScaledWidth()
return math.floor(self:getWidth() * self:getScale())
end
function ImageInfo:getScaledHeight()
return math.floor(self:getHeight() * self:getScale())
end
function ImageInfo:getImageLink()
local width = self:getScaledWidth()
return cleanimage{file = self.fileName, width = width}
end
function ImageInfo:getRawImageLink()
local template = '[[File:{0}|{1}px]]'
return template
:gsub('{0}', self.fileName)
:gsub('{1}', self:getScaledWidth())
end
-- Static functios --
function ImageInfo._scaleToImage(left, right)
if left ~= nil then
left:scaleToImage(right)
end
end
function ImageInfo._overrideWidth(image, width)
if image ~= nil then
image:setOverrideWidth(width)
end
end
function ImageInfo._getWidestImage(images)
if images == nil then
error('images argument cannot be nil.')
end
local widest = 0
for i = 1, #images, 1
do
local img = images[i]
local width = images[i]:getWidth()
if width > widest then widest = width end
end
return widest
end
return ImageInfo