Ron*_*rer 533
我认为它应该是:
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
Run Code Online (Sandbox Code Playgroud)
Pek*_*ica 117
简单:
$imagedata = file_get_contents("/path/to/image.jpg");
// alternatively specify an URL, if PHP settings allow
$base64 = base64_encode($imagedata);
Run Code Online (Sandbox Code Playgroud)
请记住,这会将数据扩大33%,并且您将遇到大小超过您的数据的问题memory_limit
.
小智 35
也可以这种方式用base64编码格式表示图像...找到PHP函数file_get_content
,然后使用函数base64_encode
并得到结果准备str作为data:" . file_mime_type . " base64_encoded string
.在img src属性中使用它.看下面的代码可以帮到你.
// A few settings
$img_file = 'raju.jpg';
// Read image path, convert to base64 encoding
$imgData = base64_encode(file_get_contents($img_file));
// Format the image SRC: data:{mime};base64,{data};
$src = 'data: '.mime_content_type($img_file).';base64,'.$imgData;
// Echo out a sample image
echo '<img src="'.$src.'">';
Run Code Online (Sandbox Code Playgroud)
yck*_*art 13
万一你(无论出于何种原因)无法使用curl
也file_get_contents
可以解决:
$img = imagecreatefrompng('...');
ob_start();
imagepng($img);
$bin = ob_get_clean();
$b64 = base64_encode($bin);
Run Code Online (Sandbox Code Playgroud)
Gol*_*naz 10
<img src="data:image/png;base64,<?php echo base64_encode(file_get_contents("IMAGE URL HERE")) ?>">
Run Code Online (Sandbox Code Playgroud)
我试图使用这个资源,但一直出错,我发现上面的代码完美无缺.
刚用图像的URL替换了IMAGE URL - http://www.website.com/image.jpg
非常简单并且常用:
function getDataURI($imagePath) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type = $finfo->file($imagePath);
return 'data:'.$type.';base64,'.base64_encode(file_get_contents($imagePath));
}
//Use the above function like below:
echo '<img src="'.getDataURI('./images/my-file.svg').'" alt="">';
echo '<img src="'.getDataURI('./images/my-file.png').'" alt="">';
Run Code Online (Sandbox Code Playgroud)
注意:将自动添加文件的Mime-Type(从此PHP文档获取帮助).
归档时间: |
|
查看次数: |
372006 次 |
最近记录: |