4

現在、CodeIgniter 3.1.3 とParsedown / Markdown Guideを使用しています

パースダウンを使用して、画像の幅と高さを設定できるようにしたいと思います

![enter image description][1]

[1]: http://www.example.com/image.png '100x200' <-- widthxheight

上記の方法を試しますが、代わりに画像のタイトルを設定します。

出力は次のようになります

<img src="http://www.example.com/image.png" width="100" height="200" alt="enter image description">

parsedown ライブラリの質問画像の幅と高さを取得および設定できるように変更する方法はありますか?

protected function inlineImage($Excerpt)
{
    if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[')
    {
        return;
    }

    $Excerpt['text']= substr($Excerpt['text'], 1);

    $Link = $this->inlineLink($Excerpt);

    if ($Link === null)
    {
        return;
    }

    $Inline = array(
        'extent' => $Link['extent'] + 1,
        'element' => array(
            'name' => 'img',
            'attributes' => array(
                'src' => $Link['element']['attributes']['href'],
                'alt' => $Link['element']['text'],
                'width' => '',
                'height' => ''
            ),
        ),
    );

    $Inline['element']['attributes'] += $Link['element']['attributes'];

    unset($Inline['element']['attributes']['href']);

    return $Inline;
}
4

2 に答える 2

6

この (参照) 構文の最後の要素[1]: http://www.example.com/image.png '100x200'

属性として渡されるtitleため、次のようにすることができます。

class MyParsedown extends Parsedown
{
    protected function inlineImage($Excerpt)
    {
        $Inline = parent::inlineImage($Excerpt);

        if (!isset($Inline['element']['attributes']['title'])) { return $Inline; }

        $size = $Inline['element']['attributes']['title'];

        if (preg_match('/^\d+x\d+$/', $size)) {
            list($width, $height) = explode('x', $size);

            $Inline['element']['attributes']['width'] = $width;
            $Inline['element']['attributes']['height'] = $height;

            unset ($Inline['element']['attributes']['title']);
        }

        return $Inline;
    }
}

パターンに一致する場合、属性はwidth+に変更されます。ページの破損を防ぐために桁数またはサイズを制限する場合があります。また、先頭の 0 は除外する必要があります (または のみのサイズ)。 heighttitleNUMERICxNUMERIC0

于 2017-01-12T09:31:06.207 に答える