我正在使用一个带有wordpress的视频网站和我的懒虫是这样工作的
<img class="b-lazy" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src="images/image.jpg" data-src-small="images/image.jpg" alt="alt text" class="tmb_img"/>
我为wordpress缩略图设计了它但当我使用视频抓取器抓取多缩略图时
图像以这种形式来自plguin;
<img id="latest-196" class="img-responsive" alt="alt text" onmouseout="thumbStop('latest-196', 'xxxxxx/wp-content/uploads/multi-thumbs/96/', '3');" onmouseover="thumbStart('latest-196', 15, ''xxxxxx/wp-content/uploads/multi-thumbs/96/');" src="'xxxxxx/wp-content/uploads/multi-thumbs/96/196_3.jpg">
有没有任何方法可以使用任何函数将第二种类型与第一种类型转换为惰性加载?
我希望你能理解我的问题谢谢。
您可以使用输出缓冲,就像这里解释的那样,然后使用preg-replace来编辑您需要的内容。
所以这可能是你的图像模板:
<div id="images">
<?php
// start output buffering
ob_start();
// get the images
$images = get_images_from_video_grabber();
foreach ($images as $im_tag) {
echo $im_tag;
}
// get the buffer and end output buffering
$output = ob_get_clean();
// replace what you need
$pattern = '~<img (.+)src="(.+)">~';
$replacement = '<img \1src="data:image/gif;base64,..." data-src="\2">';
$output = preg_replace($pattern, $replacement, $output);
// echo the buffer
echo $output;
?>
</div>
也许还有更优雅的解决方案(使用过滤器和操作),但如果不知道你使用的是哪个视频抓取器,我就无法检查。