4 * Base class for the output of MediaHandler::doTransform() and Image::transform().
6 abstract class MediaTransformOutput
{
8 * Get the width of the output box
15 * Get the height of the output box
17 function getHeight() {
22 * @return string The thumbnail URL
29 * @return string Destination file path (local filesystem)
36 * Fetch HTML for this transform output
37 * @param array $attribs Advisory associative array of HTML attributes supplied
38 * by the linker. These can be incorporated into the output in any way.
39 * @param array $linkAttribs Attributes of a suggested enclosing <a> tag.
42 abstract function toHtml( $attribs = array() , $linkAttribs = false );
45 * This will be overridden to return true in error classes
52 * Wrap some XHTML text in an anchor tag with the given attributes
54 protected function linkWrap( $linkAttribs, $contents ) {
56 return Xml
::tags( 'a', $linkAttribs, $contents );
65 * Media transform output for images
67 class ThumbnailImage
extends MediaTransformOutput
{
69 * @param string $path Filesystem path to the thumb
70 * @param string $url URL path to the thumb
73 function ThumbnailImage( $url, $width, $height, $path = false ) {
75 # These should be integers when they get here.
76 # If not, there's a bug somewhere. But let's at
77 # least produce valid HTML code regardless.
78 $this->width
= round( $width );
79 $this->height
= round( $height );
84 * Return HTML <img ... /> tag for the thumbnail, will include
85 * width and height attributes and a blank alt text (as required).
87 * You can set or override additional attributes by passing an
88 * associative array of name => data pairs. The data will be escaped
89 * for HTML output, so should be in plaintext.
91 * If $linkAttribs is given, the image will be enclosed in an <a> tag.
93 * @param array $attribs
94 * @param array $linkAttribs
98 function toHtml( $attribs = array(), $linkAttribs = false ) {
99 $attribs['src'] = $this->url
;
100 $attribs['width'] = $this->width
;
101 $attribs['height'] = $this->height
;
102 if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
103 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
109 * Basic media transform error class
111 class MediaTransformError
extends MediaTransformOutput
{
112 var $htmlMsg, $textMsg, $width, $height, $url, $path;
114 function __construct( $msg, $width, $height /*, ... */ ) {
115 $args = array_slice( func_get_args(), 3 );
116 $htmlArgs = array_map( 'htmlspecialchars', $args );
117 $htmlArgs = array_map( 'nl2br', $htmlArgs );
119 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
120 $this->textMsg
= wfMsgReal( $msg, $args );
121 $this->width
= intval( $width );
122 $this->height
= intval( $height );
127 function toHtml( $attribs = array(), $linkAttribs = false ) {
128 return "<table class=\"MediaTransformError\" style=\"" .
129 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
131 "</td></tr></table>";
135 return $this->textMsg
;
138 function getHtmlMsg() {
139 return $this->htmlMsg
;
148 * Shortcut class for parameter validation errors
150 class TransformParameterError
extends MediaTransformError
{
151 function __construct( $params ) {
152 parent
::__construct( 'thumbnail_error',
153 max( @$params['width'], 180 ), max( @$params['height'], 180 ),
154 wfMsg( 'thumbnail_invalid_params' ) );