如何使用 FPDF 和 PHP 保持图像质量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10040309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to maintain image quality with FPDF and PHP?
提问by chejnik
I'm using FPDF with PHP to add an image to a PDF. But the image quality in the PDF is much worse than the original image, as you can see here:
我正在使用 FPDF 和 PHP 将图像添加到 PDF。但是 PDF 中的图像质量比原始图像差很多,如下所示:




Relevant code:
相关代码:
$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");
The original image is 150x150 pixels.
原始图像为 150x150 像素。
采纳答案by eheydenr
I faced the same problem in projects for customers. Blurry pictures in a generated pdf document even with hires images.
我在客户项目中遇到了同样的问题。生成的 pdf 文档中的图片模糊,即使是雇佣图片。
It took me a couple of hours, but this is what worked for me.
我花了几个小时,但这对我有用。
I have a taken a look at the code and saw that there was a scale factor being set in the constructor of the pdf document:
我看了一下代码,发现在 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);
The scalefactor is depending on the value given in the constructor of the pdf document:
比例因子取决于 pdf 文档的构造函数中给出的值:
function FPDF($orientation='P',$unit='mm',$format='A4')
The default is 'mm'. In most of my documents I initiate a pdf document like:
默认值为“毫米”。在我的大多数文档中,我启动了一个 pdf 文档,例如:
$pdf = new PDF('P');
This means that there will be a scalefactor of 72/25.4 = 2.83 used. When I placed an image before I just used:
这意味着将使用 72/25.4 = 2.83 的比例因子。当我在刚使用之前放置图像时:
$this->Image('path/to/file', 0, 0);
This way I got the blurry images. It is also possible to give the width of the image in the command
这样我得到了模糊的图像。也可以在命令中给出图像的宽度
$this->Image('path/to/file', 0, 0, 200); // for a image width 200
This gave me an image that was far too large. But - and here comes the trick - when you divide the real width by the scalefactor (in my case 2.83) and put this in this statement it gives a perfectly sharp image:
这给了我一个太大的图像。但是 - 技巧来了 - 当您将实际宽度除以比例因子(在我的情况下为 2.83)并将其放在此语句中时,它会给出一个非常清晰的图像:
$this->Image('path/to/file', 0, 0, 71); // for a image width 200 / 2.83 = app 71
I hope this works for you too!
我希望这对你也有用!
回答by team.flashindia
FPDF with a statement like this to set the user unit to mm $pdf=new FPDF('P','mm','Letter');
FPDF 用这样的语句将用户单位设置为 mm $pdf=new FPDF('P','mm','Letter');
<?php
require_once('fpdf.php');
$image_height = 40;
$image_width = 40;
$pdf = new FPDF('P','mm','Letter');
$pdf->AddPage();
$start_x = $pdf->GetX();
$start_y = $pdf->GetY();
$pdf->Image('./images/ds_pexeso_ros_0_17.jpg',$start_x+0,$start_y-2,40);
$pdf->Output("pexeso".date("Y-m-d"),"I");
?>
FPDF made a very good looking result.
FPDF 取得了非常好看的结果。
回答by ab_dev86
I think the problem could be related to:
我认为问题可能与:
$image_height = 40;
$image_width = 40;
With these two instructions your are setting the dimensions the image will have in the pdf.
通过这两条说明,您可以设置图像在 pdf 中的尺寸。
But if the original image is bigger than 40x40 the scaling of the image can cause quality problem.
但是如果原始图像大于 40x40,图像的缩放会导致质量问题。
So what i suggest:
所以我的建议是:
- Do a correct resize of the image (php provides GD library). Resize it to 40x40. The GD function imagecopyresampled is your friend: resize and resample the image! Complete reference: http://www.php.net/manual/en/function.imagecopyresampled.php
- Insert now the image in the pdf
- 正确调整图像大小(php 提供 GD 库)。将其调整为 40x40。GD 函数 imagecopyresampled 是您的朋友:调整图像大小和重新采样!完整参考:http: //www.php.net/manual/en/function.imagecopyresampled.php
- 现在在pdf中插入图像

