2016-03-25 93 views
0

我目前正在尝试生成水印并将它们放入PDF(FPDI)中,然后再下载文件。到目前为止,它的工作完美,除非背景颜色不是白色,因为那样你就可以看到字体颜色。
所以我的问题将是如何使用PDF中的两种透明颜色来呈现图像,或者如何仅使字体透明并使用原始背景色作为背景。
那是到目前为止我的代码:PHP:透明PNG并插入到PDF

$length = strlen($watermark); 
$fw  = imagefontwidth($fontsize); 
$width = $fw*$length; 
$height = imagefontheight($fontsize); 

//Create watermark-image 
$tmp_file_img = tempnam(TMP.'/pdfwatermarks', "pdfwatermark_img_"); 
$img = imagecreatetruecolor($width, $height); 
    //Background color 
$bg = imagecolorallocate($img, 255, 255, 255); 
imagefilledrectangle($img, 0, 0, $width, $height, $bg); 
imagecolortransparent($img, $bg); 
    //Font color 
$color = imagecolorallocate($img, 50, 50, 50); 
    //Write watermark-string 
for($i=0; $i<$length; $i++){ 
    $xpos = $i * $fw; 
    imagechar($img, $fontsize, $xpos, 0, $watermark, $color); 
    $watermark = substr($watermark, 1); 
} 
    //Opacity 
$blank = imagecreatetruecolor($width, $height); 
$tbg = imagecolorallocate($blank, 255, 255, 255); 
imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg); 
imagecolortransparent($blank, $tbg); 
if (($opacity < 0) OR ($opacity >100)) $opacity = 100; 
imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $opacity); 
imagepng($blank,$tmp_file_img); 

//Create PDF 
$pdf = new FPDI(); 
if (file_exists($tmp_file)){ 
    $pagecount = $pdf->setSourceFile($tmp_file); 
} else { 
    clear(); 
    return FALSE; 
} 

//Put the watermark on all pages 
for($i=1; $i <= $pagecount; $i++) { 
    $tpl = $pdf->importPage($i); 
    $pdf->addPage(); 
    $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE); 
    $pdf->Image($tmp_file_img, 1, 1, 0, 0, 'png'); 
} 

//Write PDF 
$pdf->Output($tmp_file, 'F'); 

有什么办法,以使人们有可能要么得到的位置处的颜色,所以我可以改变FONTCOLOR或使用两个透明色?

回答

0

解决了:

// determine image size 
$font = 4; 
$width = imagefontwidth($font) * strlen($text) + 2; 
$height = imagefontheight($font) + 2; 
// create transparent image 
$png = imagecreatetruecolor($width, $height); 
imageAlphaBlending($png, true); 
imagesavealpha($png, true); 
// background 
$color1 = imagecolorallocatealpha($png, 0, 0, 0, 127); 
imagefill($png, 0, 0, $color1); 
// foreground - text 
$color2 = imagecolorallocatealpha($png, 255, 255, 255, 126); 
imagestring($png, $font, 1, 1, $text, $color2); 
// save as png file 
imagepng($png, $fname);