首页 > 解决方案 > Fopen如何在php中读取png文件

问题描述

我正在做一些事情,我需要一个 png 文件中的数据,我只能使用 PHP。不要问为什么给我答案哈哈

标签: phpfopen

解决方案


让我们假设下面是您的图像。

在此处输入图像描述

您必须使用以下行在变量中定义其 url

$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';

之后,您可以使用 file_get_content 函数来获取图像的数据/内容。

$image_content = file_get_contents($image);

另外,如果要将其转换为 base64 字符串,可以使用 base64_encode 函数。

$image_base64Data = base64_encode($image_content);

整个代码将......

<?php
$image = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
$image_content = file_get_contents($image);
$image_base64Data = base64_encode($image_content);
?>

推荐阅读