October 22, 2024
Chicago 12, Melborne City, USA
PHP

How to define new Image formatting options?


I’d like to define a new image formatting option, in addition to the available mediawiki image formatting options, so I can apply it as needed.

I’m very new at web development and I’m having difficulty determining which file(s) I need to work in to achieve this. After messing around for a while I believe it may need to be done using the parser.php file, but am wondering if anyone has experience with this and can provide more guidance and ideally, example code.

This is the block of code, in the parser.php file, that I suspect is at least partially involved:

    /**
     * @param MediaHandler|false $handler
     * @return array
     */
    private function getImageParams( $handler ) {
        if ( $handler ) {
            $handlerClass = get_class( $handler );
        } else {
            $handlerClass="";
        }
        if ( !isset( $this->mImageParams[$handlerClass] ) ) {
            # Initialise static lists
            static $internalParamNames = [
                'horizAlign' => [ 'left', 'right', 'center', 'none' ],
                'vertAlign' => [ 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
                    'bottom', 'text-bottom' ],
                'frame' => [ 'thumbnail', 'manualthumb', 'framed', 'frameless',
                    'upright', 'border', 'link', 'alt', 'class' ],
            ];
            static $internalParamMap;
            if ( !$internalParamMap ) {
                $internalParamMap = [];
                foreach ( $internalParamNames as $type => $names ) {
                    foreach ( $names as $name ) {
                        // For grep: img_left, img_right, img_center, img_none,
                        // img_baseline, img_sub, img_super, img_top, img_text_top, img_middle,
                        // img_bottom, img_text_bottom,
                        // img_thumbnail, img_manualthumb, img_framed, img_frameless, img_upright,
                        // img_border, img_link, img_alt, img_class
                        $magicName = str_replace( '-', '_', "img_$name" );
                        $internalParamMap[$magicName] = [ $type, $name ];
                    }
                }
            }

            # Add handler params
            $paramMap = $internalParamMap;
            if ( $handler ) {
                $handlerParamMap = $handler->getParamMap();
                foreach ( $handlerParamMap as $magic => $paramName ) {
                    $paramMap[$magic] = [ 'handler', $paramName ];
                }
            } else {
                // Parse the size for non-existent files.  See T273013
                $paramMap[ 'img_width' ] = [ 'handler', 'width' ];
            }
            $this->mImageParams[$handlerClass] = $paramMap;
            $this->mImageParamsMagicArray[$handlerClass] =
                $this->magicWordFactory->newArray( array_keys( $paramMap ) );
        }
        return [ $this->mImageParams[$handlerClass], $this->mImageParamsMagicArray[$handlerClass] ];
    }

I added universal image border styling in the content.media-common.less file but it did not apply to my /template files and I ultimately think it isn’t applicable to what I’m trying to accomplish as it applies universally and is not an as-needed formatting option.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video