我目前正在尝试生成水印并在下载文件之前将它们放入PDF(FPDI)。到目前为止它的工作完美,除非背景颜色不是白色,因为那时你可以看到字体颜色
所以我的问题是如何在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');
有没有办法让这个位置的颜色可以改变字体颜色或使用两种透明颜色?
答案 0 :(得分: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);