【问题标题】:Parsedown set image width and heightParsedown 设置图片宽高
【发布时间】:2017-05-24 12:29:56
【问题描述】:

目前我使用 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">

Question 在 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;
}

【问题讨论】:

    标签: php codeigniter parsedown


    【解决方案1】:

    此(参考)语法[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;
        }
    }
    

    如果title 匹配NUMERICxNUMERIC 模式,属性将更改为width+height。您可能会限制位数或大小以保护分页,还应排除前导 0(或仅包含 0 的大小)。

    【讨论】:

    • 我尝试以 codeigniter 的方式扩展它,但没有运气还有其他方法可以将它与我的代码混合
    • @wolfgang1983 这应该不是问题。你如何加载Parsedown 库?如果您使用$autoload 数组,那么ParsedownMyParsedown(或任何您命名的类和文件)都应该被注册。唯一更简单的方法是通过在原始文件的末尾(第 1188 行)粘贴此方法的主体(没有初始父调用)来更改原始文件
    • 如果我将解析库放在系统库而不是应用程序中,然后在应用程序库中使用 MY_Parsedown 则它可以工作。
    • @shudder “在原始方法的末尾(没有初始父调用)粘贴此方法的主体(第 1188 行)” 不确定我是否做得正确。只需将您的方法复制并粘贴到当前 Parsedown.php 文件 (Parsedown with Image Resize) 的末尾即可。不幸的是,它没有奏效。知道为什么吗?
    • @shudder 刚刚发现问题:$parsedown = new MyParsedown;
    【解决方案2】:

    accepted answer 略有不同,可以通过将宽度或高度设置为零来保持原始纵横比

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-06
      • 2016-10-08
      • 1970-01-01
      • 2019-06-20
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      相关资源
      最近更新 更多