PHP Data-URI to file
A quick look at the PHP manual yields the following:
If you want to save data that is derived from a Javascript
canvas.toDataURL()
function, you have to convert blanks into plusses.
If you do not do that, the decoded data is corrupted:$encodedData = str_replace(' ','+',$encodedData);
$decodedData = base64_decode($encodedData);
dataurl to image for download in php
Thanks for all. but I got answer using,
file_put_contents();
But thing, i dont know how to use. Finally i got it from this Answer.
Answer is,
$data = 'data:image/png;base64,AAAFBfj42Pj4';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('/tmp/image.png', $data);
But still i'm waiting for download button with option of image/pdf format.
Convert Image Data URI to Image PHP
$image_content = base64_decode(str_replace("data:image/png;base64,","",$imageURL)); // remove "data:image/png;base64,"
$tempfile = tmpfile(); // create temporary file
fwrite($tempfile, $image_content); // fill data to temporary file
$metaDatas = stream_get_meta_data($tempfile);
$tmpFilename = $metaDatas['uri'];
Now you can use that image into fpdf like:
$pdf->Image($tmpFilename,null,null,0,0);
Or you can specify image type by adding image type parameter like this:
$pdf->Image($tmpFilename,null,null,0,0,'PNG');
Please check to http://www.fpdf.org/en/doc/image.htm
PHP Data- URI to Image
Given that I believe PHP now supports (>5.4.17 ) "proper" data URIs (i.e. without the double-slash necessary in older versions) I'd probably just try this:
fwrite($fp, file_get_contents($_POST['strDataURI']);
...seems to work okay in PHP 5.4.17 using a test script I just wrote using a data uri generated from this online tool.
How to convert data URI to buffer (string with binary)?
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();
data URI to image png file
Looks like you perform substr()
on the wrong string.
$uri = substr($data,strpos($encodedData,",")+1);
which should be:
$uri = substr($encodedData, strpos($encodedData, ',') + 1);
fetch image and convert it to data-uri format
The data URI is composed by the literal data:
, then the media type (defaults to text-plain if omitted), ;base64
if you are encoding (which you should, if you have binary data as an image), and then the data itself.
Linky to some docs.
You could do it like this:
// Your image
$image = 'crazy_cat.jpg';
$myvar = file_get_contents($image);
function srcData($image)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
// reads your image's data and convert it to base64
$data = base64_encode($image);
// Create the image's SRC: "data:{mime};base64,{data};"
return 'data: ' . finfo_buffer($finfo, $image) . ';base64,' . $data;
}
echo "<img src='" . srcData($myvar) . "'/>";
Ta da!
If you are downloading the image before serving it like data
, you'd have to do it before calling this function, and pass the function the buffer containing the file.
For bonus points: the "how to use curl" can be summarized thus:
$ch = curl_init('http://placekitten.com.s3.amazonaws.com/homepage-samples/408/287.jpg');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$myvar = curl_exec($ch);
curl_close($ch);
And you'll have your image in $myvar
, ready to pass to srcData()
.
Related Topics
How to Change Maximum Number of Post Variable in PHP
PHP Pdo Prepared Statement - MySQL Like Query
Running Command-Line Application from PHP as Specific User
Fatal Error: Cannot Use Object of Type MySQLi_Result
How to Get the Current Page'S Full Url on a Windows/Iis Server
Can You Store a Function in a PHP Array
PHP - Merging Two Arrays into One Array (Also Remove Duplicates)
When to Use Strtr VS Str_Replace
How to Import a .SQL File in MySQL Database Using PHP
How to Override the Path of PHP to Use the Mamp Path
How to Get Last Key in an Array
PHP: If Internet Explorer 6, 7, 8 , or 9
How to Get Url of Current Page in PHP