-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageSizeInfoFunctions.hooks.php
72 lines (68 loc) · 2 KB
/
ImageSizeInfoFunctions.hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* ImageSizeInfoFunctions
* ImageSizeInfoFunctions Hooks
*
* @license GNU GPL v2.0
* @package ImageSizeInfoFunctions
* @link https://github.com/CurseStaff/ImageSizeInfoFunctions
*
**/
class ImageSizeInfoFunctionsHooks {
/**
* Sets up this extension's parser functions.
*
* @access public
* @param object Parser object passed as a reference.
* @return boolean true
*/
static public function onParserFirstCallInit( Parser &$parser ) {
$parser->setFunctionHook( "imgw", "ImageSizeInfoFunctionsHooks::getImageWidth");
$parser->setFunctionHook( "imgh", "ImageSizeInfoFunctionsHooks::getImageHeight");
return true;
}
/**
* Function for when the parser object is being cleared.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserClearState
*
* @param $parser
* @return bool
*/
static public function onParserClearState( &$parser ) {
return true;
}
/**
* 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_FILE );
$file = wfFindFile( $title );
$width = ( is_object( $file ) && $file->exists() ) ? $file->getWidth() : 0;
return $width;
} catch ( Exception $e ) {
return wfMessage( 'error_returning_width' )->text();
}
}
/**
* Function to get the height of the image.
*
* @param $parser Parser object passed a reference
* @param string Name of the image being parsed in
* @return mixed integer of the height or error message.
*/
static public function getImageHeight( &$parser, $image = '' ) {
try {
$title = Title::newFromText( $image, NS_FILE );
$file = wfFindFile( $title );
$height = ( is_object( $file ) && $file->exists() ) ? $file->getHeight() : 0;
return $height;
} catch ( Exception $e ) {
return wfMessage( 'error_returning_height' )->text();
}
}
}