如何在ampify_img函数中获取图像的src

时间:2019-09-05 15:59:31

标签: php regex image amp-html

我正在使用ampify_img将常规图像标签转换为amp图像标签。 所以:

    <img src="dir/whatever/photo.jpg" alt="Whatever">

已转换为:

    <amp-img src="dir/whatever/photo.jpg" layout="responsive" class="i-amphtml-element"></amp-img>

问题是: 为了成为有效的放大器宽度和高度标记,必须在此转换后的标签中进行设置。而且我不知道如何将src提取到转换图像并写入新标记的函数中。 我知道我可以使用PHP getimagesize()获得图像大小,但不知道这在哪里。我不擅长正则表达式,这可能会使实现目标更加困难。

放大默认图片功能:

<?php
/**
 * Replace img tag with amp-img
 *
 * <amp-img src="[src]"
 *   width="[width]"
 *   height="[height]"
 *   layout="responsive"
 *   alt="[alt]">
 * </amp-img>
 *
 */
function ampify_img ($html) {
  preg_match_all("#<img(.*?)\\/?>#", $html, $matches);
  foreach ($matches[1] as $key => $m) {
    preg_match_all('/(alt|src|width|height)=("[^"]*")/i', $m, $matches2);
    $amp_tag = '<amp-img ';
    foreach ($matches2[1] as $key2 => $val) {
      $amp_tag .= $val .'='. $matches2[2][$key2] .' ';
    }
    $amp_tag .= 'layout="responsive"';
    $amp_tag .= '>';
    $amp_tag .= '</amp-img>';
    $html = str_replace($matches[0][$key], $amp_tag, $html);
  }
  return $html;
}

我试图从$ matches2 [2] [$ key2]或$ matches2 [2]或$ matches中提取getimagesize(),但没有成功。 我认为比起其他任何事情,都更了解在何处提取信息以写入$ amp_tag。

<?php
// comments where i tried to get info
function ampify_img ($html) {
  preg_match_all("#<img(.*?)\\/?>#", $html, $matches);
  foreach ($matches[1] as $key => $m) {
    preg_match_all('/(alt|src|width|height)=("[^"]*")/i', $m, $matches2);
    $amp_tag = '<amp-img ';
    foreach ($matches2[1] as $key2 => $val) {
      $amp_tag .= $val .'='. $matches2[2][$key2] .' '; // guess it can be here and possibly width and height can be writed here
    }
    $amp_tag .= 'layout="responsive"'; // certainly width and height can be writed here if we can get each image src at conversion and call PHP getimagesize
    $amp_tag .= '>';
    $amp_tag .= '</amp-img>';
    $html = str_replace($matches[0][$key], $amp_tag, $html);
  }
  return $html;
}

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,该解决方案不涉及使用ampify_img函数,而仅涉及放大器本身,从而改变了布局。 对于曾经有同样困难的人,必须实施放大器将其<img>标签图像实时转换为<amp-img>标签,这是一个解决方案,但是图像布局是固定的,可以保留长宽比。

我也不知道要检查长内容中的每个图像大小并写入标记中,cpu和ram会消耗什么。但是我认为这是一种理想的选择,为每个图像编写特定的宽度和高度,而不分配固定的布局。

解决方案非常容易实现:

function img_to_amp ($html) {
  preg_match_all("#<img(.*?)\\/?>#", $html, $matches);
  foreach ($matches[1] as $key => $m) {
    preg_match_all('/(alt|src|width|height)=("[^"]*")/i', $m, $matches2);
    $amp_tag = '<div class="fixed-height-container"><amp-img ';
    foreach ($matches2[1] as $key2 => $val) {
      $amp_tag .= $val .'='. $matches2[2][$key2] .' ';
    }
    $amp_tag .= 'class="contain" layout="fill"';
    $amp_tag .= '>';
    $amp_tag .= '</amp-img></div>';
    $html = str_replace($matches[0][$key], $amp_tag, $html);
  }
  return $html;
}

只需要输入: <div class="fixed-height-container">之前的<amp-img

并更改:$amp_tag .= 'layout="responsive"'; 到:$amp_tag .= 'class="contain" layout="fill"';

和上一个</div>之后的结尾$amp_tag .= '</amp-img></div>';

您可以在以下位置找到我找到此解决方案的放大器教程: amp.dev site

这样,您无需在图像标签上提供宽度和高度模式。


更新: ampify_img的作者返回了我的电子邮件,因此这是一种更好的方法。 PHP从那里检查img src和getimagesize()。这样,图像就会变得灵敏。

function img_to_amp ($html) {
    preg_match_all("#<img(.*?)\\/?>#", $html, $img_matches);
    foreach ($img_matches[1] as $key => $img_tag) {
      preg_match_all('/(alt|src|width|height)=["\'](.*?)["\']/i', $img_tag, $attribute_matches);
      $attributes = array_combine($attribute_matches[1], $attribute_matches[2]);
      if (!array_key_exists('width', $attributes) || !array_key_exists('height', $attributes)) {
        if (array_key_exists('src', $attributes)) {
          list($width, $height) = getimagesize($attributes['src']);
          $attributes['width'] = $width;
          $attributes['height'] = $height;
        }
      }
      $amp_tag = '<amp-img ';
      foreach ($attributes as $attribute => $val) {
        $amp_tag .= $attribute .'="'. $val .'" ';
      }
      $amp_tag .= 'layout="responsive"';
      $amp_tag .= '>';
      $amp_tag .= '</amp-img>';
      $html = str_replace($img_matches[0][$key], $amp_tag, $html);
    }
    return $html;
  }

您还可以检查代码并做出贡献 https://gist.github.com/elalemanyo/034490164beb7b935559585ff1cc7d9f