我该如何将来自URL的图像转换为Base64编码?
我认为应该是这样:
$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
pathinfo($path, PATHINFO_EXTENSION)
这部分可能不能用于 SVG 图像。只需将最后一行更改为$base64 = 'data:image/svg+xml;base64,' . base64_encode($data);
- Eddie C.简单:
$imagedata = file_get_contents("/path/to/image.jpg");
// alternatively specify an URL, if PHP settings allow
$base64 = base64_encode($imagedata);
请记住,这将使数据增加33%,并且文件大小超过您的memory_limit
时会出现问题。
data:image/type;base64,
),因此仅凭这段代码无法显示图像。 - Jose Manuel Abarca Rodríguez同样可以使用这种方式来表示Base64编码格式的图片...
查找PHP函数file_get_content
,接着使用函数base64_encode
。
获取结果以准备字符串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.'">';
如果由于任何原因你无法使用 curl
或 file_get_contents
,你可以采用以下方法:
$img = imagecreatefrompng('...');
ob_start();
imagepng($img);
$bin = ob_get_clean();
$b64 = base64_encode($bin);
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="">';
<img src="data:image/png;base64,<?php echo base64_encode(file_get_contents("IMAGE URL HERE")) ?>">
我一直在尝试使用这个资源,但是一直遇到错误。我发现上面的代码完美地解决了问题。
我只需将 "IMAGE URL HERE" 替换为您图像的网址 - http://www.website.com/image.jpg
下面是将编码上传并保存到MySQL数据库的代码:
if (!isset($_GET["getfile"])) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
$bin_string = file_get_contents($_FILES["file"]["name"]);
$hex_string = base64_encode($bin_string);
$mysqli = mysqli_init();
if (!$mysqli->real_connect('localhost', 'root', '', 'arihant')) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
$mysqli->query("INSERT INTO upload(image) VALUES ('" . $hex_string . "')");
}
}
echo "<img src='data:image/jpeg;base64, $image' width=300>";
public static function getImageDataFromUrl($url)
{
$urlParts = pathinfo($url);
$extension = $urlParts['extension'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);
$base64 = 'data:image/' . $extension . ';base64,' . base64_encode($response);
return $base64;
}
<?php
$url = "http://example.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
?>
<img src="data:image/png;base64,<?php echo base64_encode($output);?>">