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

che*_*nik 12 php image fpdf

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

从网页打印屏幕 从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");
Run Code Online (Sandbox Code Playgroud)

原始图像为150x150像素.

ehe*_*enr 9

我在客户项目中遇到了同样的问题.生成的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);
Run Code Online (Sandbox Code Playgroud)

scalefactor取决于pdf文档的构造函数中给出的值:

function FPDF($orientation='P',$unit='mm',$format='A4')
Run Code Online (Sandbox Code Playgroud)

默认值为"mm".在我的大多数文档中,我发起了一个pdf文档,如:

$pdf = new PDF('P');
Run Code Online (Sandbox Code Playgroud)

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

$this->Image('path/to/file', 0, 0);
Run Code Online (Sandbox Code Playgroud)

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

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

这给了我一个太大的图像.但是 - 这就是诀窍 - 当你用scalefactor(在我的情况下是2.83)划分实际宽度时,把它放在这个语句中它会给出一个非常清晰的图像:

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

我希望这也适合你!