gpt4 book ai didi

php - 如何使用 FPDF 和 PHP 保持图像质量?

转载 作者:行者123 更新时间:2023-12-03 01:38:23 24 4
gpt4 key购买 nike

我正在使用 FPDF 和 PHP 将图像添加到 PDF。但 PDF 中的图像质量比原始图像差很多,如下所示:

Print screen from web page Print screen from PDF

相关代码:

$image_height = 40;
$image_width = 40;
$pdf = new FPDF();
$pdf->AddPage();
$start_x = $pdf->GetX();
$start_y = $pdf->GetY();
$pdf->Image('./images/ds_pexeso_ros_0_17.jpg', $pdf->GetX(), $pdf->GetY(), $image_height, $image_width);
$pdf->Output("pexeso".date("Y-m-d"),"I");

原始图像为 150x150 像素。

最佳答案

我在客户项目中遇到了同样的问题。生成的 pdf 文档中的图片模糊,即使包含员工图像也是如此。

我花了几个小时,但这对我有用。

我查看了代码,发现 pdf 文档的构造函数中设置了比例因子:

//Scale factor
if($unit=='pt')
$this->k=1;
elseif($unit=='mm')
$this->k=72/25.4;
elseif($unit=='cm')
$this->k=72/2.54;
elseif($unit=='in')
$this->k=72;
else
$this->Error('Incorrect unit: '.$unit);

比例因子取决于 pdf 文档的构造函数中给出的值:

function FPDF($orientation='P',$unit='mm',$format='A4')

默认值为“mm”。在我的大多数文档中,我都会创建一个 pdf 文档,例如:

$pdf = new PDF('P');

这意味着将使用比例因子 72/25.4 = 2.83。当我在刚刚使用之前放置图像时:

$this->Image('path/to/file', 0, 0);

这样我就得到了模糊的图像。也可以在命令中给出图像的宽度

$this->Image('path/to/file', 0, 0, 200); // for a image width 200

这给了我一个太大的图像。但是 - 技巧来了 - 当您将实际宽度除以比例因子(在我的例子中为 2.83)并将其放入此语句中时,它会给出完美清晰的图像:

$this->Image('path/to/file', 0, 0, 71); // for a image width 200 / 2.83 = app 71

我希望这也适合你!

关于php - 如何使用 FPDF 和 PHP 保持图像质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10040309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com