Php 还原内容文件\u获取\u内容()函数

Php 还原内容文件\u获取\u内容()函数,php,Php,我将图像作为参数提供给file_get_content函数,并将其作为字符串输出接收。现在我想做与此相反的操作。我该怎么办?您可以使用 例如: $pathToImage = '/images/test.png'; $newFile = '/images/test-new.png'; // Get the contents of the image file $imgString = file_get_contents($pathToImage); // Write it back to a

我将图像作为参数提供给file_get_content函数,并将其作为字符串输出接收。现在我想做与此相反的操作。我该怎么办?

您可以使用

例如:

$pathToImage = '/images/test.png';
$newFile = '/images/test-new.png';


// Get the contents of the image file
$imgString = file_get_contents($pathToImage);

// Write it back to an image
file_put_contents($newFile, $imgString);

如果您随后查看文件夹images/,则应该有一个名为test new.png的新文件,该文件使用base64处理图像 与

在线base64图像转换为字符串

<?php

// Image as base64 truncated
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY8AAAFwCAYA...';

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);

$image = 'image.png';

file_put_contents($image, $data);

echo "<img src='$file' alt='image' />";