How to send base64 png image to PHP - php

I have a base64 png image.
I'd like to use it in FPDF
I create it like this:
var dataURI = canvas.toDataURL("image/png");
And send it to FPDF like this:
$('#send').click(function(e){
e.preventDefault();
var uri = "create_pdf.php?imgURI="+ dataURI;
window.open ( uri, "Temp wind" );
});
inside the create_pdf.php I have:
require('fpdf.php');
$img = $_GET['imgURI'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image( $data, 0, 0, 200 );
$pdf->Output( "myimage.pdf", 'D' );
Instead of the usual prompt to save PDF I get this error:
Image file has no extension and no type was specified: �PNG IHDRSJ����IDATx......
How to properly send that base64 encoded image?

Seems like pfdf takes saved iamge as parameter, not base64
Try the answer here

FPDF image() function checks for the file type based on the $file string which should contain a filepath. If you offer a type eg 'png' in the optional parameters you can overcome this part.
Afterwards, FPDF will try to fopen $file within its _parsepng and _parsejpg functions so you can easily take use of the data wrapper
http://www.php.net/manual/en/wrappers.data.php
$fpdf->Image('data://image/png;base64,'.$image_data,null, null, 0, 0, 'png');

Related

Imagecreatefromwebp(): WebP decode: fail to decode input data

I am trying to convert a webp file to JPEG using imagecreatefromwebp() but unfortunately, it throws me a warning: Warning: imagecreatefromwebp(): WebP decode: fail to decode input data.
Here's my code
$filename = dirname(__FILE__)."\\".$keyword."1.webp"; // $keyword = 'xyz';
$im = imagecreatefromwebp($filename);
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
Please help.
i am using this code, it works fine for me. Here $data contains the base64encoded data
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
The imagecreatefromwebp() function accepts either a valid file or URL. You can also pass the your binary data in that function. You can check the function definition and example here http://php.net/manual/en/function.imagecreatefromwebp.php

Server does not support base64_encode() and base64_decode() methods

i am using the following ajax/jquery code to save html5 canvas as png:
var canvas = document.getElementById("canvas");
var data = canvas.toDataURL("image/png");
$.ajax({
url: "saveAsImage.php",
data:{data:data},
type:"POST",
success:function(r){
$("#result").html(r);
}
});
The PHP code create .png file from the posted data with the following php code:
<?php
if(isset($_POST['data'])){
$img = $_POST['data'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
$fileName = date('Ymdhisa').'.png';
file_put_contents("img/".$fileName, $fileData);
echo('<hr>Created Image is: <br><img src="img/'.$fileName.'">');
}
?>
Now, the actual problem is that my webserver does not support base64_encode() and base64_decode() functions, therefore, i am using the following method instead the base64_decode() method and commented out the base64_decode() method
//$fileData = base64_decode($img);
$fileData = rawurldecode($img);
Is there anyway through which i can do this?

base64 encode PHP generated image without writing the image to disk

I'm generating an image in PHP for use in a users avatar.
I start by hashing the username and then doing a hexdec() conversion on various substrings of the hash to build up a set of RGB colours.
//create image
$avatarImage = imagecreate(250, 250);
// first call to imagecolorallocate sets the background colour
$background = imagecolorallocate($avatarImage, hexdec(substr($hash, 0, 2)), hexdec(substr($hash, 2, 2)), hexdec(substr($hash, 4, 2)));
//write the image to a file
$imageFile = 'image.png';
imagepng($avatarImage, $imageFile);
//load file contents and base64 encode
$imageData = base64_encode(file_get_contents($imageFile));
//build $src dataURI.
$src = 'data: ' . mime_content_type($imageFile) . ';base64,' . $imageData;
Ideally I'd not use the intermediate step and would skip writing the image out onto disk, although I'm not sure how best to implement this?
I've tried passing $avatarImage directly to base64_encode() but that expects a string so doesn't work.
Any ideas?
You can imagepng to a variable:
//create image
$avatarImage = imagecreate(250, 250);
//whatever image manipulations you do
//write the image to a variable
ob_start();
imagepng($avatarImage);
$imagePng = ob_get_contents();
ob_end_clean();
//base64 encode
$imageData = base64_encode($imagePng);
//continue
You can use output buffering to capture the image data and then use it as desired:
ob_start ( ); // Start buffering
imagepng($avatarImage); // output image
$imageData = ob_get_contents ( ); // store image data
ob_end_clean ( ); // end and clear buffer
For convenience you could create a new function to handle image encoding:
function createBase64FromImageResource($imgResource) {
ob_start ( );
imagepng($imgResource);
$imgData = ob_get_contents ( );
ob_end_clean ( );
return base64_encode($imgData);
}

Generate PDF from base64 String PHP

I'm trying to create a .pdf file with a base64 string from an image, and I can create it correctly, but when I try to open the file, the program sends a message that tells the file is corrupt or something like that..
I got this code:
define('UPLOAD_DIR', '../image/');
$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$uniqueNumber = uniqid();
$namefile = $uniqueNumber.'.png';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
$namefile = $uniqueNumber.'.pdf';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
I can open the .png file correctly so, i think it's not problem from the base64 decoded string. Thank you all!
EDIT:
I'm trying this code and getting the same issue.
$data = base64_decode ($img);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$data);
//close output file
fclose ($pdf);
echo 'Done';
Is that becouse i'm saving an image with .pdf ? I think no, because if i'm doing fopen with .pdf should be with that format.
EDIT 2:
FOUND A SOLUTION.
http://www.fpdf.org/en/script/script45.php
I followed these steps and i can get that, thank you all!
Check out DOMPDF: https://github.com/dompdf/dompdf
You can definitely use DOMPDF to create a PDF with an image tag whose source is that Base64 string. and render that to PDF.
<?php
require_once("dompdf_config.inc.php");
$img = $_POST['image'];
$html =
'<html><body>'.
'<img src="'.$img.
'"></body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

Converting a base64 string into a image, without save it on disk using php

I'm working on generating dinamic PDF with FPDF.
Now I'm stuck in this point, I send from Javascript a base64 image containing a Graph.
This is my code inside the FPDF class for insert an image.
function Grafici ($image)
{
list($type, $image) = explode(';', $image);
list(, $image) = explode(',', $image);
$data = base64_decode($image);
file_put_contents('/tmp/image.png', $data);
$this->Image('/tmp/image.png', 10, 63, 100);
}
As you can see, i'm saving the image on the server disk.
Is there a way to avoid the saving? If i remove the file_put_contents command and I put directly $data on $this-> Image($data, 10, 63, 100); FPDF return the error:
FPDF error: Image file has no extension and no type was specified:

Categories