简体   繁体   中英

Restore content file_get_content() function

I gave an image as a parameter to the file_get_content() function and received it as the output of a string. Now I want to do the opposite of this operation. What should I do?

You can write it back to an image file using file_put_contents

Example:

$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);

If you then look into your folder "images/", there should be a new file called "test-new.png"

Deal with image using base64 with file_put_contents

Online base64 image converte as string encode image

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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