Files
GlModelViewer/includes/GlModelViewer.php
2024-10-23 07:23:37 -07:00

57 lines
2.1 KiB
PHP

<?php
class GlModelViewer extends ImageHandler {
public static function onMimeMagicInit(MimeAnalyzer $mime) {
$mime->addExtraTypes('model/gltf-binary glb gltf');
$mime->addExtraInfo('model/gltf-binary [DRAWING]');
}
public static function onMimeMagicImproveFromExtension( MimeAnalyzer $mimeAnalyzer, $ext, &$mime ) {
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
$mime = 'model/gltf-binary';
}
}
function doTransform($image, $dstPath, $dstUrl, $params, $flags = 0) {
}
public static function onBeforePageDisplay(OutputPage $out) {
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
if ($findGltf[0]) {
$out->addModules('ext.glmv');
}
}
public static function onImageBeforeProduceHTML( DummyLinker &$linker, Title &$title, &$file, array &$frameParams, array &$handlerParams, &$time, &$result, Parser $parser, string &$query, &$widthOption ) {
if ($file->getMimeType() !== 'model/gltf-binary') {
return true;
}
preg_match('/<pre>([\S\s]*?)<\/pre>/',$file->getDescriptionText(),$modelDescript);
$metadata = json_decode($modelDescript[1], true);
if (isset($frameParams['class'])) {
preg_match('/view-(\S*)/',$frameParams['class'],$viewClassExtract);
$viewClass = $viewClassExtract[1];
} else {
$viewClass = 'default';
}
$modelView = $metadata['viewerConfig'][$viewClass];
$modelView = array_merge(['src' => $file->getFullUrl()], $modelView);
$modelView['style'] = 'width: 800px; height: 600px;';
$result = Html::element('model-viewer', $modelView);
echo '<script>';
#GlModelViewer::console_log($handlerParams);
#GlModelViewer::console_log($frameParams['class']);
#GlModelViewer::console_log($file->getDescriptionText());
#GlModelViewer::console_log($metadata->viewerConfig->default);
echo '</script>';
return false;
}
public static function console_log($data, $add_script_tags = false) {
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
if ($add_script_tags) {
$command = '<script>'. $command . '</script>';
}
echo $command;
}
}