TCPDF图像质量问题 [英] TCPDF image quality issues

查看:245
本文介绍了TCPDF图像质量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TCPDF生成PDF文档,而我正在尝试在标题中添加徽标,但是我的图像质量有问题,后来我发现也有缩放。

I'm using a TCPDF to generate PDF documents and I'm trying to add a logo into header, but I have a problem with the image quality and later as I found out, with scaling also.

使用TCPDF插入PDF文档的图像存在质量损失和尺寸问题。我不太关心尺寸,因为我可以改变它,但我无法恢复图像质量。

The images inserted into PDF documents with TCPDF suffer from quality losses and size issues. I don't care so much for the size because I can change it, but I can't restore the quality of image.

我尝试过:

  • TCPDF scaling and quality issue
  • tcpdf: poor image quality
  • I have also converted header into HTML code, same quality degradation occurrs.
  • Removed all other options for $pdf->Image() and left everything in default - image size increased to (presumably) original size with the same quality issues.

这是我用来插入图片的代码:

This is the code I'm using to insert an image:

$this->Image("../images/logo.jpg", 8, 10, 35, '', 'JPG', '', 'T', false, 0, '', false, false, 0, false, false, false);

这是 createPDF() function:

And this is the beginning of createPDF() function:

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator('PHP TC-PDF Generator');
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(8, 50, 8); // page margins
$pdf->SetAutoPageBreak(TRUE, 30);
$pdf->setImageScale(1);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 12, '', true);
$pdf->AddPage();
$pdf->setTextShadow(array('enabled'=>false));

还有其他推荐吗?

替代方案:
我愿意用任何其他PDF生成器更改TCPDF,如果它支持HTML表格并且可以插入质量更好的图像。

Alternative: I'm willing to change TCPDF with any other PDF generator, if it supports HTML tables and can insert images with better quality.

你的帮助将会很大赞赏。

Your help will be greatly appreciated.

推荐答案

要使图像更大,请增加定义的宽度。您似乎使用的默认单位为mm,因此您指定的宽度为35 mm。如果您想要更大的图像,请将该数字更改为所需的宽度。

To make the image bigger, increase the defined width. You appear to be using the default units of mm so you are specifying a width of 35 mm. If you want a bigger image, change that number to the desired width.

要获得更高质量的图像,请从更大的图像开始。您可能需要进行实验,但尝试增加1.53 1 倍。如果你把它做得太大,那么在缩放时它可能看起来不稳定。

For a better quality image, start with a bigger image. You may have to experiment, but try 1.531 times bigger. If you make it too big, then it may look choppy when scaled.

我会使用PNG。 PNG格式更适合具有文本和纯色的图像,就像您在那里一样。但如果您想使用JPG,那么尝试使用 noreferrer> SetJPEGQuality()方法。我相信默认值是75.

I would use PNG. The PNG format is better suited to images with text and solid colours like you have there. But If you want to use JPG, then try increasing the image quality with the SetJPEGQuality() method. I believe the default is 75.

如果指定尺寸,则图像比例值为不曾用过。如果您不想明确指定大小,则使用 setImageScale()方法。因此,如果您将图片设置为所需尺寸的1.53倍,请将比例设置为1.53。

If you specify a dimension, then the image scale value is not used. If you don't want to specify the size explicitly, then set the scale factor accordingly with the setImageScale() method. So if you make your image 1.53 times bigger than the desired size, set the scale to 1.53.

注意:文档中的说明IMI,setImageScale()具有误导性。它与将像素转换为用户单元并没有任何关系。该转换由内部脚本处理。

Note: The description in the docs for setImageScale() is misleading, IMO. It doesn't really have anything to do with converting pixels to user units. That conversion is handled by the script internally.

请注意图像看起来不太好在所有缩放级别,无论你做什么。您可以使用 SetDisplayMode()方法设置初始缩放级别。 TCPDF默认为页面宽度,在大多数现代台式机和笔记本电脑上将超过100%。

Be aware that the image will not look good at all zoom levels, regardless of what you do. You can set the initial zoom level with the SetDisplayMode() method. TCPDF defaults to page width, which will be more than 100% on most modern desktops and laptops.

传递给 Image()方法的维度使用已定义的用户单位(默认为mm)。虽然dpi和ppi之间存在一些技术差异,但它们仅适用于打印。

The dimensions passed to the Image() method use the defined user units (default is mm). While there are some technical differences between dpi and ppi, they only apply to print.

所以出于我们的目的:

72 pixels = 72 points = 1 inch = 2.54 cm = 25.4 mm

因此,如果您想要40毫米宽的图像,请尝试创建图像宽度为173像素。

Therefore, if you want an image 40 mm wide, try creating an image that is 173 pixels wide.

40 mm * 72 px / 25.4 mm * 1.53 scale factor






矢量图像



如果图像质量非常重要,则应考虑使用其中一种矢量格式。查看TCPDF手册,了解 ImageSvg() ImageEps()方法。

脚注


  1. 我发现在100%变焦时,当原始图像比所需尺寸大1.53倍时,质量有明显改善。当我第一次开始摆弄TCPDF和图像质量时,我偶然发现了1.53的数字。但是,从来没有任何解释这个数字的来源。我相信我终于弄明白了。 Adobe Reader默认使用110 ppi的分辨率,TCPDF创建72 dpi的文档。 110/72 = 1.53

  1. I've found that at 100% zoom, there is an appreciable improvement in quality when the original image is 1.53 times bigger than the desired size. When I first started fiddling with TCPDF and image quality, I came across the number 1.53 numerous times. However, there was never any explanation as to where that number came from. I believe I have finally figured that out. Adobe Reader uses a resolution of 110 ppi by default, and TCPDF creates a document that is 72 dpi. 110 / 72 = 1.53

这篇关于TCPDF图像质量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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