目前我使用的是CodeIgniter 3.1.3和帕赛 / 减价指南
通过向下解析,我希望能够设置图像的宽度和高度。
![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">问题在解析库中有什么方法可以修改它从而获得和设置图像的宽度和高度吗?
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;
}发布于 2017-01-12 09:31:06
这个(引用)语法[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的大小)。
发布于 2018-08-22 20:24:14
只需稍微改变一下接受答案,就可以通过将宽度或高度设置为零来使保持原来的纵横比:
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;
}
}https://stackoverflow.com/questions/41561684
复制相似问题