Restore content file_get_content() function - php

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

Related

Base 64 Image String Convert and Upload To the PHP server

I Have Image String Like
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1IAAAE7CAYAAADXQrC8AAANvUlEQVR4nO3dLXoqWRuGUebDGBgBA0BjsZFxuDhcVFQMKiYmBoPBIDCImAgEAoGoCexPpM9PkirgSYo06W+JZbr7UPu8u0XdV8GuTlVVBQAAgNN1/u0FAAAA/DRCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJKQAAABCQgoAACAkpAAAAEJCCgAAICSkAAAAQkIKAAAgJ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FAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQElIAAAAhIQUAABASUgAAACEhBQAAEBJSAAAAISEFAAAQ+h9OaCdW3kXmdwAAAABJRU5ErkJggg==';
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data); // Decode image using base64_decode
$file = uniqid() . '.jpg';
//Now you can put this image data to your desired file using file_put_contents function like below:
$success = file_put_contents($file, $data);
I wanted To convert Into Image and Upload To the Server
The Upload Folder Like
www.abc.com/uploads
And My File is
Www.abc.com/webservices/index.php (In this file I will write the code)
Thanks In advance
One way of doing it,
function writePng($data, $filename){
$bin = substr($data, strlen('data:image/png;base64,'));
$bin=base64_decode($bin);
$fp = fopen($filename,'w+');
fwrite($fp, $bin);
fclose($fp);
}
writePng($data, '1.png');

Can call imagecreatefromjpeg() with data-uri as parameter?

I used data-uri data:image/jpeg;base64,BASE64_HERE to pass the image. Can I pass this value imagecreatefromjpeg() to save as an image file.
How does this function work?
imagecreatefromjpeg() function creates image variable from file, but not backwards. While you have image source in base64 you can use simple file_put_contents() function to save file. Eg.:
$data = 'data:image/jpeg;base64,BASE64_HERE';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.jpg', $data);
You can also use imagecreatefromjpeg() and then imagejpeg() method if it's more preferable to you. But best method is to use imagecreatefromstring(), because it will automatically detects whenever image is jpg,gif,png or etc. Eg.:
$data = 'data:image/jpeg;base64,BASE64_HERE';
$image = imagecreatefromstring(base64_decode($data));
imagejpeg($image, 'image.jpg');

PHP base64_docode saves corrupt image

I am sending base64 encoded image to PHP from my android app. Sometime it stores full image (4KB) and sometime (3KB) (Same Image). when I use URL in picasso, image with 4KB size works fine but image with 3KB size does not load it shows decode error.
This is my PHP code (which sometime works)
$encodedImage = str_replace(' ','+',$_POST['encodedProfileImage']);
$data = base64_decode($encodedImage);
$file = 'Pics/'. uniqid() . '.png';
$success = file_put_contents($file, $data);
$BASE_URL = 'http://domain.com/TestApp/';
I then do SQL operation in PHP to store image path. Is there any chance that next code operation is done on half decoded image(which is corrupt).
You need to remove the part that says data:image/png;base64, at the beginning of the image data. The actual base64 data comes after that.
Use below function:-
function base64_to_png($base64_string, $output_file) {
$ifp = fopen($output_file, "wb");
$data = explode(',', $base64_string);
fwrite($ifp, base64_decode($data[1]));
fclose($ifp);
return $output_file;
}
If you want to use str_replace function then may be below way work. I am not sure :)
$fname = filter_input(INPUT_POST, "name");
$encodedImage = filter_input(INPUT_POST, "image");
$encodedImage = str_replace('data:image/png;base64,', '', $encodedImage);
$encodedImage = str_replace(' ', '+', $encodedImage);
$encodedImage = base64_decode($encodedImage);
file_put_contents($fname, $encodedImage);
print "Image has been saved!";
Hope it will help you :)

string to file stream using php or jquery

i have image that was in base64 format as follows:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAE........
I am trying to convert the above string to input file stream. I have checked for this but i didn't get anything, please let me know the process.
You have to use the file put contents.
Try this:
// Your image data
$data = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pV....
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
//Image name
$filename = "myname.png";
$data = base64_decode($data);
$file = 'uploads/' . $filename;
// decode the image data and save it to file
file_put_contents($file, $data);

unlink base64 encoded image not working

hi guys ive created a base64 encoded image captured with web cam now i convert the .png to .jpg all works fine but now i get two images on server both .png and .jpg how do i go about deleting the .png or is their a way to convert to jpg without saving .png image to disk thanx here my code
$rawData = $_POST['imgBase64'];
$filteredData = explode(',', $rawData);
$unencoded = base64_decode($filteredData[1]);
$randomName = rand(1000, 99999999999);
//Create the image
$fp = fopen('user/'.$randomName.'.png', 'w');
fwrite($fp, $unencoded);
//convert image from png to jpg
$image = imagecreatefrompng('user/'.$randomName.'.png');
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
unlink($fp);
ive tried it with
unlink($image);
unlink($_SERVER['DOCUMENT_ROOT'] . "/user/.$randomName.'.png'");
imagedestroy($fp);
imagedestroy($image);
Use the function unlink() but passing the file name to it instead of the file handler.
So from your example it would be:
EDIT: You might need to close the file first:
fclose( $fp );
unlink( 'user/'.$randomName.'.png' );
as far as i understand all you need is:
$data = base64_decode( $_POST['imgBase64']);
// image resource from your string
$image = imagecreatefromstring($data);
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);

Categories