社区文档编写中,欢迎参与。社区答疑群(非官方):717421103
版本250312.1.2
全站通知:

帮助:解析函数/imgw

来自WIKI实验室WIKI_BWIKI_哔哩哔哩
< 帮助:解析函数(重定向自帮助:Imgw
跳到导航 跳到搜索

imgw是一个解析函数。帮助:解析函数页列出了所有解析函数的说明。

imgw

获取指定图片宽度。出自扩展 ImageSizeInfoFunctions

语法

{{#imgw: 图片名称 }}

  • 图片名称:需包含文件扩展名,可不带"文件:"前缀

不存在的文件返回0。

由于需要查询并读取文件,此函数的开销较高。在新版此扩展中,imgw被标记为高开销函数(BWiki目前使用的版本没有)。

示例

  • 获取存在图片的宽度 {{#imgw: 文件:Wikilogo.png }} → 440
  • 省略命名空间前缀 {{#imgw: Wikilogo.png }} → 440
  • 不存在的文件 {{#imgw: aaaaaaaaaaa.png }} → 0

底层代码

来自MediaWiki及其扩展的源代码,运行在服务端。此处仅供快速查阅,便于更充分的挖掘其“特性”。

/** ImageSizeInfoFunctions-v1.1.1\ImageSizeInfoFunctions.hooks.php
 * Function to get the width of the image.
 *
 * @param	$parser	Parser object passed a reference
 * @param	string	Name of the image being parsed in
 * @return	mixed	integer of the width or error message.
 */
static public function getImageWidth( &$parser, $image = '' ) {
	try {
		$title = Title::newFromText( $image, NS_IMAGE );
		$file = wfFindFile( $title );
		$width = ( is_object( $file ) && $file->exists() ) ? $file->getWidth() : 0;
		return $width;
	} catch ( Exception $e ) {
		return wfMessage( 'error_returning_width' )->text();
	}
}
代码逻辑:
  • 查找文件
  • 读取图片宽度,如果文件不存在则高度为0

实际用例

一些Wiki使用了相关特性,如下所示这个静态列表可能在下列页面更改后过时仅供批判性参考

评论

BWIKI首页创建WIKIBWIKI客服BWIKI君

目录