简体   繁体   English

如何在 PHP 中将 base64 字符串转换为 png 图像?

[英]How do I convert a base64 string to a png image in PHP?

I am trying to create a PHP form that outputs a PDF (using mPDF) of all the values in the form after hitting submit.我正在尝试创建一个 PHP 表单,该表单在点击提交后输出表单中所有值的 PDF(使用 mPDF)。 I have included jSignature which takes in an e-signature drawn by the user.我已经包含了 jSignature,它接收用户绘制的电子签名。 The signature is then converted to an image and outputs the data URI string in a text area.然后将签名转换为图像并在文本区域中输出数据 URI 字符串。

            <div id="signature"></div><br/>

            <input type='button' id='click' value='click'>
            <textarea id='output' name="signature"></textarea><br/>

            <!-- Preview image -->
            <img src='' id='sign_prev' style='display: none;' />

            <!-- Script -->
            <script>
            $(document).ready(function() {

            // Initialize jSignature
            var $sigdiv = $("#signature").jSignature({'UndoButton':true});

            $('#click').click(function(){
            // Get response of type image
            var data = $sigdiv.jSignature('getData', 'image');

            // Storing in textarea
            $('#output').val(data);

            // Alter image source 
            $('#sign_prev').attr('src',"data:"+data);
            $('#sign_prev').show();
            });
            });
            </script>
            <button type="submit" class="btn btn-success btn-lg">Submit</button>

I have another file that uses the name attribute from the input areas to display the values from each input field to show on the PDF.我有另一个文件,它使用输入区域中的 name 属性来显示每个输入字段中的值以显示在 PDF 上。 Since the signature comes in as a data URI string I have to convert it back into a png image before creating the PDF.由于签名是作为数据 URI 字符串出现的,因此我必须在创建 PDF 之前将其转换回 png 图像。 What I am sure of at the moment is that I am able to retrieve the data URI string and I am able to replace the "image/png;base64" string that comes with it so all I am left with is the data itself.我现在确定的是,我能够检索数据 URI 字符串,并且能够替换它附带的“image/png;base64”字符串,因此我只剩下数据本身。 But I am struggling to decode the URI back to an image and I am not sure about how I should display it back as an image.但是我正在努力将 URI 解码回图像,我不确定应该如何将其显示为图像。

<?php


require_once __DIR__ . '/vendor/autoload.php';


//grab variables
$inspection = $_POST['inspection'];
$ccOfficer = $_POST['cco-name'];
$inspector = $_POST['inspector'];
$tower = $_POST['tower'];
$strata = $_POST['strata'];
$suite = $_POST['suite'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$signature = $_POST['signature'];

//Create new PDf instance
$mpdf = new \Mpdf\Mpdf();

$mpdf->debug = true;

$signature = str_replace('image/png;base64,','', $signature);

$signatureDecode = base64_decode($signature);

$im = imagecreatefromstring($signatureDecode);

if(!$im) {
    die('Base64 value is not a valid image');
};

// $img_file = '/wti-report-form/images/signature.png';
// imagepng($im, $img_file, 0);

//$file = fopen($output_file, "wb");

//fwrite($file, $signatureDecode);
//fclose($file);

//Create our PDF
$data = '';


$data .= '<h1>ARC - NEW HOME WALK-THROUGH CHECKLIST</h1>';

//add data
$data .= '<strong>Inspection Date</strong> ' . $inspection . '<br/>';
$data .= '<strong>Customer Care Officer</strong> ' . $ccOfficer . '<br/>';
$data .= '<strong>This Suite was inspected by</strong> ' . $inspector . '<br/>';
$data .= '<strong>Tower #</strong> ' . $tower . '<br/>';
$data .= '<strong>Strata Lot #</strong> ' . $strata . '<br/>';
$data .= '<strong>Suite #</strong> ' . $suite . '<br/>';
$data .= '<strong>Email</strong> ' . $phone . '<br/>';
$data .= '<strong>Phone</strong> ' . $email . '<br/>';
//$data .= "<img src='" . $file . "'/>";    
//$data .= '<p>' . $signature . '</p>';    

//Write PDF
$mpdf->WriteHTML($data);

//output to the browser

$mpdf->Output('myfile.pdf', 'I');
?>

Any help will be appreciated.任何帮助将不胜感激。 Thanks!谢谢! :) :)

据我所知,您可以在 img 标签中使用 base64 数据字符串,而 mpdf 将正确呈现 PDF,您是否尝试过此解决方案?

$data.='<img src="data:image/png;base64,'.$signature.'" />';

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

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