This topic is locked
[SOLVED]

 Saving Signature Image into database as blob

10/16/2019 5:58:46 AM
PHPRunner General questions
A
acpan author

Hi,
Referring to this thread: https://asprunner.com/forums/topic/26463-signaturepad-edit-to-store-in-database-blob/pagehlsignature%20padfromsearch1
Background:
I need to do the same, reason being i have 2 same websites on 2 different servers (one is primary and the other secondary site, in case primary is not accessible) ,

they point to the same database, so storing signature images in a folder will not be ideal and i want to minimize the risk of losing the physical signature image files

when updating codes on both sites.
So i need to save the signature images into database so that both sites can access at the same time.
What i did:
I edited the sources and checked output file at:
source at Documents\PHPRunnerPlugins\edit\SignaturePad\EditSignaturePad.php

output \project_folder\signature-to-image.php
EditSignaturePad.php file modification:



...

// original codes

$water_img = imagecreatefrompng($filename);

$water_size = getimagesize($water_img);

imagecopy($img, $water_img, 0, 0, 0, 0, $this->width, $this->height);

imagepng($img, $filename);

...
// my codes inserted after line 160 to store image into database blob field: "signature"

ob_start();

imagepng($img);

$imagevariable = ob_get_contents();

ob_end_clean();

$output = $imagevariable;
// Create some other pieces of information about the user

// to confirm the legitimacy of their signature

$created = time();

$ip = $_SERVER['REMOTE_ADDR'];

$data = array();

$data["signature"] = $imagevariable;

$data["date_created"] = $created;

DB::Insert("signatures_test", $data );


The above was inserted successfully with data saved to the "date_created" field,

but the image was not stored in to the field "signature" in the table above, it is empty.
What did i do wrong?
Thanks for helping.
ACP

Sergey Kornilov admin 10/16/2019

Try to output $imagevariable variable to see what's in there.

A
acpan author 10/16/2019

Thanks, yes, your quick hint get me out of the woods.
Output for $imagevariable:
"�PNG  IHDR�X�ѳs IDATx��� ....
Manually run insert into DB, it gave an error.
Error Code: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Cg������۠�
So, thought the data was not binary safe.
Finally did,
$imagevariable = base64_encode($imagevariable) before insert to db.
and it works! and it works on text field with the base64 encoded data taken up only 11K memory space.
To retrieve the signature, in before process record event, take the data out directly:
$signature = '<img src="data:image/png;base64,'.$data["$signature"].'" width="240" class="no_border" alt="Logo" />';
echo $signature;
Thanks!
ACP



Try to output $imagevariable variable to see what's in there.

Sergey Kornilov admin 10/16/2019

Nice, thank you for the update!