admin管理员组

文章数量:1289528

I want to encode images in base64 for better solution to SEO proposes, I am fix some issues in audit and it say to use new image formats then load images as default like <img src='path/to/image.jpg'/> so on my research I found that base64 encoded images solve this warning in audit.

I want to encode images in base64 for better solution to SEO proposes, I am fix some issues in audit and it say to use new image formats then load images as default like <img src='path/to/image.jpg'/> so on my research I found that base64 encoded images solve this warning in audit.

Share Improve this question asked Jun 30, 2019 at 17:59 Rafael GuimarãesRafael Guimarães 1191 silver badge3 bronze badges 1
  • 1 Using a base64 encoded image src might make the warning go away, but you haven't actually solved the problem. It's still a JPG. – Jacob Peattie Commented Jul 1, 2019 at 8:31
Add a comment  | 

1 Answer 1

Reset to default 3

in your file functions.php in your template put this funcitons

/**
* @param $path
* @return string
* @author https://github/ozzpy
*/
function imageEncode($path)
{
$path  = __DIR__."/".$path;
$image = file_get_contents($path);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type  = $finfo->buffer($image);
return "data:".$type.";charset=utf-8;base64,".base64_encode($image);
}

/**
 * @param $path
 * @return string
 * @author https://github/ozzpy
 */
function imageEncodePath($path)
{
$image = file_get_contents($path);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type  = $finfo->buffer($image);
return "data:".$type.";charset=utf-8;base64,".base64_encode($image);
}


/**
 * @param $path
 * @return string
 * @author https://github/ozzpy
 */
function imageEncodeURL($path)
{
$image = file_get_contents($path);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type  = $finfo->buffer($image);
return "data:".$type.";charset=utf-8;base64,".base64_encode($image);
}

in your html files in your template use it like:

template level: <img src="<?php echo imageEncode("img/logo.jpeg");?>"/>

another path level: <img src="<?php echo imageEncodePath(__DIR__."/images/slider.png");?>"/>

outside url level: <img src="<?php echo imageEncodeURL("https://somedomain/img/image.jpeg");?>"/>

本文标签: seohow to base64 encode images in wordpress template