75 lines
2.0 KiB
PHP
75 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\KarateMedia;
|
|
|
|
use File;
|
|
use MediaHandler;
|
|
use MediaTransformOutput;
|
|
|
|
/**
|
|
* MediaHandler fuer [[Datei:Beispiel.mp4|mini|Beschreibung]] und [[File:Beispiel.mp4|300x169px]]
|
|
*/
|
|
class KarateVideoHandler extends MediaHandler {
|
|
|
|
public function getParamMap(): array {
|
|
return [
|
|
'img_width' => 'width',
|
|
];
|
|
}
|
|
|
|
public function validateParam( $name, $value ): bool {
|
|
if ( in_array( $name, [ 'width', 'height' ], true ) ) {
|
|
return $value > 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function makeParamString( $params ): string {
|
|
$width = $params['width'] ?? 320;
|
|
return "{$width}px";
|
|
}
|
|
|
|
public function parseParamString( $str ) {
|
|
if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
|
|
return [ 'width' => (int)$m[1] ];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function normaliseParams( $image, &$params ): bool {
|
|
if ( ( !isset( $params['width'] ) || $params['width'] <= 0 ) && isset( $params['height'] ) && $params['height'] > 0 ) {
|
|
$params['width'] = (int)round( $params['height'] * 16 / 9 );
|
|
}
|
|
if ( !isset( $params['width'] ) || $params['width'] <= 0 ) {
|
|
$params['width'] = 320;
|
|
}
|
|
// Verhindere briefmarkengrosse 2014er Video-Player (z.B. 150x100px), in denen die Bedienelemente unbenutzbar sind
|
|
if ( $params['width'] < 280 ) {
|
|
$ratio = ( isset( $params['height'] ) && $params['height'] > 0 )
|
|
? ( $params['height'] / $params['width'] )
|
|
: ( 9 / 16 );
|
|
$params['width'] = 280;
|
|
$params['height'] = (int)round( 280 * $ratio );
|
|
}
|
|
if ( !isset( $params['height'] ) || $params['height'] <= 0 ) {
|
|
$params['height'] = (int)round( $params['width'] * 9 / 16 );
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function getImageSize( $image, $path ) {
|
|
// Standard 16:9 Fallback-Dimensionen fuer den MediaWiki-Layout-Berechner
|
|
return [ 640, 360 ];
|
|
}
|
|
|
|
public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ): MediaTransformOutput {
|
|
$this->normaliseParams( $image, $params );
|
|
return new KarateVideoTransformOutput(
|
|
$image,
|
|
$image->getUrl(),
|
|
(int)$params['width'],
|
|
(int)$params['height']
|
|
);
|
|
}
|
|
}
|