繁体   English   中英

恢复内容 file_get_content() function

[英]Restore content file_get_content() function

我将图像作为参数提供给 file_get_content() function 并将其作为字符串的 output 接收。 现在我想做与此操作相反的操作。 我应该怎么办?

您可以使用file_put_contents将其写回图像文件

例子:

$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 和file_put_contents处理图像

在线 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' />";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM