wtite a image to database using imagejpeg - php

Basically what I have to do, is write a Image to the database.
As far as I understand the imagejpeg function - the output should be the plain image data, if the "string $filename" is not set or NULL.
But it does not, the only output is "1" (true)...
How do I get to the image data, without first storing the image in the filesystem and reloading it?
Here is my Code example:
// If it is the right filetype.
if ($_FILES[$dom_element]['type'] == 'image/jpeg' || $_FILES[$dom_element]['type'] == 'image/pjpeg') {
// Create the image recource.
$image_image = imagecreatefromjpeg($_FILES[$dom_element]['tmp_name']);
}
// Resize the image.
imagecopyresampled($image_image_p, $image_image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_width, $image_height);
// Write the image in the database (does not work this way -> output = "1")
mysql_select_db("$db_announcement");
$sql = "UPDATE announcement
SET announcement_guest_image_data = '" . addslashes(imagejpeg($image_image_p, NULL, $settings_image_quality)) . "',
announcement_guest_image_exists = 'yes'
WHERE announcement_timestamp = '$timestamp' AND announcement_guest_mail = '$global_mail'";
$sql = mysql_query($sql);
// Delete the image recources.
imagedestroy($image_image);
imagedestroy($image_image_p);

The documentation says about the $filename parameter:
If not set or NULL, the raw image stream will be outputted directly.
And about the return value:
Returns TRUE on success or FALSE on failure.
The function outputs the image to the standard output. It does not return it.
You can capture it from the standard output like so:
ob_start();
imagejpeg(...);
$imageData = ob_get_clean();

I would highly recommend you to avoid storing binary data, such as images in database. You can simply record everything, including the image name, date of creation, etc. on your tables, then just put that file on the file system.
I know that it is not the answer you're looking for, however I thought that it might helps.

Related

PHP retrieve multiple rows with stream content at once with sqlsrv

This is kind of a follow up of this question.
This code retries a row from the database where one column is a resource and creates a file system file with it.
$query = "select top(1) DESCRIPTION, FILETYPE, DOCUMENT from dbo.Documents;";
$stmt = sqlsrv_query($this->sqlsrv_conn, $query);
if (sqlsrv_fetch($stmt)) {
$document = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
// $fileName = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
// $ext = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
file_put_contents(
// $fileName . '.' . $ext,
'filename'.'.doc',
stream_get_contents($document),
);
}
Now I have to do this with all the records in the database, not only one. What is the best way to achieve that?
I looked at sqlsrv_fetch_array which has an argument "$fetchType" but this is to define in which format the rows are grouped together (numbered or assoc array).
How can I define the fetchType for each column under that array of rows? Like I can with sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)) when only one row is fetched.
Note: I have received the feedback that my questions tend to be unclear, if you see things that can be improved to make this question better and easier to answer, please let me know.
Edit
Thank you #Zhorov the while loop does work! But I face a new silly issue.
I lied, the code I posted was not exactly the code that worked for me. Apparently I didn't test it properly with fetching the file name and extension as well. What worked was only when I fetched the binary data as a stream with nothing else.
I don't understand it at all. The following code works like expected:
$query = "select top(1) DESCRIPTION, FILETYPE, DOCUMENT from dbo.Documents;";
$stmt = sqlsrv_query($this->sqlsrv_conn, $query);
if (sqlsrv_fetch($stmt)) {
$document = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
// $fileName = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
file_put_contents(
// $fileName . '.doc',
'filename.doc',
stream_get_contents($document),
);
}
But if I also want to fetch the file name or anything else
$query = "select top(1) DESCRIPTION, FILETYPE, DOCUMENT from dbo.Documents;";
$stmt = sqlsrv_query($this->sqlsrv_conn, $query);
if (sqlsrv_fetch($stmt)) {
$document = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
$fileName = sqlsrv_get_field($stmt, 0);
file_put_contents(
'filename.doc',
stream_get_contents($document),
);
}
I get the following exception:
Error: stream_get_contents(): supplied resource is not a valid stream resource File
I debugged it down to this with xdebug. Here are screenshots of the two scenarios. This is the run with a valid stream and no exception (you can see the "type=stream"):
And here, the only thing I did is remove the comment on the line and a breakpoint right before the exception happens (you can see the "type=Unknown"):
Why does fetching something else break the stream? (This behaviour is the same when I using while loop or not)
Edit 2
Behaviour does change when changing the order in which sqlsrv_get_field is called. If I put the binary data after the file name, the $document variable is false. Not even an "Unknown" stream.
Edit: Reason for this is, as #Zhorov pointed out:
From documentation - sqlsrv_get_field retrieves data from the specified field of the current row. Field data must be accessed in order. For example, data from the first field cannot be accessed after data from the second field has been accessed
#Zhorov brought me on the right track for the solution.
It appears that the stream content has to be accessed last. If I get the file name before document, stream is "Unknown" and an exception is thrown. But if I get the stream at last, it works!
See for yourself. The following is the version where the stream is invalid:
And when retrieving the binary data in the column DOCUMENT last, it works:

Using PHP how can you tell if the image already exists on your server regardless of name?

I have seen several websites where if you upload an image and an identical image already exists on there servers they will reject the submission. Using PNGs is there an easy way to check one image against a massive folder of images?
http://www.imagemagick.org/discourse-server/viewtopic.php?t=12618
I did find this with imagemagick, but I am looking for one vs many and not one to one a million
You can transform the file content into a sha1. That will give you a way to identify two pictures strictly identical.
see http://php.net/manual/fr/function.sha1-file.php
Then after you save it into a NFS, or use some kind of database to test if the hash already exists.
Details of the images are probably maintained in a database; while the images are stored in the filesystem. And that database probably has a hash column which is used to store an md5 hash of the image file itself, calculated when the image is first uploaded. When a new image is uploaded, it calculates the hash for that image, and then checks to see if any other image detail in the database has a matching hash. If not, it stores the newly uploaded image with that hash; otherwise it can respond with details of the previous upload. If the hash column is indexed in the table, then this check is pretty quick.
If I understood your question correctly. You want to find out if a specific image exists in a Directory with so many images, right? If so, take a look at the solution:
<?php
// CREATE A FUNCTION WHICH RETURNS AN ARRAY OF ALL IMAGES IN A SPECIFIC FOLDER
function getAllImagesInFolder($dir_full_path){
$returnable = array();
$files_in_dir = scandir($dir_full_path);
$reg_fx = '#(\.png|\.jpg|\.bmp|\.gif|\.jpeg)#';
foreach($files_in_dir as $key=>$val){
$temp_file_or_dir = $dir_full_path . DIRECTORY_SEPARATOR . $val;
if(is_file($temp_file_or_dir) && preg_match($reg_fx, $val) ){
$regx_dot_wateva = '/\.{2,4}$/';
$regx_dot = '/\./';
$regx_array = array($regx_dot_wateva, $regx_dot);
$replace_array = array("", "_");
$return_val = preg_replace($regx_array, $replace_array, $val);
$returnable[$return_val] = $temp_file_or_dir ;
}else if(is_dir($temp_file_or_dir) && !preg_match('/^\..*/', $val) ){
getFilesInFolder($temp_file_or_dir);
}
}
return $returnable;
}
// CREATE ANOTHER FUNCTION TO CHECK IF THE SPECIFIED IMAGE EXISTS IN THE GIVEN DIRECTORY.
// THE FIRST PARAMETER SHOULD BE THE RESULT OF CALLING THE PREVIOUS FUNCTION: getAllImagesInFolder(...)
// THE SECOND PARAMETER IS THE IMAGE YOU WANT TO SEARCH WHETHER IT EXISTS IN THE SAID FOLDER OR NOT
function imageExistsInFolder($arrImagesInFolder, $searchedImage){
if(!is_array($arrImagesInFolder) && count($arrImagesInFolder) < 1){
return false;
}
foreach($arrImagesInFolder as $strKey=>$imgPath){
if(stristr($imgPath, $searchedImage)){
return true;
}
}
return false;
}
// NOW GET ALL THE IMAGES IN A SPECIFIED FOLDER AND ASSIGN THE RESULTING ARRAY TO A VARIABLE: $imgFiles
$imgFolder = "/path/to/directory/where/there/are/images";
$arrImgFiles = getAllImagesInFolder($imgFolder);
$searchedImage = "sandwich.jpg"; //<== OR EVEN WITHOUT THE EXTENSION, JUST "sandwich"
// ASSUMING THE SPECIFIC IMAGE YOU WANT TO MATCH IS CALLED sandwich.jpg
// YOU CAN USE THE imageExistsInFolder(...) FUNCTION TO RETURN A BOOLEAN FLAG OF true OR false
// DEPENDING ON IF IT DOES OR NOT.
var_dump($arrImgFiles);
var_dump( imageExistsInFolder($arrImgFiles, $searchedImage) );

How to processing an image downloaded from AWS S3 with Laravel 5?

I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.

Image from database into PDF using FPDF

I have an image that is sent from an iPad app to an SQL database. I can retrieve this image and display in a web page using the following php:
$img = base64_encode($row['photoData']);
echo "<img src=\"data:image/jpg;charset=utf8;base64, $img\"/>";
This displays fine. What I want to do now is put this image into a PDF document using FPDF however I am struggling to do this.
This:
$img = base64_encode($row['photoData']);
$pdf->Image($img);
give this error:
FPDF error: Image file has no extension and no type was specified:
So I tried this (although I realise I will then have to look at how to get the size of the image sorted):
$pdf->Image($img, 20, 20, 20, 20 'JPG');
which give me:
FPDF error: Missing or incorrect image file:
What is the correct way to do this?
Or would it be easier to temporarily save the image to the server and then place the saved image into the PDFdoc?
As mentioned in the comments above this is possible by using a stream ("data url") to hand over the image data to the fpdf library without writing physical files to disk:
<?php
// load the 'fpdf' extension
require('fpdf.php');
// just for demonstration purpose, the OP gets the content from a database instead
$h_img = fopen('img.jpg', "rb");
$img = fread($h_img, filesize('img.jpg'));
fclose($h_img);
// prepare a base64 encoded "data url"
$pic = 'data://text/plain;base64,' . base64_encode($img);
// extract dimensions from image
$info = getimagesize($pic);
// create a simple pdf document to prove this is very well possible:
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello Image!');
$pdf->Image($pic, 10, 30, $info[0], $info[1], 'jpg');
$pdf->Output();
If this is a good advice is another question, this is merely meant to prove that this is possible...
According to the Docs FPDF::Image accepts a filename as the first argument, not a binary blob.
If you want to use FPDF specifically, save the image to a temporary file first, and then pass that to FPDF::Image.
To do that, something like this should work:
$tmpFile = tempnam(sys_get_temp_dir(), 'fpdfimg');
if (file_put_contents($tmpFile, $row['photoData'])) {
$fpdf->Image($tmpFile);
// save/display image
unlink($tmpFile);
}
Alternatively, if you want to just serve the image as a PDF (with no other content) you could use Imagick:
$im = new \Imagick();
$im->readImageBlob($row['photoData']);
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;
Since FPDF cannot use base64 data to produce images on the PDF, I would recommend saving the file to the disk permanently as opposed to writing a temp file for every PDF operation.
This will save you a lot of I/O overhead.
Assuming your table has unique photo_id or photo_name to accompany photoData then you can use something like this to create your images and use them in FPDF.
I will also assume you have a last_update and photo_extension column.
<?php
$path = '/path/to/fpdf/images/';
$filename = $row['photo_id'].'.'.$row['photo_extension'];
$filepath = $path.$filename;
// If a physical file is not available then create it
// If the DB data is fresher than the file then make a new file
if(!is_file($filepath) || strtotime($row['last_update']) > filemtime($filepath))
{
$result = file_put_contents($filepath, $row['photoData']);
if($result === FALSE)
{
die(__FILE__.'<br>Error - Line #'.__LINE__.': Could not create '.$filepath);
}
}
$pdf->Image($filepath);
If you plan on updating the photoData which is stored in your DB then you will have to make sure to also have a timestamp column and compare that timestamp against the filemtime($filepath) of the image on your disk.
Another solution for this ;)
Make a new php by copying and pasting this (piece of fpdf's code edited):
require('fpdf.php');
class DATAIMAGE extends FPDF
{
protected function _parsedata($file)
{
// Extract info from a JPEG file
$a = getimagesizefromstring($file);
if(!$a)
$this->Error('Missing or incorrect image file: '.$file);
if($a[2]!=2)
$this->Error('Not a JPEG file: '.$file);
if(!isset($a['channels']) || $a['channels']==3)
$colspace = 'DeviceRGB';
elseif($a['channels']==4)
$colspace = 'DeviceCMYK';
else
$colspace = 'DeviceGray';
$bpc = isset($a['bits']) ? $a['bits'] : 8;
return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$file);
}
}
Then call this php instead of fpdf.php in your main php.
You'll now be able to display an image simply by adding 'data' to the end of the function:
$pdf->Image($mysqlrow["blob"],0,0,40,0,'data');

How to display a JSON/base64 encoded image in FPDF?

I'm storing signatures (using signaturepad in my database Coldfusion/MySQL 5.0.88 and would like to output the signature I'm taking onto a pdf which I'm generating with fpdf. However I can*t get it to work...
Signatures are stored like this:
[{"lx":19,"ly":58,"mx":19,"my":57},{"lx":23,"ly":54,"mx":19,"my":58},{"lx":26,"ly":53,"mx":23,"my":54},{"lx":32,"ly":51,"mx":26,"my":53},{"lx":38,"ly":47,"mx":32,"my":51},{"lx":44,"ly":44,"mx":38,"my":47},{"lx":51,"ly":41,"mx":44,"my":44},{"lx":58,"ly":37,"mx":51,"my":41},{"lx":64,"ly":35,"mx":58,"my":37},{"lx":67,"ly":31,"mx":64,"my":35},{"lx":70,"ly":30,"mx":67,"my":31},{"lx":72,"ly":28,"mx":70,"my":30},{"lx":71,"ly":28,"mx":72,"my":28},{"lx":69,"ly":28,"mx":71,"my":28},{"lx":66,"ly":28,"mx":69,"my":28},{"lx":62,"ly":29,"mx":66,"my":28},{"lx":59,"ly":31,"mx":62,"my":29},{"lx":55,"ly":32,"mx":59,"my":31},{"lx":52,"ly":33,"mx":55,"my":32},{"lx":48,"ly":35,"mx":52,"my":33},{"lx":44,"ly":37,"mx":48,"my":35},{"lx":41,"ly":38,"mx":44,"my":37},{"lx":39,"ly":40,"mx":41,"my":38},{"lx":36,"ly":40,"mx":39,"my":40},{"lx":33,"ly":42,"mx":36,"my":40},{"lx":32,"ly":43,"mx":33,"my":42},{"lx":31,"ly":44,"mx":32,"my":43},{"lx":31,"ly":46,"mx":31,"my":44},{"lx":32,"ly":48,"mx":31,"my":46},{"lx":136,"ly":23,"mx":32,"my":48},{"lx":132,"ly":24,"mx":136,"my":23},{"lx":104,"ly":38,"mx":132,"my":24},{"lx":103,"ly":40,"mx":104,"my":38},{"lx":102,"ly":41,"mx":103,"my":40},{"lx":102,"ly":42,"mx":102,"my":41},{"lx":103,"ly":42,"mx":102,"my":42},{"lx":108,"ly":42,"mx":103,"my":42},{"lx":115,"ly":42,"mx":108,"my":42},{"lx":123,"ly":39,"mx":115,"my":42},{"lx":133,"ly":36,"mx":123,"my":39},{"lx":141,"ly":34,"mx":133,"my":36},{"lx":148,"ly":32,"mx":141,"my":34},{"lx":155,"ly":30,"mx":148,"my":32},{"lx":159,"ly":29,"mx":155,"my":30},{"lx":161,"ly":28,"mx":159,"my":29},{"lx":159,"ly":27,"mx":161,"my":28},{"lx":155,"ly":27,"mx":159,"my":27},{"lx":151,"ly":27,"mx":155,"my":27},{"lx":145,"ly":27,"mx":151,"my":27},{"lx":140,"ly":29,"mx":145,"my":27},{"lx":135,"ly":31,"mx":140,"my":29},{"lx":132,"ly":33,"mx":135,"my":31},{"lx":127,"ly":36,"mx":132,"my":33},{"lx":125,"ly":38,"mx":127,"my":36},{"lx":125,"ly":40,"mx":125,"my":38},{"lx":125,"ly":41,"mx":125,"my":40},{"lx":125,"ly":42,"mx":125,"my":41},{"lx":127,"ly":43,"mx":125,"my":42},{"lx":131,"ly":44,"mx":127,"my":43},{"lx":139,"ly":45,"mx":131,"my":44},{"lx":147,"ly":45,"mx":139,"my":45},{"lx":157,"ly":43,"mx":147,"my":45},{"lx":164,"ly":41,"mx":157,"my":43},{"lx":173,"ly":39,"mx":164,"my":41},{"lx":181,"ly":36,"mx":173,"my":39},{"lx":186,"ly":34,"mx":181,"my":36},{"lx":191,"ly":33,"mx":186,"my":34},{"lx":193,"ly":30,"mx":191,"my":33},{"lx":194,"ly":29,"mx":193,"my":30},{"lx":194,"ly":28,"mx":194,"my":29},{"lx":193,"ly":27,"mx":194,"my":28},{"lx":191,"ly":26,"mx":193,"my":27},{"lx":188,"ly":25,"mx":191,"my":26},{"lx":183,"ly":25,"mx":188,"my":25},{"lx":180,"ly":25,"mx":183,"my":25},{"lx":177,"ly":25,"mx":180,"my":25},{"lx":174,"ly":27,"mx":177,"my":25},{"lx":171,"ly":30,"mx":174,"my":27},{"lx":169,"ly":32,"mx":171,"my":30},{"lx":168,"ly":34,"mx":169,"my":32},{"lx":167,"ly":36,"mx":168,"my":34},{"lx":167,"ly":38,"mx":167,"my":36},{"lx":168,"ly":40,"mx":167,"my":38},{"lx":169,"ly":41,"mx":168,"my":40},{"lx":171,"ly":41,"mx":169,"my":41},{"lx":174,"ly":41,"mx":171,"my":41},{"lx":176,"ly":41,"mx":174,"my":41},{"lx":176,"ly":40,"mx":176,"my":41},{"lx":177,"ly":39,"mx":176,"my":40},{"lx":177,"ly":37,"mx":177,"my":39}]
There is a custom function included in siganturepad (signature to image) to convert the above back into an image. Like so:
<?php
require_once '../../signature-to-image.php';
$img = sigJsonToImage(file_get_contents('sig-output.json'));
// Output to browser
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
>
But if I try this on my fpdf page, the whole page breaks ( I guess because I want to embed the image in a cell not output only the image to the browser) and I get this:
Resource id #10‰PNG IHDRÆ7Ø7¢¶±IDATxœí›iP[×Ç/’a6±£'ŒÀk0›ë݆6)NÒƉݸµM2î´nf:“d2gq“Æušq–Ió! ŽSâÄNJgRÛñ ’#1KOFf±õ´ ÀfÖÖ/}y‘Ä`çþ>x¬û®Gºÿ{Î=ç…¹.Pm¡ €Ük#IA(J B1PRŠ’‚P”„b ¤ %¡()Å#IA(J B1PRŠ’‚P”„b ¤ %¡()Å#IA(J B1PRŠ’‚Pc¡ €üÀðèT³Æ Pa­×L'^z02œ¾Ð”Ô£·Œ+ÔX‹S¨0íÍb¼«wpåòô4,h ¤vK†ê¿Õe¨þÆà1Á óÓ¥bŽTÌÉËN™ÃL#u ªƒÇ•¾§­Y‘±27½ZÂg%{^ ƒ? œNÚoQ¨°A†ê‡F­nâ–Dìyà¾=÷ß¹ >¿¤ýËáÆ[Óþ¿¥€—ºwKá/Ëy4Z1%*lvG{¹YmÀãÚØ”mÆiþ‹i|êN}c÷7­}ß´öûiÃoÖ×dº-¹'“VÛSïž?)ïTKxÏ?.‰aFø˜ßÞcúJ¡;¥¼>:y°±ˆ{è÷kÙ‰1øÕ»XR£“wZԘŔZ÷Z£ÛUuíîäø¨y6iÒjSjJ­A†êÛºMV›ƒ¸ÄCX"Äîp6u}ìüS‡Î\× ªo잰άKß¼³wãk>lÞvàk9Š±¢#_ß³vKEŽŸ·¶9ŽžS½Qßj™xò+^Ýý3|ü®””idâí/ÛjOw:œ^fkɳ[KçÁ˜‘qk‹ÚЬ6ÈP}WŸÅîpâãaa#ÌM–ˆ2[*æho¿ö™RŽbÀo1án©®AõÝõA|„‡...
If I'm doing this:
if( strlen($unterschrift) > 0){
$img = sigJsonToImage( $unterschrift );
};
and try to output:
if ( $imgProceed == "true" ){
$pdf->imagepng($img);
} else {
$pdf->Cell(50,4,'',0,1);
}
all I'm getting is:
`Call to undefined method PDF::imagepng() `/ `strpos() expects parameter 1 to be string, resource`
Can someone explain to me, what I'm doing wrong or what I need to do to include the image in a cell in fpdf? I'm clueless.
Thanks!
The issue is here
$pdf->imagepng($img);
^-------------- This should be an image path (String)
Solution
$file = 'signature.png' ;
imagepng($img, $file);
^----------- Save Image to File Instead
Then
if ($imgProceed == "true") {
$pdf->imagepng($file);
} else {
$pdf->Cell(50, 4, '', 0, 1);
}
Originally, I was going to convert to PNG and the more I thought about it, it was more overhead then necessary. After all, Signature Pad give us everything we need to re-draw the signature in FPDF.
I did the following:
// decode the signature into an array
$sig = json_decode($signatureInJSON);
// for each line segment
foreach ($sig as $line) {
$lx = $line->lx;
$ly = $line->ly;
$mx = $line->mx;
$my = $line->my;
// draw the line
$pdf->Line($lx, $ly, $mx, $my);
}
Obviously, I also added some functions to scale the signature and offset it to where I wanted in the PDF.
Hope that helps someone else.

Categories