首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Parsedown设置的图像宽度和高度

Parsedown设置的图像宽度和高度
EN

Stack Overflow用户
提问于 2017-01-10 05:24:41
回答 2查看 1.1K关注 0票数 4

目前我使用的是CodeIgniter 3.1.3和帕赛 / 减价指南

通过向下解析,我希望能够设置图像的宽度和高度。

代码语言:javascript
复制
![enter image description][1]

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

我尝试了上面的方法,但是设置了图像标题。

产出将

代码语言:javascript
复制
<img src="http://www.example.com/image.png" width="100" height="200" alt="enter image description">

问题在解析库中有什么方法可以修改它从而获得和设置图像的宽度和高度吗?

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-12 09:31:06

这个(引用)语法[1]: http://www.example.com/image.png '100x200'中的最后一个元素

将作为title属性传递,因此您可以这样做:

代码语言:javascript
复制
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+height,如果titleNUMERICxNUMERIC模式匹配。您可能会限制数字或大小的数量以保护破页,还应该排除前面的0(或者只有0的大小)。

票数 6
EN

Stack Overflow用户

发布于 2018-08-22 20:24:14

只需稍微改变一下接受答案,就可以通过将宽度或高度设置为零来使保持原来的纵横比

代码语言:javascript
复制
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);

            if($width > 0) $Inline['element']['attributes']['width'] = $width;
            if($height > 0) $Inline['element']['attributes']['height'] = $height;

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

        return $Inline;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41561684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档