如何使用FPDF和PHP保持图像质量? [英] How to maintain image quality with FPDF and PHP?

查看:57
本文介绍了如何使用FPDF和PHP保持图像质量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将FPDF与PHP结合使用,以将图像添加到PDF.但是,PDF的图像质量比原始图像差很多,如您所见:

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:

相关代码:

$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像素.

The original image is 150x150 pixels.

推荐答案

在面向客户的项目中,我遇到了同样的问题. 生成的pdf文档中的图片模糊不清,甚至包含雇用的图片.

I faced the same problem in projects for customers. Blurry pictures in a generated pdf document even with hires images.

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

It took me a couple of hours, but this is what worked for me.

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

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:

//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文档的构造函数中给出的值:

The scalefactor is depending on the value given in the constructor of the pdf document:

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

默认值为"mm".在我的大多数文档中,我都会启动pdf文档,例如:

The default is 'mm'. In most of my documents I initiate a pdf document like:

$pdf = new PDF('P');

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

This means that there will be a scalefactor of 72/25.4 = 2.83 used. When I placed an image before I just used:

$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

这给了我一张太大的图像.但是-诀窍到了-当您将实际宽度除以比例因子(在我的示例中为2.83)并将其放入此语句中时,它会给出非常清晰的图像:

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:

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

我希望这对您也有用!

这篇关于如何使用FPDF和PHP保持图像质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆