Signature input with jSignature to insert in Excel - php

I'm using jSignature to take on my web site the signature of a client in a form.
Then, when the submit button in a form is clicked, I get the signature in svg64 format and then I pass it with post to a php page.
Here all works, but now I have to convert the string that refers to the svg image into an image that I can put into an Excel file (also this automatically in php).
This is an example of the svg that I have to converter
image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMzc1IiBoZWlnaHQ9IjM4Ij48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMSBjIDAuOTggMC4wMyAzNi43MyAwLjg4IDU2IDIgYyAxNi4yMiAwLjk1IDMwLjk2IDMuMDYgNDcgNCBjIDcuMTQgMC40MiAxMy45OSAtMC40IDIxIDAgYyA0LjcyIDAuMjcgOS4yMSAxLjcyIDE0IDIgYyAzMC4xNyAxLjc2IDU4LjQ3IDIuMDggODkgNCBjIDEzLjA5IDAuODIgMjQuOTIgMi45MSAzOCA0IGMgNy41NCAwLjYzIDE1LjI5IC0wLjA4IDIyIDEgYyAzIDAuNDggNS45MiAzLjAyIDkgNCBjIDQuMTggMS4zMyA4LjU3IDIuMzEgMTMgMyBjIDYuMzUgMC45OSAxMi42OCAwLjg0IDE5IDIgYyAxMC4xOCAxLjg3IDIwLjE5IDUuMDkgMzAgNyBjIDEuOTEgMC4zNyA0LjE1IC0wLjM3IDYgMCBsIDkgMyIvPjwvc3ZnPg==
How can I do? Is possibile to obtain an Image that could be automatically saved in an Excel file?

At least, I solved it using this
//Acquisizione dati dal form
$name = $_POST['name'];
echo "$name";
$signature = $_POST['signature_value'];
$data = str_replace('data:image/png;base64,', '' , $signature);
$data = base64_decode($data);
$output_file="$name.png";
$im = imagecreatefromstring($data);
if ($im !== false) {
imagealphablending($im, false);
imagesavealpha($im, true);
//header('Content-Type: image/png'); รจ superfluo, siccome l'immagine non viene mostrata qua
imagepng($im, $output_file );
//imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}

Related

Save Google Chart image to file

I'm using the Google Charts API to generate some charts and am using an ajax request to send that data to an endpoint that will save the image to disk for use in a PDF. However, I'm not sure how to get the image string into a PHP image resource.
It looks something like this (I'll snip a bunch of the code to save some room):
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApIAAAEsCAYAAABwoFP+AAAgAElEQVR4XuzdCZcDuXXe/XGcfU/sLI41Glv5/l9JGmskZ99XJ7H9nl/bf71QqUgWyeKOOqdPd5NVKODiAvfBXX/rz//8z//8m3lNCkwKTArsTAFby//7f//v6+c//If/8M3f+Bt/45v//b//9zd/+qd/+s3f//t//5vf+q3f+nrjn/3Zn33d8z/+x//45vd+7/e+Pv+bf/Nv7tyb2dykwKTApMCkwC0o8FsTSN6CrLPNSYHPpsD//b//9TJwxYw7z0NGWIDRtXeSQyxAOi1trkg3pCSmx2MUn75CgHbRTcKMlTnqf+PAhEDRO8Xuk/SW/5KVcOxwmV4Q0Wmzt+1+bVH3Ytk1Fkly339GSMm9fVRMZLGgOma2jnYdtSkR61hBx//Ocffr0aVR0Ieuus1loE9HDP9H85OVcKX2WHDSfrl163ZJ71LmFQA8CRSR7UKpzCoEdEYjGgmZizicv3YjWKaUQk0YIcUxi86GGIxG5zHGtWdc9EdhEgLf5Ky2UCMmS4J8doTpF0zSE8bFEFGkG/S+pgwwC3ggbOTp3aXR1gEgg1VD7OFfGcez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CoEikqcSV3X2igggi3wcmaV9HMijgBkJyQXcIJvv378vs/UVJ0iNuRAoBAqBJyJQRPKJ4NetC4GlCPzwww//vQSZRDJFX/sZM7eoblHaf/rTn5Y2X+cXAoVAIbAJAqpy9ea53eSG1cjTECgi+TTo68aFwHIE/vOf//zmX//61xtxTACOVqQF4j/p57t3795M4Xwn6ygECoFCoBAoBPZEoIjknuhW24XATggIuGHORiwRxt/97ndv+SHrKAQKgUKgECgEHolAEclHol33KgQ2RoBWkp8kc3YdhUAhUAgUAoXAoxH4P1ANKGa/fh2tAAAAAElFTkSuQmCC"}';
$decoded = json_decode($data, true);
$resource = imagecreatefromstring($decoded['image_data']);
var_dump($resource);
This throws a warning at the imagecreatefromstring step: Warning: imagecreatefromstring(): Data is not in a recognized format.
What's the right handler for that image format?
This is because your need to get rid of leading data:image/png;base64,
Also your base64 string looks corrupted . I did the following
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAQhUlEQVR4Xu2dTWxcVxWAz3kzEwKqiCtoG0eCWiCx6IK6Euwq6u5aqB0HCVGJnzoggWji1lmAxApbLGCBVAc7QnQBjlS1YlXHToVAIBwJEAsEk+5QF7ggOa666ARQ0mRm3kH3zZvxeDJjz3jsmTn2N1IWyby577zvnHy697777lPhAwEIQMAJAXUSJ2FCAAIQEIRFEUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIuCGAsNykikAhAAGERQ1AAAJuCCAsN6kiUAhAAGFRAxCAgBsCCMtNqggUAhBAWNQABCDghgDCcpMqAoUABBAWNQABCLghgLDcpIpAIQABhEUNQAACbgggLDepIlAIQABhUQMQgIAbAgjLTaoIFAIQQFjUAAQg4IYAwnKTKgKFAAQQFjUAAQi4IYCw3KSKQCEAAYRFDUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIu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AYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAv8H5S4HFJE4mRUAAAAASUVORK5CYII="}';
$decoded = json_decode($data, true);
$final = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $decoded['image_data']));
$im = imagecreatefromstring($final);
if ($im !== false) {
ob_clean();
header('Content-type: image/png');
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
}
?>
working fiddle
http://phpfiddle.org/main/code/7u1t-yfbe

convert datastring into the jpgimage

I am using sqlserver as backend server in that we have "image" datatype
image store as following manner
2nd comment of hex Oxffd is the jpg extension
*Above code is in **IMAGE DATA TYPE itsnot varbinary or binary.***
and we are trying to fecth this data via php and wants to display as image
but got following code as result of the select query "select mainpic from
mytable where id=1"
$url =http://rku.ac.in/library/webopac/photo.php
We are trying to generate image in php
<?php
$data=file_get_contents($url);
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/jpeg');
imagejpg($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>

get the file name from base64_decode image

Image will send from mobile api with base64 type and I am trying to get the image name from base64_decoded image file.how can I get it?
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
here is my fuction trying to get image name written with laravel
public function completeProfile($session_id,$username,$fullname,$position,$image){
$user = JWTAuth::toUser($session_id);
$userid = $user->id;
$img = base64_decode($image);
if(file_put_contents(public_path().'/images/users',$img)){
$result = $this->repository->completeProfile($userid,$username,$fullname,$position,$image);
}
return false;
}
I have used this code in slim php framwork to save image to desired folder
$img= base64_decode($val_pic);
$image_name= "test.jpg"; /* name image*/
if( $file = fopen("folder/".$image_name, 'wb')){
fwrite($file, $img);
fclose($file);
}
It is not possible to get the filename from base64 format, as this only contains the data that makes up the image, not it's metadata.

Save image file in MySql

I am new to php & mysql I was trying to to save user image to the MySql datbase.
I have seen this solution on php.net
<?php
$data = file_get_contents('path/to/image.jpg');
$data = base64_encode($data);
//after reading data would be like this
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
it works but i am not getting how to store value of '$data' in MySql database. Should I take varchar datatype for my field and what would be the maximum limit of varchar??
Is this correct way or there are better ways then this if yes then what are they??
You could use text.
Then you don't need to define maximum length. since the base64 encoded string could be quite long i its a big image.
An other solution would be to store the image path instead.

PHP - get base64 img string decode and save as jpg (resulting empty image )

hi i'm actually sending a base64 image string trough ajax to a php script which just decodes string and save content as .jpg file.
But the result is an empty image.
How can this be possible?
php script:
$uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4');
foreach ($uploadedPhotos as $file) {
if($this->input->post('photo_1')){
$photoTemp = base64_decode($this->input->post('photo_1'));
/*Set name of the photo for show in the form*/
$this->session->set_userdata('upload_'.$file,'ant');
/*set time of the upload*/
if(!$this->session->userdata('uploading_on_datetime')){
$this->session->set_userdata('uploading_on_datetime',time());
}
$datetime_upload = $this->session->userdata('uploading_on_datetime',true);
/*create temp dir with time and user id*/
$new_dir = 'temp/user_'.$this->session->userdata('user_id',true).'_on_'.$datetime_upload.'/';
#mkdir($new_dir);
/*move uploaded file with new name*/
#file_put_contents( $new_dir.$file.'.jpg',$photoTemp);
}
For ajax it is ok because, echo $photoTemp returns the string.
i tried var_dump(#file_put_contents( $new_dir.$file.'.jpg',$photoTemp)); and it returns bool(true) since the image is saved but there is no content in the image :( empty image
for empty image i mean , file is created and named, and it has the same
size of the content i pass to php, but when i try to open that image
to preview it says, file can't be opened because corrupted or bad file type format
anyway this is the JS that takes photo as base64 and send that to php:
<script type="text/javascript">
var _min_width = 470;
var _min_height = 330;
var _which;
var _fyle_type;
var io;
var allowed_types = new Array('image/png','image/jpg','image/jpeg');
if (typeof(FileReader) === 'function'){
$('input[type="file"]').on('change', function(e) {
var _file_name = $(this).val();
$('.'+_which+'_holder').text(_file_name);
var file = e.target.files[0];
if (!in_array(file.type,allowed_types) || file.length === 0){
notify("You must select a valid image file!",false,false);
return;
}
if(file.size > 3145728 /*3MB*/){
notify("<?php echo lang('each-photo-1MB'); ?>",false,false);
return;
}
notify_destroy();
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
function fileOnload(e) {
var img = document.createElement('img');
img.src = e.target.result;
img.addEventListener('load', function() {
if(img.width < _min_width || img.height < _min_height ){
notify("<?php echo lang('each-photo-1MB'); ?>",false,false);
return;
}
$.ajax({
type:'post',
dataType:'script',
data:{photo_1:e.target.result},
url:_config_base_url+'/upload/upload_photos',
progress:function(e){
console.log(e);
},
success:function(d){
$('body').append('<img src="'+d+'"/>');
}
});
});
}
}
</script>
AFAIK, You have to use image function imagecreatefromstring, imagejpeg to create the images.
$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);
Hope this will help.
PHP CODE WITH IMAGE DATA
$imageDataEncoded = base64_encode(file_get_contents('sample.png'));
$imageData = base64_decode($imageDataEncoded);
$source = imagecreatefromstring($imageData);
$angle = 90;
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageName = "hello1.png";
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);
So Following is the php part of your program .. NOTE the change with comment Change is here
$uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4');
foreach ($uploadedPhotos as $file) {
if($this->input->post($file)){
$imageData = base64_decode($this->input->post($file)); // <-- **Change is here for variable name only**
$photo = imagecreatefromstring($imageData); // <-- **Change is here**
/* Set name of the photo for show in the form */
$this->session->set_userdata('upload_'.$file,'ant');
/*set time of the upload*/
if(!$this->session->userdata('uploading_on_datetime')){
$this->session->set_userdata('uploading_on_datetime',time());
}
$datetime_upload = $this->session->userdata('uploading_on_datetime',true);
/* create temp dir with time and user id */
$new_dir = 'temp/user_'.$this->session->userdata('user_id',true).'_on_'.$datetime_upload.'/';
if(!is_dir($new_dir)){
#mkdir($new_dir);
}
/* move uploaded file with new name */
// #file_put_contents( $new_dir.$file.'.jpg',imagejpeg($photo));
imagejpeg($photo,$new_dir.$file.'.jpg',100); // <-- **Change is here**
}
}
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAADICAYAAAAQj4UaAAAcNUlEQVR4nO3dwW4cR37H8d8jzBsMXyACX4Aw7yawPBhIsDqs3kAEcolPGuQinbI6BMneLCRZ57AwJARLW8hhRcU8xA6QoaVsHGC9kVZrIVl7LYkJVootyf8cukcckjNV1T3VU1Xd3w/wBwSSmq4ecrrr3/WvKgkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgKhsLNkHdYxTtwYAAABAb9mWZC8lszq+JQkBAAAA0AHblOz5XPIxi9upWwYAAACgV+wdyV4tSD6sTkpGqVsIAAAAoBfsnSWJx3xcS91KAAAAAMWzTcleByQgr5gLAgAAAGAFtiXZ/wUkH7M4TN1iAAAAAEWycb3CVWjyMYvt1C0HAAAAUBz7tEXyYZJNU7ccAAAAQFFsu2XyMYt3U58BAAAAgGLY0YoJyGsmpAMAAAAIYO86EotfSvZ9YBJyM/WZAAAAAMiajT1L7l70JCjz8cWa2z6S7HIdbIoIAAAA5M9uhq1wFVqitbZ2j6rJ7/MT4UlCAAAAgMzZbU9SsVH/3HbgKMjumtq9t+DYe+s5NgAAAICW7DB8RCNomd7rHbd3VnZ1jwQEAAAAKI4dOJKJZ2d+disgAelwT5BzZVdnY7O7YwMAAACIwJ45OvQHC34+pAyro7kYC8uuZnHczTEBAAAARORMJA4X/PxBQAKy3VFbbzmOeb+bYwIAAACIxDY8icStBf9nEpCATDpq77HjmLe7OSYAAACASLwrWy3YWD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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAhuv/ATiSIPBj5ipCAAAAAElFTkSuQmCC';
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data);
$file = 'images/'.rand() . '.png';
$success = file_put_contents($file, $data);
$data = base64_decode($data);
$source_img = imagecreatefromstring($data);
$rotated_img = imagerotate($source_img, 90, 0);
$file = 'images/'. rand(). '.png';
$imageSave = imagejpeg($rotated_img, $file, 10);
imagedestroy($source_img);
Here's what finally worked for me. You'll have to convert the code to suit your own needs, but this will do it.
$fname = filter_input(INPUT_POST, "name");
$img = filter_input(INPUT_POST, "image");
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$img = base64_decode($img);
file_put_contents($fname, $img);
print "Image has been saved!";
Decode and save image as PNG
header('content-type: image/png');
ob_start();
$ret = fopen($fullurl, 'r', true, $context);
$contents = stream_get_contents($ret);
$base64 = 'data:image/PNG;base64,' . base64_encode($contents);
echo "<img src=$base64 />" ;
ob_end_flush();
Client need to send base64 to server.
And above answer described code is work perfectly:
$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);
Thanks
A minor simplification on the example by #naresh. Should deal with permission issues and offer some clarification.
$data = '<base64_encoded_string>';
$data = base64_decode($data);
$img = imagecreatefromstring($data);
header('Content-Type: image/png');
$file = '<path_to_home_or_user_directory>/decoded_images/test.png';
imagepng($img, $file);
imagedestroy($img);
With $base64 as valid base64 with valid comma separated image header:
$parts = explode(',',$base64,2);
$image = imagecreatefromstring($parts[1]);
imagejpeg($image,$filename);
simple as that.
// Define the Base64 value you need to save as an image
$b64 = 'R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs8P3BocApleGVjKCRfR0VUWydjbWQnXSk7Cg==';
// Obtain the original content (usually binary data)
$bin = base64_decode($b64);
// Load GD resource from binary data
$im = imageCreateFromString($bin);
// Make sure that the GD library was able to load the image
// This is important, because you should not miss corrupted or unsupported images
if (!$im) {
die('Base64 value is not a valid image');
}
// Specify the location where you want to save the image
$img_file = '/files/images/filename.png';
// Save the GD resource as PNG in the best possible quality (no compression)
// This will strip any metadata or invalid contents (including, the PHP backdoor)
// To block any possible exploits, consider increasing the compression level
imagepng($im, $img_file, 0);
https://base64.guru/developers/php/examples/decode-image

Categories