使用 PHP 将图像转换为 Base64 编码
借助PHP中的多个内置函数,有很多方法可以将图像编码为base64格式。这些函数包括:pathinfofile_get_contentsbase64_encodemime_content_type在php中使用file_get_contents、pathinfo和base64_encode将图像编码为Base64图像应该有一个存储在变量中的定义路径,以使用file_get_contents、pathinfo和base64_encode将图像编码为base64。使用pathinfo功能读取信息。然后将结果信
借助 PHP 中的多个内置函数,有很多方法可以将图像编码为 base64 格式。
这些函数包括:
-
pathinfo -
file_get_contents -
base64_encode -
mime_content_type
在 php 中使用 file_get_contents、pathinfo 和 base64_encode 将图像编码为 Base64
图像应该有一个存储在变量中的定义路径,以使用 file_get_contents、pathinfo 和 base64_encode 将图像编码为 base64。
使用 pathinfo 功能读取信息。然后将结果信息传递给 file_get_contents 函数。
file_get_contents 的工作是将图像文件读入字符串。我们可以将字符串传递给 base64_encode 函数。
要显示 base64 编码的图像,连接字符串 data:image/、来自 pathinfo 的信息、字符串 ;base64 和 base64 编码的字符串。
php
// Define a path for the image
$image_path = 'xps-unsplash.jpg';
// Check the image type
$image_type = pathinfo($image_path, PATHINFO_EXTENSioN);
// Read the image into a string
$data = file_get_contents($image_path);
// Encode the image
$base64_encode = 'data:image:/' . $image_type . ';base64,' . base64_encode($data);
// Return the first 50 characters of the
// base64 encoded string
echo substr($base64_encode, 0, 50);
?>
输出:
data:image:/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD
在 PHP 中使用 file_get_contents 和 base64_encode 将图像编码为 Base64
要将图像转换为 base64,请将图像的路径直接传递给 file_get_contents 函数。
file_get_contents 函数将返回一个字符串。如果你将此字符串作为参数提供给 base64_encode 函数,将会有所帮助。
php
// Define a path for the image
$data = file_get_contents('xps-unsplash.jpg');
// Encode the image
$base64_encode = base64_encode($data);
// Return the first 50 characters of the
// base64 encoded string
echo substr($base64_encode, 0, 50);
?>
输出:
/9j/4AAQSkZJRgABAQEASABIAAD/4gIcSUNDX1BST0ZJTEUAAQ
在 PHP 中使用 base64_encode 和 mime_content_type 将图像编码为 Base64
使用 file_get_contents 函数读取图像路径。然后将生成的字符串转换为 base64 编码。
你可以在将图像源传递给 html 标记中的 src 标记之前对其进行格式化。
为此,你需要将图像格式化为 data:{mime};base64,{data}。你可以从格式中推断出你需要图像 mime 类型。
你可以使用 mime_content_type 函数获取图像 mime 类型。最后,你将格式作为 标记的 src 属性的值传递。
php
// Define a path for the image
$image_path = 'xps-unsplash.jpg';
// Read the image path and convert it to
// base64
$base64_encode = base64_encode(file_get_contents($image_path));
// Prepare the image for use in an img
// src tag
$image_source = 'data: ' . mime_content_type($image_path) . ';base64,' . $base64_encode;
// Display the image in the img src tag
echo '
';
?>
输出:

使用 PHP 中的自定义函数将图像编码为 Base64
在此函数的定义中,你可以使用诸如 file_get_contents、base64_encode 和 pathinfo 之类的方法来处理图像编码。
你可以使用 file_put_contents 实现缓存功能以获得更好的性能。
php
function encode_html_image (
$image_file,
$alternate_text = NULL,
$image_cache,
$image_extension = NULL
) {
// check if the image is not a file
if (!is_file($image_file)) {
return false;
}
// Save the image as base64 extension for
// use as a cache
$image_with_base64_extension = "{$image_file}.base64";
if ($image_cache && is_file($image_with_base64_extension)) {
// Use the cache image
$base64_encoded_image = file_get_contents($image_with_base64_extension);
} else {
// Create a new base64 encoded image
$image_binary_data = file_get_contents($image_file);
$base64_encoded_image = base64_encode($image_binary_data);
if ($image_cache) {
file_put_contents($image_with_base64_extension, $base64_encoded_image);
}
}
$image_extension = pathinfo($image_file, PATHINFO_EXTENSION);
return "
";
}
$sample_image = 'xps-unsplash.jpg';
$encoded_image = encode_html_image($sample_image, 'XPS by DELL', true);
echo $encoded_image;
?>
输出:

Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















