4 * Base class for the output of MediaHandler::doTransform() and File::transform().
8 abstract class MediaTransformOutput
{
10 * Get the width of the output box
17 * Get the height of the output box
19 function getHeight() {
24 * @return string The thumbnail URL
31 * @return string Destination file path (local filesystem)
38 * Fetch HTML for this transform output
39 * @param array $attribs Advisory associative array of HTML attributes supplied
40 * by the linker. These can be incorporated into the output in any way.
41 * @param array $linkAttribs Attributes of a suggested enclosing <a> tag.
44 abstract function toHtml( $attribs = array() , $linkAttribs = false );
47 * This will be overridden to return true in error classes
54 * Wrap some XHTML text in an anchor tag with the given attributes
56 protected function linkWrap( $linkAttribs, $contents ) {
58 return Xml
::tags( 'a', $linkAttribs, $contents );
67 * Media transform output for images
71 class ThumbnailImage
extends MediaTransformOutput
{
73 * @param string $path Filesystem path to the thumb
74 * @param string $url URL path to the thumb
77 function ThumbnailImage( $url, $width, $height, $path = false ) {
79 # These should be integers when they get here.
80 # If not, there's a bug somewhere. But let's at
81 # least produce valid HTML code regardless.
82 $this->width
= round( $width );
83 $this->height
= round( $height );
88 * Return HTML <img ... /> tag for the thumbnail, will include
89 * width and height attributes and a blank alt text (as required).
91 * You can set or override additional attributes by passing an
92 * associative array of name => data pairs. The data will be escaped
93 * for HTML output, so should be in plaintext.
95 * If $linkAttribs is given, the image will be enclosed in an <a> tag.
97 * @param array $attribs
98 * @param array $linkAttribs
102 function toHtml( $attribs = array(), $linkAttribs = false ) {
103 $attribs['src'] = $this->url
;
104 $attribs['width'] = $this->width
;
105 $attribs['height'] = $this->height
;
106 if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
107 return $this->linkWrap( $linkAttribs, Xml
::element( 'img', $attribs ) );
113 * Basic media transform error class
117 class MediaTransformError
extends MediaTransformOutput
{
118 var $htmlMsg, $textMsg, $width, $height, $url, $path;
120 function __construct( $msg, $width, $height /*, ... */ ) {
121 $args = array_slice( func_get_args(), 3 );
122 $htmlArgs = array_map( 'htmlspecialchars', $args );
123 $htmlArgs = array_map( 'nl2br', $htmlArgs );
125 $this->htmlMsg
= wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
126 $this->textMsg
= wfMsgReal( $msg, $args );
127 $this->width
= intval( $width );
128 $this->height
= intval( $height );
133 function toHtml( $attribs = array(), $linkAttribs = false ) {
134 return "<table class=\"MediaTransformError\" style=\"" .
135 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
137 "</td></tr></table>";
141 return $this->textMsg
;
144 function getHtmlMsg() {
145 return $this->htmlMsg
;
154 * Shortcut class for parameter validation errors
158 class TransformParameterError
extends MediaTransformError
{
159 function __construct( $params ) {
160 parent
::__construct( 'thumbnail_error',
161 max( isset( $params['width'] ) ?
$params['width'] : 0, 180 ),
162 max( isset( $params['height'] ) ?
$params['height'] : 0, 180 ),
163 wfMsg( 'thumbnail_invalid_params' ) );