Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何更改wordpress img属性_Php_Html_Image - Fatal编程技术网

Php 如何更改wordpress img属性

Php 如何更改wordpress img属性,php,html,image,Php,Html,Image,我习惯于在wordpress中延迟加载图像。我想把img属性改成这样 <img data-src="http://lorempixel.com/g/800/500/city/10" data-src-retina="http://lorempixel.com/g/1280/800/city/10" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /> 你能分享你

我习惯于在wordpress中延迟加载图像。我想把img属性改成这样

<img data-src="http://lorempixel.com/g/800/500/city/10" data-src-retina="http://lorempixel.com/g/1280/800/city/10" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />

你能分享你的代码吗?
function image_tag($html, $id, $alt, $title) {
//generate data url
$type = pathinfo($img, PATHINFO_EXTENSION);
$data = file_get_contents($img);
$src = 'data:image/' . $type . ';base64,' . base64_encode($data);

return preg_replace(array(
        '/'.str_replace('//','//',get_bloginfo('url')).'/i',
        '/s+width="d+"/i',
        '/s+height="d+"/i',
        '/alt=""/i'
    ),
    array(
        'data-src=" "',
        'src="'.$src.'"',
        'data-src-retina=" "',
        'alt="' . $title . '"'
    ),
    $html);
}
add_filter('get_image_tag', 'image_tag', 0, 4);