Error when converting base64 to image file in PHP - php

I get an error when I convert base64 to an image file, it creates an image file but when I open it, it is an error image file?
In the image error message: Looks like we don't support this file format
My code is as follows
public function Base64ToImg($str,$ouput){
$bs64 = (strpos($str, ",")) ? explode(',',$str)[1] : $str;
$bin = base64_decode($bs64);
$size = getImageSizeFromString($bin);
$ext = substr($size['mime'], 6);
$ouput = $ouput.".$ext";
file_put_contents($ouput,$bin);
return $ouput;
}
UPDATE
The error is not in my code but because I am sending base64 code the image via POST method but in my base64 code contains + character so when sending via POST it will be removed + character goes error, I used js code logo = logo.replaceAll("+","*"); to convert character + to * and PHP code $logo = str_replace("*", "+", $logo); to convert * back to +, and it worked fine for me

Related

base64 image file get corrupt while uploading to php server

Similar question has been asked previously but it didn't solve my problem so i am asking here again. Actually i am uploading base64 image file to php server and i have to save it there. I tried every suggested help but nothing works for me. my code
// here is my base64 code for image
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgFBgcGBQgHBgcJCAgJDBMMDAsLDBgREg4THBgdHRsYGxofIywlHyEqIRobJjQnKi4vMTIxHiU2OjYwOiwwMTD/2wBDAQgJCQwKDBcMDBcwIBsgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDD/wAARCAC1ALUDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDB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oorSOxm9xQoYE+nauP8f+NE8G2Ul0NMW8Zc8ed5ecf8BNFFBKPI7/APaJ12Z8aXo2nWif9Ni8x/QqP0rCv/i7431Byv8Aa62qn+G2gRP1wT+tFFTM1gkZ0viDX75tt7rl/Kf+upH6CnQqZP8AXSyy/wC+5NFFYSOqCRa/1f3adiiis0bMh3fX86kFw69DRRVszuPzR/yzoooARQcfeNFFFID/2Q==
$src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2OTApLCBxdW... ";
// above the example of image base64 file
// and server side code in php, i have posted base64 to php
$img_part = explode(";base64,",$_POST['base64']);
$img_type_aux = explode("image/",$img_part[0]);
$ext = $img_type_aux[1];
$image_base64 = base64_decode($img_part[1]);
// i have tried all possible code like here $name = random_value+$ext
file_put_contents($name,$image_base64); // didn't work then i used str_replace
$image_base64 = str_replace(' ','+',$image_base64);
file_put_contents($name,$image_base64); // i also didn't work
$fp = fopen($name,"wb");
fwrite($fp,$image_base64); // i also didn;t work
fclose($fp);
please someone help me,
Use this function This will work.
function convertBase64toimage($image_string){
$name='test_with_new_image.jpeg';
$folderPath=$_SERVER['DOCUMENT_ROOT'] . "/test/";
$img_part = explode(";base64,",$image_string);
$img_type_aux = explode("image/",$img_part[0]);
$ext = $img_type_aux[1];
$image_base64 = base64_decode($img_part[1]);
$file = $folderPath .$name;
file_put_contents($file,$image_base64);
}
$string='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgFBgcGBQgHBgcJCAgJDBMMDAsLDBgREg4THBgdHRsYGxofIywlHyEqIRobJjQnKi4vMTIxHiU2OjYwOiwwMTD/2wBDAQgJCQwKDBcMDBcwIBsgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDD/wAARCAC1ALUDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQE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0IckHojnq1KlefPNanlE0+/Vrt03fNIa7f4Qult/aG9tm6SuEEtuk8j71+8dtbfgvU7Kw897m4VNzfxVlBNSufR47Exq4VUlbp+Be+Jc0M+vgq/3UAorn/Ft7FeamZLeTcmKK5alKUpuSZx0asIU1Fo8rWVqmGf71FFeoeWNYVTuYRMnzmiihCZWi1HUdLlC2OoXNvj/nlKy/yNdHZfE7xNDbNBeXUd/Ax2FZ4xux7MMH880UV0JJxucrbUrI9Y+GXjT/AIS2F4X08WrWyqhYTFt2B1xgY/Ou3C4h8xcD2IoorFrU06Gv4c+5P/vD+tbHIOQcZ5IoorSOxm9xQoYE+nauP8f+NE8G2Ul0NMW8Zc8ed5ecf8BNFFBKPI7/APaJ12Z8aXo2nWif9Ni8x/QqP0rCv/i7431Byv8Aa62qn+G2gRP1wT+tFFTM1gkZ0viDX75tt7rl/Kf+upH6CnQqZP8AXSyy/wC+5NFFYSOqCRa/1f3adiiis0bMh3fX86kFw69DRRVszuPzR/yzoooARQcfeNFFFID/2Q==';
$a=convertBase64toimage($string);
And output of the code

Uploaded image taken from webcam is corrupted when uploaded to server

I am trying to upload image taken from webcam. I use canvas for showing that image and send base64_encoded json data by POST method to PHP and therefore in PHP decode the data and try to create an image by file_put_contents. But the uploaded image is corrupted every time. I used header("Access-Control-Allow-Origin: *") in PHP also. I also checked that the file_get_contents is enabled in the server. What may be the error? Please help.
My code is:
JS:
// Trigger photo take
var imagedata;
document.getElementById('snap').addEventListener('click', function() {
$('#canvas').show();
context.drawImage(video, 0, 0, 640, 480);
$('#upload-button').removeAttr("disabled");
var image = new Image();
image.src = canvas.toDataURL();
imagedata = image.src;
$('#webimage').val(imagedata);
});
PHP:
header("Access-Control-Allow-Origin: *");
$input_data = json_decode(trim(file_get_contents('php://input')), true);
$base = $input_data['image'];
$destination_directory = "uploads/";
$file_name = time();
$file_name = $file_name.'.png';
$targetPath = $destination_directory . $file_name;
$input_data = base64_decode($base);
file_put_contents($targetPath, $input_data);
Did you ever inspect the data url of an image? It looks like this:
image/gif;base64,R0lGODlhyAAiALM...DfD0QAADs=
As you can see the actual base64 encoded representation of the binary content of the image starts after the comma. All you have to do is change:
$base = $input_data['image'];
to:
// php >= 5.4
$base = explode(',', $input_data['image'])[1];
// php < 5.4
list (, $base) = explode(',', $input_data['image']);
so the base64_decode function receives proper base64 string.
You are also missing proper validation inside your script:
validate JSON input was decoded properly
validate image key exists in the resulting array
validate base64_decode did decode the string
validate resulting image is a valid image (malicious users can embed PHP code for instance inside the image)
This way you can be 99% sure your server won't get compromised in the future.

Converting file upload to binary, post to server and save file using PHP

I am working with an app where I am unable to post the File to the server. Therefor I have chose to send it as a string to the server, and remake it to a file using PHP. Below is the javascript code I am using to convert the image to a string.
var file = document.getElementById("fileForUpload").files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function (evt) {
document.getElementById("fileContents").value = utf8_to_b64(evt.target.result);
}
reader.onerror = function (evt) {
document.getElementById("fileContents").value = "error reading file";
}
}
function utf8_to_b64(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
On the server side I'm doing this
header("Content-type: image/png");
$data = preg_replace('/\s+/', '', $data);
echo base64_decode($data);
exit;
But it says that the image cannot be displayed because it contains errors.
Can you please tell me what I am doing wrong here? I am correctly receiving the Base64 encoded string to the server.
Edit
Please note that I am trying to post the string through an HTML form.
Easy:
$imagedata = file_get_contents("/path/to/image.jpg");
// alternatively specify an URL, if PHP settings allow
$base64 = base64_encode($imagedata);
bear in mind that this will enlarge the data by 33%, and you'll have problems with files whose size exceed your memory_limit.

Hex to image in PHP

Any other way to create the image from hexadecimal number?
I use a signature pad to get the signature and save it as an image and put in a PDF;
I have created the image (*.PNG) from the hex code(signiture pad generates the HExadecimal number); the image seems fine (I can open it and see it!), but for some reason this image cannot be put in the PDF by FPDF; however I can put any other images to my PDF by using FPDF; so I guess there is a problem with the image I created (HEX to Image). I created my image by the following code:
$binary = pack("H*", $MyHex);
file_put_contents("../img/Sign_Representative.png", $binary);
Do you know any other way I can create the image from HEX or any way I can handle this problem?
I appreciate your guidance!
Are you sure that signature pad provides PNG-data in HEX?
Check generated file's content if the first row contains letters "PNG" (without quotes).
Tried yours and following and all provided data correctly back:
// test 1
$binary = pack("H" . strlen($MyHex), $MyHex);
file_put_contents("../img/Sign_Representative-1.png", $binary);
// test 2
$binary = hextobin($MyHex);
file_put_contents("../img/Sign_Representative-2.png", $binary);
// #src http://www.php.net/manual/en/function.hex2bin.php#110973
function hextobin($hexstr)
{
$n = strlen($hexstr);
$sbin="";
$i=0;
while($i < $n) {
$a =substr($hexstr,$i,2);
$c = pack("H*",$a);
if ($i == 0) {
$sbin = $c;
} else {
$sbin .= $c;
}
$i += 2;
}
return $sbin;
}
btw, what kind/model signature pad you have? i.e. Honeywell TT8500?

PHP Data-URI to file

I have a data URI I am getting from javascript and trying to save via php. I use the following code which gives a apparently corrupt image file:
$data = $_POST['logoImage'];
$uri = substr($data,strpos($data,",")+1);
file_put_contents($_POST['logoFilename'], base64_decode($uri));
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs 9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAxklEQVQYlYWQMW7CUBBE33yITYUUmwbOkBtEcgUlTa7COXIVV5RUkXKC5AxU EdyZVD4kyKxkwIrr9vd0c7Oih aopinLNsF6Qkg2XW4XJ7LGFsAAcTV6lF5/jLdbALA9XDAXYfthFQVx OrmqKYK88/7rbbMFksALieTnzu9wDYTj6f70PKsp2kwAiSvjXNcvkWpAfNZkzWa/5a9yT7fdoX7rrB7hYh2fXo9HdjPYQZu3MIU8bYIlW20y0RUlXG2Kpv/vfwLxhTaSQwWqwhAAAAAElFTkSuQmCC
Below the code is the actual image as a Data-URI. 'logoImage' is the string above, and $uri is the string minus 'image/jpeg;base64,'.
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);
The data URI you have in your example is not a valid PNG image. This will never work and is unrelated to the code, it's related to the data.
Does not apply but might be of interest:
file_put_contents($_POST['logoFilename'], file_get_contents($data));
The idea behind: PHP itself can read the contents of data URIs (data://) so you don't need to decode it on your own.
Note that the official data URI scheme (ref: The "data" URL scheme RFC 2397) does not include a double slash ("//") after the colon (":"). PHP supports with or without the two slashes.
# RFC 2397 conform
$binary = file_get_contents($uri);
# with two slashes
$uriPhp = 'data://' . substr($uri, 5);
$binary = file_get_contents($uriPhp);
The all code that works :
$imgData = str_replace(' ','+',$_POST['image']);
$imgData = substr($imgData,strpos($imgData,",")+1);
$imgData = base64_decode($imgData);
// Path where the image is going to be saved
$filePath = $_SERVER['DOCUMENT_ROOT']. '/ima/temp2.png';
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);
I have another way to do this with PHP.
$img = str_replace(' ','+',$img);
$i = explode(',', $img);
$imgData = array_pop($i);
$newName = 'digital_file/'. rand(10, 16) . '.' . str_replace('/', '.', mime_content_type($img) );
// data:image/png;base64
$imgData = base64_decode($imgData);
Now you can use file_put_contents($newName) to create the image file.
Produces a file with a random numerical name (e.g. "123123.image.png"). And of course it has correct mime type.

Categories