2017-01-10 110 views
2

目前我使用的CodeIgniter 3.1.3和Parsedown/Markdown GuideParsedown設置圖像的寬度和高度

隨着解析下來,我想能夠把圖像

![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

從這裏(參考)語法[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 + height如果title匹配NUMERICxNUMERIC模式。您可能會限制數字或大小的數量來保護打破頁面,也應該排除領先0(或只有0的大小)。

+0

我試圖擴展它codeigniter的方式,但沒有運氣有另一種方式與我的代碼混合 – user4419336

+0

@ wolfgang1983它應該不是一個問題。你如何加載'Parsedown'庫?如果你使用'$ autoload'數組,那麼'Parsedown'和'MyParsedown'(或者你命名這個類和文件)都應該被註冊。唯一更簡單的方法是在原始文件的末尾粘貼此方法的主體(沒有初始父調用)來更改原始文件(第1188行) – shudder

+0

如果我將系統庫中的解析庫放在系統庫中而不是應用程序,則使用MY_Parsedown應用程序庫然後工作。 – user4419336