Difference between revisions of "Module:Main page image"

From blackwiki
Jump to navigation Jump to search
m (8 revisions imported)
blackwiki>MSGJ
(new image size calculation)
Line 8: Line 8:
  
 
function p._width(args)
 
function p._width(args)
local new_width = 140 -- square image width
+
local new_width = 140
 
local page = mw.title.makeTitle('File', args[1])
 
local page = mw.title.makeTitle('File', args[1])
 
if not page.fileExists then
 
if not page.fileExists then
Line 14: Line 14:
 
end
 
end
  
local ratio = page.file.width / page.file.height
+
new_width = math.floor ( ( page.file.width / math.sqrt ( ( page.file.width * page.file.height ) / 19600 ) ) + 0.5 )
 
if ratio > 1.2 then -- consider images with aspect ratio 5:6 - 6:5 as "basically square"
 
new_width = 160 -- tall image width
 
elseif ratio < 0.8333 then
 
new_width = 120 -- long image width
 
end
 
  
 
return new_width
 
return new_width

Revision as of 06:51, 8 September 2020

This module provides utility for wrappers of {{Main page image}}.

Usage

{{#invoke:Main page image|width|ImageTitle}} Get image width from ImageTitle.

ImageTitle must not contain the namespace prefix (eg "File:"). You can use Template:T to strip it, if it exists, when sanitising template params. Example: {{#invoke:Main page image|width|{{Remove file prefix|{{{image}}}}}}}



local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.width(frame)
	local args = getArgs(frame)
	return p._width(args)
end

function p._width(args)
	local new_width = 140
	local page = mw.title.makeTitle('File', args[1])
	if not page.fileExists then
		return new_width
	end

	new_width = math.floor ( ( page.file.width / math.sqrt ( ( page.file.width * page.file.height ) / 19600 ) ) + 0.5 )

	return new_width
end

return p