PHP:如何将图像从 URL 转换为 Base64?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4343715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:44:16  来源:igfitidea点击:

PHP: How to convert an image from URL to Base64?

phpbinarybase64

提问by Sharj

I want to convert image from its url to base64.

我想将图像从其 url 转换为 base64。

回答by jwueller

Do you want to create a data url? You need a MIME-Type and some other additional information then (see Wikipedia). If this is not the case, this would be a simple base64 representation of the image:

您要创建数据网址吗?您需要一个 MIME-Type 和一些其他附加信息(请参阅Wikipedia)。如果不是这种情况,这将是图像的简单 base64 表示:

$b64image = base64_encode(file_get_contents('path/to/image.png'));

Relevant docs: base64_encode()-function, file_get_contents()-function.

相关文档:base64_encode()-functionfile_get_contents()-function

回答by mcelicalderon

I got to this question searching for a similar solution, actually, I understood that this was the original question.

我找到了这个问题,寻找类似的解决方案,实际上,我明白这是最初的问题。

I wanted to do the same, but the file was in a remote server, so this is what I did:

我想做同样的事情,但文件在远程服务器中,所以我这样做了:

$url = 'http://yoursite.com/image.jpg';
$image = file_get_contents($url);
if ($image !== false){
    return 'data:image/jpg;base64,'.base64_encode($image);

}

So, this code is from a function that returns a string, and you can output the return value inside the src parameter of an img tag in html. I'm using smarty as my templating library. It could go like this:

所以,这段代码来自一个返回字符串的函数,你可以在html中的一个img标签的src参数中输出返回值。我使用 smarty 作为我的模板库。它可以是这样的:

<img src="<string_returned_by_function>">

Note the explicit call to:

注意显式调用:

if ($image !== false)

This is necessary because file_get_contents can return 0 and be casted to false in some cases, even if the file fetch was successful. Actually in this case it shouldn't happen, but its a good practice when fetching file content.

这是必要的,因为 file_get_contents 可以返回 0 并在某些情况下被强制转换为 false,即使文件获取成功。实际上在这种情况下它不应该发生,但是在获取文件内容时这是一个很好的做法。

回答by Gaurang P

Try this:-

尝试这个:-

Example One:-

示例一:-

<?php 
function base64_encode_image ($filename=string,$filetype=string) {
    if ($filename) {
        $imgbinary = fread(fopen($filename, "r"), filesize($filename));
        return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
    }
}
?>

used as so

<style type="text/css">
.logo {
    background: url("<?php echo base64_encode_image ('img/logo.png','png'); ?>") no-repeat right 5px;
}
</style>

or

<img src="<?php echo base64_encode_image ('img/logo.png','png'); ?>"/>

Example Two:-

例子二:-

$path= 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

回答by Lobo

I'm not sure, but check this example http://www.php.net/manual/es/function.base64-encode.php#99842

我不确定,但请查看此示例http://www.php.net/manual/es/function.base64-encode.php#99842

Regards!

问候!

回答by Andreas Wong