简体   繁体   English

如何将base64 png图像发送到PHP

[英]How to send base64 png image to PHP

I have a base64 png image. 我有一个base64 png图像。
I'd like to use it in FPDF 我想在FPDF中使用它

I create it like this: 我这样创建它:

var dataURI = canvas.toDataURL("image/png");

And send it to FPDF like this: 像这样发送到FPDF:

$('#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: create_pdf.php我有:

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: 我没有出现通常的保存PDF的提示,而是出现以下错误:

Image file has no extension and no type was specified: PNG IHDRSJ IDATx...... 图像文件没有扩展名且未指定类型:``PNGIHDRSJ IDATx......''

How to properly send that base64 encoded image? 如何正确发送base64编码的图像?

似乎pfdf将保存的iamge作为参数,而不是base64尝试在此处回答

FPDF image() function checks for the file type based on the $file string which should contain a filepath. FPDF image()函数根据应包含文件路径的$ file字符串检查文件类型。 If you offer a type eg 'png' in the optional parameters you can overcome this part. 如果您在可选参数中提供类型(例如“ png”),则可以克服这一部分。

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将尝试在其_parsepng和_parsejpg函数中打开$ file,以便您可以轻松地使用数据包装器http://www.php.net/manual/en/wrappers.data.php

$fpdf->Image('data://image/png;base64,'.$image_data,null, null, 0, 0, 'png');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM