How to maintain image quality with FPDF and PHP?

让人想犯罪 __ 提交于 2020-08-22 09:37:39

问题


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:

Print screen from web pagePrint screen from 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.


回答1:


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.

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);

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

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

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

$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:

$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:

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

I hope this works for you too!




回答2:


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.

But if the original image is bigger than 40x40 the scaling of the image can cause quality problem.

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



回答3:


FPDF with a statement like this to set the user unit to 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.



来源:https://stackoverflow.com/questions/10040309/how-to-maintain-image-quality-with-fpdf-and-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!