PNG image required to be converted from JSON and email - php

So I have used a signature pad from another source and the pad works fine. When I submit the form, it saves the image as JSON code and this is useful as I need to store it within a database as well. However upon submission, I need to convert the code to png and email the image. This is the code I use to convert the code to image, $output2 is the JSON code from the signature capture, signature-to-image.php is the file actually converting the image:
require_once 'signature-to-image.php';
$img = sigJsonToImage($output2);
// Save to file
imagepng($img);
// Destroy the image in memory when complete - I destroy the image at the very end of the PHP file
This code converts the code fine as I have tested it opening a header with just $img in it and the image appears fine.
However when I try to add the png image to the body of the email, it just shows up as Resource id#7. I use the line of code below to send the image:
$email_message .= "Signature: ".clean_string($img)."\n";
I am assuming I need to either embed the image within the body of the email (which I am unfamiliar with) or I need to send the image as an attachment (which I am also unfamiliar with). Am I correct in thinking this? which option is the better choice or is there a better alternative?
So I write in the signature pad, it captures the signature and saves the image in JSON (as far as I am aware, I have zero experience using JSON) This is an example of the output I would receive:
[{"lx":46,"ly":49,"mx":46,"my":48},{"lx":46,"ly":48,"mx":46,"my":49},{"lx":46,"ly":51,"mx":46,"my":48},{"lx":46,"ly":55,"mx":46,"my":51},{"lx":46,"ly":62,"mx":46,"my":55},{"lx":47,"ly":69,"mx":46,"my":62},{"lx":52,"ly":94,"mx":47,"my":69},{"lx":54,"ly":105,"mx":52,"my":94},{"lx":56,"ly":114,"mx":54,"my":105},{"lx":58,"ly":126,"mx":56,"my":114},{"lx":59,"ly":133,"mx":58,"my":126},{"lx":60,"ly":136,"mx":59,"my":133},{"lx":60,"ly":140,"mx":60,"my":136},{"lx":88,"ly":25,"mx":88,"my":24},{"lx":88,"ly":24,"mx":88,"my":25},{"lx":88,"ly":28,"mx":88,"my":24},{"lx":89,"ly":33,"mx":88,"my":28},{"lx":90,"ly":39,"mx":89,"my":33},{"lx":91,"ly":43,"mx":90,"my":39},{"lx":93,"ly":60,"mx":91,"my":43},{"lx":94,"ly":69,"mx":93,"my":60},{"lx":96,"ly":79,"mx":94,"my":69},{"lx":98,"ly":94,"mx":96,"my":79},{"lx":99,"ly":102,"mx":98,"my":94},{"lx":100,"ly":114,"mx":99,"my":102},{"lx":100,"ly":117,"mx":100,"my":114},{"lx":100,"ly":118,"mx":100,"my":117},{"lx":100,"ly":120,"mx":100,"my":118},{"lx":28,"ly":116,"mx":28,"my":115},{"lx":28,"ly":115,"mx":28,"my":116},{"lx":28,"ly":114,"mx":28,"my":115},{"lx":30,"ly":113,"mx":28,"my":114},{"lx":32,"ly":112,"mx":30,"my":113},{"lx":36,"ly":110,"mx":32,"my":112},{"lx":57,"ly":104,"mx":36,"my":110},{"lx":67,"ly":100,"mx":57,"my":104},{"lx":91,"ly":92,"mx":67,"my":100},{"lx":121,"ly":75,"mx":91,"my":92},{"lx":127,"ly":72,"mx":121,"my":75},{"lx":129,"ly":72,"mx":127,"my":72},{"lx":130,"ly":72,"mx":129,"my":72},{"lx":130,"ly":73,"mx":130,"my":72},{"lx":196,"ly":48,"mx":196,"my":47},{"lx":196,"ly":47,"mx":196,"my":48},{"lx":198,"ly":45,"mx":196,"my":47},{"lx":200,"ly":43,"mx":198,"my":45},{"lx":204,"ly":42,"mx":200,"my":43},{"lx":209,"ly":41,"mx":204,"my":42},{"lx":219,"ly":40,"mx":209,"my":41},{"lx":232,"ly":39,"mx":219,"my":40},{"lx":238,"ly":39,"mx":232,"my":39},{"lx":244,"ly":39,"mx":238,"my":39},{"lx":257,"ly":38,"mx":244,"my":39},{"lx":261,"ly":38,"mx":257,"my":38},{"lx":263,"ly":38,"mx":261,"my":38},{"lx":223,"ly":42,"mx":223,"my":41},{"lx":223,"ly":41,"mx":223,"my":42},{"lx":223,"ly":44,"mx":223,"my":41},{"lx":223,"ly":45,"mx":223,"my":44},{"lx":223,"ly":50,"mx":223,"my":45},{"lx":223,"ly":52,"mx":223,"my":50},{"lx":223,"ly":56,"mx":223,"my":52},{"lx":223,"ly":75,"mx":223,"my":56},{"lx":223,"ly":84,"mx":223,"my":75},{"lx":223,"ly":91,"mx":223,"my":84},{"lx":223,"ly":110,"mx":223,"my":91},{"lx":223,"ly":119,"mx":223,"my":110},{"lx":223,"ly":123,"mx":223,"my":119},{"lx":223,"ly":127,"mx":223,"my":123},{"lx":222,"ly":129,"mx":223,"my":127},{"lx":221,"ly":129,"mx":222,"my":129},{"lx":219,"ly":130,"mx":221,"my":129},{"lx":217,"ly":130,"mx":219,"my":130},{"lx":211,"ly":131,"mx":217,"my":130},{"lx":201,"ly":131,"mx":211,"my":131},{"lx":192,"ly":132,"mx":201,"my":131},{"lx":181,"ly":132,"mx":192,"my":132},{"lx":177,"ly":132,"mx":181,"my":132},{"lx":173,"ly":132,"mx":177,"my":132},{"lx":163,"ly":132,"mx":173,"my":132},{"lx":161,"ly":132,"mx":163,"my":132},{"lx":159,"ly":132,"mx":161,"my":132},{"lx":161,"ly":132,"mx":159,"my":132},{"lx":166,"ly":131,"mx":161,"my":132},{"lx":172,"ly":130,"mx":166,"my":131},{"lx":176,"ly":130,"mx":172,"my":130},{"lx":192,"ly":130,"mx":176,"my":130},{"lx":203,"ly":130,"mx":192,"my":130},{"lx":211,"ly":130,"mx":203,"my":130},{"lx":219,"ly":130,"mx":211,"my":130},{"lx":222,"ly":130,"mx":219,"my":130},{"lx":228,"ly":130,"mx":222,"my":130},{"lx":232,"ly":130,"mx":228,"my":130},{"lx":235,"ly":130,"mx":232,"my":130},{"lx":240,"ly":130,"mx":235,"my":130},{"lx":241,"ly":130,"mx":240,"my":130},{"lx":242,"ly":130,"mx":241,"my":130},{"lx":243,"ly":130,"mx":242,"my":130},{"lx":246,"ly":130,"mx":243,"my":130},{"lx":250,"ly":129,"mx":246,"my":130},{"lx":261,"ly":128,"mx":250,"my":129},{"lx":265,"ly":128,"mx":261,"my":128},{"lx":267,"ly":127,"mx":265,"my":128},{"lx":269,"ly":127,"mx":267,"my":127}]

Related

Trying to display an echoed image from a PHP script in and `<img src="..."` tag

The objective:
1) I have a images folder outside web root where all images from the web app are placed (each image path is saved in a DB table);
2) I want to get that image upon a request from the user.
Simple, right?
I did:
a) Create a class to reads the image (with all security features and safeguards that I can remember). It returns (successfully) the image encoded (base64) with the respective mime type.
b) I've created, for demonstration purposes, a index.php, which has:
<img src="agivenscript.php?img=name.jpeg">
My only objective here is to call from any other place in my web app a script that outputs the base64 encoded image with the respective mime type.
c) Created a script "agivenscript.php" that receives the image name by GET, instantiates my class and gets, when all goes right, the base64 encoded image. It then echoes the image.
What happens:
When I do the output - using echo '<img src="' . $output64encodedwithmimetype . '">'; - in agivenscript.php it works like a charm;
Moreover, if I take the $output64encodedwithmimetype contents and place the string directly in the src tag of index.php, it also works.
However, it does not work when I try <img src="agivenscript.php?img=name.jpeg">.
As the base64 encoded image is obviously fine (and has the mime type), what am I missing? Any idea?
Thank in advance.
data: scheme URLs can be Base64 encoded.
HTTP responses of the image/jpeg (or png or whatever) can't. You need to serve up the actual image data without encoding it as text.
readfile is probably all you need.

PHP - Possible to take a base64 encoded pdf data string and compress it?

So I have an XML file that has a base64 encoded data string for a pdf file, which just has an image taken from an iPad.
This pdf file can be excessively large, as much as 14MB with dimensions of 57"x38".
These images are taken from an iPad through a DocuSign session, thus I have no way at the moment of controlling their size or format before they get to my php listener script.
However, my script cannot work with such large files as my CRM's API file size max is 10MB, and I need a way of reducing the file size before I can upload it through my CRM's API.
Now if it was just a jpg, it would be ok as there are plenty of ways to reduce file size in PHP, but it is a PDF. I have found plenty of PHP extensions for making PDFs, but I haven't found any for reading a PDF and extracting an image from it.
So is there a way to extract the image from the PDF through PHP, or perhaps compress the pdf file?
UPDATE
I didn't think about the possibility of converting a pdf into a jpg, which apparently is easier to do with imagick. Having my server admin install it and I will see if I can make it work with my script.
UPDATE 2
So I was able to get imagick working and locally I am able to convert pdf files into jpg, and reduce file size dramatically.
However, I am running into an issue using it with my application. I get the following error from my CRM's API:
Failed to parse XML-RPC request: Invalid byte 1 of 1-byte UTF-8 sequence.
So the process is the following:
XML file has a base64 encoded data stream of the pdf file.
I decode this data
I then convert with imagick and reduce file size
I base64 encode and prep for upload
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
$imagick.$x = new Imagick();
$imagick.$x->readImageBlob($imageBlob);
$imagick.$x->setImageFormat('jpeg');
$imagick.$x->setImageCompressionQuality(60);
$imagick.$x->adaptiveResizeImage(1024,768,true);
$imageBlob = $imagick.$x->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
I can test the date by using the proper header and I can see the new jpeg fine, so I assume the data is properly formatted.
What I am missing?
Ok, so I figured it out.
Imagick was the way to go, and my use of it was good. I just goofed up on the file name because I wasn't using a proper dynamic variable name. Code should have looked like this:
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
${'imagick'.$x} = new Imagick();
${'imagick'.$x}->readImageBlob($imageBlob);
${'imagick'.$x}->setImageFormat('jpeg');
${'imagick'.$x}->setImageCompressionQuality(60);
${'imagick'.$x}->adaptiveResizeImage(1024,768,true);
$imageBlob = ${'imagick'.$x}->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
$PDFfile[] = $FormCustomField . $x . '.jpg';
So the error I was getting was because of an invalid file name, because the $x variable in the previous code was getting junk values. Now everything works fine.

png attachment generated by an API in phpmailer

I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!

Image creation using php

Am trying to create a video clip using .jpg images and ffmpeg, am creating .jpg images as below:
$str=$_REQUEST['data_array'];//gets the base64 encoded image data;
$count =$_REQUEST['count'];//gets number of images i've to create;
$edited_str=substr($str,23,strlen($str)-1);//
$edited_str=base64_decode($edited_str);
$f=fopen("Images/temp".$count.".jpg","w");//am creating temp.jpg file
fwrite($f,$edited_str);//placing my decoded data into file
fclose($f);
are the images am creating above different from normal .jpg images?
This line:
$edited_str=substr($str,23,strlen($str)-1);
makes it different. If this is the full base64 sting of the file, then this cuts it up and corrupts it. Maybe you are adding some stuff on the front.
If you are just removing stuff from the front that was added, then it should be the same as the original file that was encoded with base64.
If you want to get the information this way from another page, I suggest using $_POST as opposed to $_REQUEST for a number of reasons.
EDIT: I wouldn't say video manipulation in php is impossible. I think there is even a toolkit... here's one:
http://phpvideotoolkit.sourceforge.net/
which states:
It can perform video format conversion, extract video frames into separate image files, and assemble a video stream from a set of separate video images.
Haven't tested it, but plan to try it out one day.
EDIT2: On the php site, there were some issues that you could try, but to help more, there needs to be more information. Check with a file directly to make sure it's being sent and decrypted properly.
I haven't got to test any of these yet.
One piece of advice was for large files use:
$decodedstring=base64_decode(chunk_split($encodedstring));
Another was if you use javascript canvas.toDataURL() then you need to convert spaces back to pluses:
$encodedData = str_replace(' ','+',$encodedData);
$decocedData = base64_decode($encodedData);
http://php.net/manual/en/function.base64-decode.php

Outputting an image from the same PHP file which also receives & sends JSON

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.
Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.
This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.
My ideal solution would be something like:
header('Content-Type: image/gif');
echo $this->canvas;
// Save user file
$this->canvas->writeImage( $this->userFile = 'user_img.gif' );
$this->canvas->destroy();
// encode everything and send to browser
echo json_encode(array('misc data back to the browser'));
(I still need to send data back to browser via JSON)
And in my HTML I would have the image laid out like this:
<img src='json-processing-script.php' />
But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.
In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.
See for example this question.

Categories