How to convert data URI to buffer (string with binary)? - php

I am using react to edit an image and send its final data using mycanvas.toDataURL() to php.
The API I am using says, instead of an image, I could although
[...] upload an image from a buffer (a string with binary)
Their example looks like this:
$sourceData = file_get_contents("example.jpg");
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();
Like I said, instead of an uploaded image, I have got a data URI looking like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABDgAAAQ4C2dj892d2dh98a2d...
How can I convert this data URI to a valid buffer to be used as $sourceData?

You can try converting the string to an image file and then send that file, you can use a code like this to do that:
function base64_to_image($base64_string, $output_file) {
$ifp = fopen( $output_file, 'wb' );
$data = explode( ',', $base64_string );
fwrite( $ifp, base64_decode( $data[ 1 ] ) );
fclose( $ifp );
return $output_file;
}
So, just call it like :
$file = base64_to_image('data:image/png;base64,iVBORw0...', 'myImage.png');
$sourceData = file_get_contents($file);
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();
Edit:
Alternatively(preferred) you can just use the decoded string as is instead of writing to a file and reading it. Like this:
function base64_to_image($base64_string) {
$data = explode( ',', $base64_string );
return base64_decode($data[ 1 ]);
}
And you can call it like this:
$sourceData = base64_to_image('data:image/png;base64,iVBORw0...');
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();

Related

PHP str_getcsv to JSON

I am trying to use str_getcsv to convert from CSV to JSON.
I'm nearly there the problem I am having is with formatting the JSON,
I need the format to be like this to use in DataTables -
{"data":[["Debra Brown"]
But it is
{"data":["[[\"Debra Brown\"]
My code
$csv = file_get_contents($targetPath);
$csvArray = array_map("str_getcsv", file($targetPath));
$csvToJson = json_encode($csvArray);
print_r($csv);
$csvJsonArray = array();
$csvJsonArray['data'][] = $csvToJson;
echo json_encode($csvJsonArray);
My CSV - CSV
You re-encode already encoded string.
Add data key before encoding data:
$csvArray = array_map( 'str_getcsv', file( $targetPath ) );
$csvArray = array( 'data' => $csvArray );
$csvToJson = json_encode( $csvArray );
Result:
{"data":[["Debra Brown"],["Jacqueline Garza"], (...) ]}

How to save base64 string as image server-side

I want to save a base64 string as an image (.png or .jpg) on my server.
The string I get looks like this:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQA[...]"
I tried this:
$data = $_POST['form_new_data'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents("/userImgs/pic.jpg",$data);
but it is not working (maybe because the base64 string is a jpeg?).. Please help..
try this
$data = $_POST['form_new_data'];
file_put_contents('img.jpg', base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data)));

I want to decode base64 encoded data which is image/gif? How can I do that in php?

I am using the below code from the library
https://github.com/antimatter15/jsgif.
I tried to convert the code given below to a gif image file but could not achieve it.
var encoder = new GIFEncoder();
encoder.setRepeat(0);
encoder.start();
var st = '|100:167.1,D,Pencil,3,#000|100:167.1,U|100:167.1,D|102:163.1,M|135:125.1|206:97.1|289:99.1|323:134.1|3\
16:252.1|205:331.1|234:260.1|504:102.1|581:92.1|631:95.1|635:132.1|512:209.1,U|385:29.1,D,,,#66c|384:29.1,M|384:40.1|378:84.\
1|365:168.1|360:215.1|354:269.1|354:300.1|357:313.1|405:319.1|450:314.1|507:291.1|546:260.1|534:223.1|487:171.1|418:119.1|29\
0:131.1|220:201.1|168:284.1|157:339.1|170:353.1|256:308.1|348:212.1|390:118.1|399:77.1|397:68.1|298:82.1|176:168.1|90:265.1|\
70:290.1|70:292.1|250:150.1|465:14.1|594:-42.9|606:-39.9|550:29.1|442:170.1|335:322.1|258:430.1|258:430.1,U';
var frames_array = st.split('|');
$.each(frames_array,function(index,value) {
encoder.addFrame(value);
});
encoder.finish();
var binary_gif = encoder.stream().getData();
var data_url = 'data:image/gif;base64,'+encode64(binary_gif);
What I get for data_url is as given below
data:image/gif;base64,fDEwMDoxNjcuMSxELFBlbmNpbCwzLCMwMDB8MTAwOjE2Ny4xLFV8MTAwOjE2Ny4xLER8MTAyOjE2My4xLE18MTM1OjEyNS4xfDIwNjo5Ny4xfDI4OTo5OS4xfDMyMzoxMzQuMXwzMTY6MjUyLjF8MjA1OjMzMS4xfDIzNDoyNjAuMXw1MDQ6MTAyLjF8NTgxOjkyLjF8NjMxOjk1LjF8NjM1OjEzMi4xfDUxMjoyMDkuMSxVfDM4NToyOS4xLEQsLCwjNjZjfDM4NDoyOS4xLE18Mzg0OjQwLjF8Mzc4Ojg0LjF8MzY1OjE2OC4xfDM2MDoyMTUuMXwzNTQ6MjY5LjF8MzU0OjMwMC4xfDM1NzozMTMuMXw0MDU6MzE5LjF8NDUwOjMxNC4xfDUwNzoyOTEuMXw1NDY6MjYwLjF8NTM0OjIyMy4xfDQ4NzoxNzEuMXw0MTg6MTE5LjF8MjkwOjEzMS4xfDIyMDoyMDEuMXwxNjg6Mjg0LjF8MTU3OjMzOS4xfDE3MDozNTMuMXwyNTY6MzA4LjF8MzQ4OjIxMi4xfDM5MDoxMTguMXwzOTk6NzcuMXwzOTc6NjguMXwyOTg6ODIuMXwxNzY6MTY4LjF8OTA6MjY1LjF8NzA6MjkwLjF8NzA6MjkyLjF8MjUwOjE1MC4xfDQ2NToxNC4xfDU5NDotNDIuOXw2MDY6LTM5Ljl8NTUwOjI5LjF8NDQyOjE3MC4xfDMzNTozMjIuMXwyNTg6NDMwLjF8MjU4OjQzMC4xLFU=‏
I need to convert this encoded code to an gif image file. How can I achieve it further
try this , may this will help you
<?php
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
?>
Use this (PHP pseudo-code): http://php.net/manual/en/function.imagecreatefromstring.php
function imagegif_from_base64($base64)
{
return imagecreatefromstring(base64_decode(substr($base64, 22) /* remove the "data:image/gif;base64," string */));
}
related get image from base64 string
AS you are using jsgif library you You can do like this to decode image
$imgstr = 'data:image/gif;base64,fDEwMDoxNjcuMSxELFBlbmNpbCwzLCMwMDB8MTAwOjE2Ny4xLFV8MTAwOjE2Ny4xLER8MTAyOjE2My4xLE18MTM1OjEyNS4xfDIwNjo5Ny4xfDI4OTo5OS4xfDMyMzoxMzQuMXwzMTY6MjUyLjF8MjA1OjMzMS4xfDIzNDoyNjAuMXw1MDQ6MTAyLjF8NTgxOjkyLjF8NjMxOjk1LjF8NjM1OjEzMi4xfDUxMjoyMDkuMSxVfDM4NToyOS4xLEQsLCwjNjZjfDM4NDoyOS4xLE18Mzg0OjQwLjF8Mzc4Ojg0LjF8MzY1OjE2OC4xfDM2MDoyMTUuMXwzNTQ6MjY5LjF8MzU0OjMwMC4xfDM1NzozMTMuMXw0MDU6MzE5LjF8NDUwOjMxNC4xfDUwNzoyOTEuMXw1NDY6MjYwLjF8NTM0OjIyMy4xfDQ4NzoxNzEuMXw0MTg6MTE5LjF8MjkwOjEzMS4xfDIyMDoyMDEuMXwxNjg6Mjg0LjF8MTU3OjMzOS4xfDE3MDozNTMuMXwyNTY6MzA4LjF8MzQ4OjIxMi4xfDM5MDoxMTguMXwzOTk6NzcuMXwzOTc6NjguMXwyOTg6ODIuMXwxNzY6MTY4LjF8OTA6MjY1LjF8NzA6MjkwLjF8NzA6MjkyLjF8MjUwOjE1MC4xfDQ2NToxNC4xfDU5NDotNDIuOXw2MDY6LTM5Ljl8NTUwOjI5LjF8NDQyOjE3MC4xfDMzNTozMjIuMXwyNTg6NDMwLjF8MjU4OjQzMC4xLFU=‏';
$new_data=explode(";",$imgstr);
$type=$new_data[0];
$imgdata=explode(",",$new_data[1]);
$imgstr = base64_decode($imgdata[1]);
// or choose any name instead sample.gif
file_put_contents("sample.gif", base64_decode($imgstr));
If you just wanna use PHP try this:
$str = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAA...';
$data = str_replace('data:image/gif;base64,', '', $str);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data); // base64 decoded image data
$source_img = imagecreatefromstring($data);
$file = uniqid() .'.gif';
$success = file_put_contents($file, $data);
This will save the .gif image in the same folder the php file is located.

Accessing POST parameter with file upload in PHP

Trying to access the POST parameter passed with image uploading. When I print $_POST, the output is as follows:
array (
'%entity' => 'org.apache.http.client.entity.UrlEncodedFormEntity#532d2d84',
)
The PHP code is as follows:
$data = file_get_contents($_FILES['source']['tmp_name']);
$image = imagecreatefromstring( $data );
$ifp = fopen( '1.png', "wb" );
fwrite( $ifp, $data);
fclose( $ifp );
echo '<pre>';
print_r($POST);
User id is sent along with the file in POST request.
How to access the USERID which is passed with the file?
In your output you have listed key => value. You have to use the key part on $_POST array.
Just try with:
$_POST['%entity']

How can I get some text from data uri and assign to an array?

my data
this1
this2
this3
this4
what I want
$keepit[0]='this1';
$keepit[1]='this2';
$keepit[2]='this3';
$keepit[3]='this4';
data uri
data:text/plain;charset=utf-8;base64,dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=
Is it possible to do this?
You can do this with normal file methods using the data:// protocol:
$data = file('data://text/plain;charset=utf-8;base64,dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=');
Alternatively though you can pull out the base64 encoded string and decode it yourself:
$plaintext = base64_decode('dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=');
If the encoded string can be decoded this will work:
$file_path = ''; // change this.
$fp = fopen($file_path, 'rb');
$contents = fread($handle, filesize($file_path));
fclose($fp);
$data_uri = preg_split('/,/', $contents);
$encoded = $data_uri[1];
$decoded = base64_decode($encoded);
var_dump($decoded);

Categories