Mr.*_* ED 3 php codeigniter parsedown
目前我使用CodeIgniter 3.1.3和Parsedown/Markdown Guide
解析后,我希望能够设置图像的宽度和高度
![enter image description][1]
[1]: http://www.example.com/image.png '100x200' <-- widthxheight
Run Code Online (Sandbox Code Playgroud)
我尝试上面的方式,但设置图像标题.
输出将是
<img src="http://www.example.com/image.png" width="100" height="200" alt="enter image description">
Run Code Online (Sandbox Code Playgroud)
解析库中的问题是否有任何方法可以修改它,以便获取和设置图像的宽度和高度?
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;
}
Run Code Online (Sandbox Code Playgroud)
此(引用)语法的最后一个元素 [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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果匹配模式,属性将更改为width+ .您可以限制数字或大小以保护打破页面,也应排除前导0(或仅限大小). heighttitleNUMERICxNUMERIC0