PHP FPDF 与透明 PNG 的速度非常慢

发布于 2024-12-01 09:50:28 字数 1728 浏览 4 评论 0原文

我正在使用 FPDF 以及我在网上找到的名为 TransFPDF 的扩展,该扩展允许将透明的 PNG 放入我用 PHP 动态生成的 PDF 中。然而,我遇到的问题是 PDF 需要很长时间才能生成(当我嵌入大约 6 个字符时,脚本运行大约需要 30 秒,其中每个字符都是透明的 PNG。这次还包括文本和背景,但我检查了时间,这些只需要大约一两秒,并且不会减慢代码速度)。

我发现主要的慢点是以下功能:

// needs GD 2.x extension 
// pixel-wise operation, not very fast 
function ImagePngWithAlpha($file,$x,$y,$w=0,$h=0,$link='') 
{ 
$tmp_alpha = tempnam('.', 'mska'); 
$this->tmpFiles[] = $tmp_alpha; 
$tmp_plain = tempnam('.', 'mskp'); 
$this->tmpFiles[] = $tmp_plain; 

list($wpx, $hpx) = getimagesize($file); 
$img = imagecreatefrompng($file); 
$alpha_img = imagecreate( $wpx, $hpx ); 

// generate gray scale pallete 
for($c=0;$c<256;$c++) ImageColorAllocate($alpha_img, $c, $c, $c); 

// extract alpha channel 
$xpx=0; 
while ($xpx<$wpx){ 
    $ypx = 0; 
    while ($ypx<$hpx){ 
        $color_index = imagecolorat($img, $xpx, $ypx); 
        $alpha = 255-($color_index>>24)*255/127; // GD alpha component: 7 bit only, 0..127! 
        imagesetpixel($alpha_img, $xpx, $ypx, $alpha); 
    ++$ypx; 
    } 
    ++$xpx; 
} 

imagepng($alpha_img, $tmp_alpha); 
imagedestroy($alpha_img); 

// extract image without alpha channel 
$plain_img = imagecreatetruecolor ( $wpx, $hpx ); 
imagecopy ($plain_img, $img, 0, 0, 0, 0, $wpx, $hpx ); 
imagepng($plain_img, $tmp_plain); 
imagedestroy($plain_img); 

//first embed mask image (w, h, x, will be ignored) 
$maskImg = $this->Image($tmp_alpha, 0,0,0,0, 'PNG', '', true); 

//embed image, masked with previously embedded mask 
$this->Image($tmp_plain,$x,$y,$w,$h,'PNG',$link, false, $maskImg); 
} 

有人知道如何加快速度吗?我似乎无法让它的速度超过每个字符约 4 秒,这确实加起来很快(一个字符可能约为 1000x2000 像素,是的,我知道这很多,但是,是的,这对于可打印的 PDF 来说是必要的) 。

谢谢。

I'm using FPDF with an extension I found online called TransFPDF that allows for transparent PNG's to be put into the PDF that I am dynamically generating with PHP. The problem I am having however is that the PDF takes a long time to generate (the script takes about 30 seconds to run when I am embedding about 6 characters, where each characters is a transparent PNG. This time also includes text and the background but I checked times and those only take about a second or two and are not slowing down the code).

I have found that the main slow point is the following function:

// needs GD 2.x extension 
// pixel-wise operation, not very fast 
function ImagePngWithAlpha($file,$x,$y,$w=0,$h=0,$link='') 
{ 
$tmp_alpha = tempnam('.', 'mska'); 
$this->tmpFiles[] = $tmp_alpha; 
$tmp_plain = tempnam('.', 'mskp'); 
$this->tmpFiles[] = $tmp_plain; 

list($wpx, $hpx) = getimagesize($file); 
$img = imagecreatefrompng($file); 
$alpha_img = imagecreate( $wpx, $hpx ); 

// generate gray scale pallete 
for($c=0;$c<256;$c++) ImageColorAllocate($alpha_img, $c, $c, $c); 

// extract alpha channel 
$xpx=0; 
while ($xpx<$wpx){ 
    $ypx = 0; 
    while ($ypx<$hpx){ 
        $color_index = imagecolorat($img, $xpx, $ypx); 
        $alpha = 255-($color_index>>24)*255/127; // GD alpha component: 7 bit only, 0..127! 
        imagesetpixel($alpha_img, $xpx, $ypx, $alpha); 
    ++$ypx; 
    } 
    ++$xpx; 
} 

imagepng($alpha_img, $tmp_alpha); 
imagedestroy($alpha_img); 

// extract image without alpha channel 
$plain_img = imagecreatetruecolor ( $wpx, $hpx ); 
imagecopy ($plain_img, $img, 0, 0, 0, 0, $wpx, $hpx ); 
imagepng($plain_img, $tmp_plain); 
imagedestroy($plain_img); 

//first embed mask image (w, h, x, will be ignored) 
$maskImg = $this->Image($tmp_alpha, 0,0,0,0, 'PNG', '', true); 

//embed image, masked with previously embedded mask 
$this->Image($tmp_plain,$x,$y,$w,$h,'PNG',$link, false, $maskImg); 
} 

Does anyone have any ideas of how I could speed this up? I can't seem to get it to go faster than about 4 seconds per character which really adds up fast (a character probably is about 1000x2000 pixels, and yes I know this is a lot, but yes it is neccessary for a printable PDF).

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文