Forums

signature widget converting base64 image to png

ftpaul 29 Jun, 2018
Is there a tutorial out there on how to convert the base64 image that gets created from the signature widget into a png file so I can have it stored in the database and displayed in emails?
healyhatman 29 Jun, 2018
There's not, but there are a few answers here on the forums and heaps more on google.
GreyHead 30 Jun, 2018
Hi ftpaul,

Please check this FAQ. If you scroll down there is a CFv6 section.

Bob
ftpaul 03 Jul, 2018
I've tried both the Chronoforms v5 and v6 code in that FAQ. The v5 code seems to create a file but it's only 0 bytes. The v6 code in the FAQ doesn't seem to work at all.

Not exactly sure what's wrong here.
ftpaul 03 Jul, 2018
<?php
$data_pieces = explode(",", $this->data[signature9]);
$encoded_image = $data_pieces[1];
$decoded_image = base64_decode($encoded_image);

// set the folder where the images will be saved
$folder = 'images/photos/signatures/';
$path = JPATH_SITE.'/'.$folder;
$url = JURI::root().$folder;
// sets a file name using a date-time prefix to make sure it is unique
$sig_name = date('Ymd-his').'_signature.png';
file_put_contents( $path.$sig_name, $decoded_image);

// add the file info into the form data
return array(
'url' => $url.$sig_name,
'path' => $path.$sig_name
);
// display the image if needed
echo "<img src='{$url}{$sig_name}' />";
// to add the image to an email add this line
// and put {sig_img} in the email template
$form->data['sig_img'] = "<img src='{$url}{$sig_name}' />";
?>
ftpaul 03 Jul, 2018
The code above does nothing.
ftpaul 03 Jul, 2018
<?php
$data_pieces = explode(",", $form->data['signature9']);
$encoded_image = $data_pieces[1];
$decoded_image = base64_decode($encoded_image);

// set the folder where the images will be saved
$folder = 'images/photos/signatures'.$form->form['Form']['title'].'/';
$path = JPATH_SITE.'/'.$folder;
$url = JURI::root().$folder;
// sets a file name using a date-time prefix to make sure it is unique
$sig_name = date('Ymd-his').'_signature.png';
file_put_contents( $path.$sig_name, $decoded_image);

// add the file info to the form data
$form->data['signature9'] = $sig_name;
$form->files['signature9']['name'] = $sig_name;
$form->files['signature9']['link'] = $url.$sig_name;
$form->files['signature9']['path'] = $path.$sig_name;
// display the image if needed
echo "<img src='{$url}{$sig_name}' />";
// to add the image to an email add this line
// and put {sig_img} in the email template
$form->data['sig_img'] = "<img src='{$url}{$sig_name}' />";
?>
ftpaul 03 Jul, 2018
This code creates a 0 byte .png file.
healyhatman 03 Jul, 2018
If you're on V6 it helps if you have the Syntax right.

$this->data("signature9", "not found")
ftpaul 03 Jul, 2018
I'm getting that syntax straight from the FAQ.
ftpaul 03 Jul, 2018
I tried using both the v5 and v6 examples from the FAQ. The v5 code is the one that creates the 0 byte png file whereas the v6 code does absolutely nothing at all.
ftpaul 03 Jul, 2018
But thank you. Switching out the syntax from the v5 example and putting in your code syntax works for v6. The image saves properly now.
ftpaul 03 Jul, 2018
Now I just need to get this image to go into an email. Do I just put ...
"<img src='{$url}{$sig_name}' />";
Or is there a different syntax I'll need for v6?
healyhatman 04 Jul, 2018
Have the PHP code block you're using to create the image return the <img src blahblahblah as a string and in your email use it with {var:php_action_name}
ftpaul 04 Jul, 2018
I'm using this code to generate the png image...
<?php
$data_pieces = explode(",", $this->data("signature9", "not found"));
$encoded_image = $data_pieces[1];
$decoded_image = base64_decode($encoded_image);

// set the folder where the images will be saved
$folder = 'images/photos/signatures'.$form->form['Form']['title'].'/';
$path = JPATH_SITE.'/'.$folder;
$url = JURI::root().$folder;
// sets a file name using a date-time prefix to make sure it is unique
$sig_name = date('Ymd-his').'_signature.png';
file_put_contents( $path.$sig_name, $decoded_image);

$form->data['signature9'] = $sig_name;
$form->files['signature9']['name'] = $sig_name;
$form->files['signature9']['link'] = $url.$sig_name;
$form->files['signature9']['path'] = $path.$sig_name;
// display the image if needed
echo "<img src='{$url}{$sig_name}' />";
// to add the image to an email add this line
// and put {sig_img} in the email template
$form->data['sig_img'] = "<img src='{$url}{$sig_name}' />";

?>
And
"<img src='{$url}{$sig_name}' />";
will echo out the png image after the form is submitted. Is the php action name the name of the custom code element? I can't seem to get the shortcode to actually fill in the url, path to the file, and filename in the email for whatever reason.
healyhatman 04 Jul, 2018
Because those aren't short codes that's PHP code.
ftpaul 04 Jul, 2018
According to the v5 example just putting {sig_img} in the email template should work but it doesn't. I just assumed because I'm using a modified version of v5 code on v6 was the reason why using the {sig_img} tag in the email template was why it wasn't working.
healyhatman 04 Jul, 2018
V5 and V6 are different, and it wouldn't have worked the way you're wanting anyway.
ftpaul 04 Jul, 2018
I changed the $form->files code to $this->files and now I'm getting better results. Now [sig_img] shows in the array as a URL. Now all I need it to do is show that in the email template.
healyhatman 04 Jul, 2018
1 Likes
Shows in what array? If in the data array then you'd get it with {data:sig_img}
ftpaul 04 Jul, 2018
I figured it out. I had to add sig_img:{data:sig_img} to the data override on insert for the save data action. That allowed me to save the data in the mysql table. Then I just added {data:sig_img} to the email body and now the png is coming through in the email itself. Super happy. Thanks healyhatman for the pointers.
This topic is locked and no more replies can be posted.