【问题标题】:TCPDF and base64 imageTCPDF 和 base64 图像
【发布时间】:2020-04-17 12:55:17
【问题描述】:

下午好, 我在使用 TCPDF lib 在 PDF 文档中插入图像时遇到问题。图像是通过这种方式从 JSignature 中捕获的:

$uri=base64_decode($_POST['firma']);
$filename=SIGNATURE_IMAGE_PATH.ucfirst(strtolower(($_POST['cognome']))).ucfirst(strtolower(($_POST['nome'])))."-".$birthday.".png";
$ret=file_put_contents($filename, $uri);

在另一个 PHP 页面中,我可以通过这种方式正确查看图像:

<?php
    $string=base64_encode(file_get_contents(str_replace(" ", "", SIGNATURE_IMAGE_PATH.$member->firma)))
?>
<img src="data:image/svg+xml;base64,<?php echo $string ?>" />

但是,如果我想打印 pdf 文件,签名不起作用:

  1. 这边:

    $string=file_get_contents(str_replace(" ", "", SIGNATURE_IMAGE_PATH.$member->firma));
    $pdf->Image('@'.$string);
    

返回这个错误:TCPD ERROR: [Image] Unable to get the size of the image:@ ** *and Corrected image visualized!!

  1. 这边:

    $string=file_get_contents(str_replace(" ", "", SIGNATURE_IMAGE_PATH.$member->firma));
    $img = '<img src=data:image/svg+xml;base64,'.$string.'>';
    $pdf->writeHTML($img, true, false, true, false, '');
    

不返回错误但不显示图像。 我已经阅读了有关此问题的其他主题,但提出的任何解决方案似乎都不适合我。

【问题讨论】:

  • Ciao Luca,你能看到我的回答吗?

标签: php image tcpdf


【解决方案1】:

在我的项目中,我这样做是为了完美地使用 JSignature + TCPDF:

签名:
- HTML

<div id="signature"></div><br>
<input type="hidden" id="hiddenSigData" name="hiddenSigData" />

-JS(用你的按钮ID和输入ID更改ID#btnSave)

$('#btnSave').click(function(){
var sigData = $('#signature').jSignature('getData','base30');
$('#hiddenSigData').val(sigData);       
 });

TCPPDF 代码:

$firmacode=$_POST['hiddenSigData'];
$firmatradotta=base30_to_jpeg($firmacode,'nameofimage.png');
$firmadefinitiva='path/to/image/'.$firmatradotta;
$content='

<table border="0">
    <tr>
        <td style="border-bottom:1pt solid black;"><img src="'.$firmadefinitiva.'"></td>
    </tr>

</table>
';
$obj_pdf->writeHTMLCell(120, 20, 80, 244, $content);

图像解码功能:

function base30_to_jpeg($base30_string, $output_file) {
require_once ('./jSignature/jSignature_Tools_Base30.php');
$data = str_replace ( 'image/jsignature;base30,', '', $base30_string );
$converter = new jSignature_Tools_Base30 ();
$raw = $converter->Base64ToNative ( $data );
// Calculate dimensions
$width = 0;
$height = 0;
foreach ( $raw as $line ) {
    if (max ( $line ['x'] ) > $width)
        $width = max ( $line ['x'] );
    if (max ( $line ['y'] ) > $height)
        $height = max ( $line ['y'] );
}

// Create an image
$im = imagecreatetruecolor ( $width + 20, $height + 20 );

// Save transparency for PNG
imagesavealpha ( $im, true );
// Fill background with transparency
$trans_colour = imagecolorallocatealpha ( $im, 255, 255, 255, 127 );
imagefill ( $im, 0, 0, $trans_colour );
// Set pen thickness
imagesetthickness ( $im, 2 );
// Set pen color to black
$black = imagecolorallocate ( $im, 0, 0, 0 );
// Loop through array pairs from each signature word
for($i = 0; $i < count ( $raw ); $i ++) {
    // Loop through each pair in a word
    for($j = 0; $j < count ( $raw [$i] ['x'] ); $j ++) {
        // Make sure we are not on the last coordinate in the array
        if (! isset ( $raw [$i] ['x'] [$j] ))
            break;
        if (! isset ( $raw [$i] ['x'] [$j + 1] ))
            // Draw the dot for the coordinate
            imagesetpixel ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $black );
        else
            // Draw the line for the coordinate pair
            imageline ( $im, $raw [$i] ['x'] [$j], $raw [$i] ['y'] [$j], $raw [$i] ['x'] [$j + 1], $raw [$i] ['y'] [$j + 1], $black );
    }
}

// Check if the image exists
if (! file_exists ( dirname ( $output_file ) )) {
    mkdir(dirname($output_file));
}

// Create Image
$ifp = fopen ( $output_file, "wb" );
imagepng ( $im, $output_file );
fclose ( $ifp );
imagedestroy ( $im );
copy($output_file, 'path/to/image/'.$output_file);
unlink( $output_file );
return $output_file;
}

希望对您有所帮助! Ciao ;)

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多