PHP - 使用在线pdf(mpdf)附加图像时调整图像大小

时间:2022-10-20 20:46:27

I am generating a pdf with some images in it. Since the images are large in size, the size of the generated PDF will also be very large. Is there a way to dynamically reduce the size of the image while attaching it to the pdf.

我正在生成带有一些图像的pdf。由于图像尺寸较大,因此生成的PDF的尺寸也将非常大。有没有办法在将图像附加到pdf时动态缩小图像的大小。

I tried something like this from here

我从这里尝试过类似的东西

`header('Content-Type: image/jpeg');`
`include('SimpleImage.php');`
`$image = new SimpleImage();`
`$image->load($imgPath);`
`$image->resizeToWidth(150);`
`$image->output();`

note1 : $imgPath is passed from a loop and I'm using MPDF to generate the pdf.

note1:$ imgPath从循环传递,我使用MPDF生成pdf。

3 个解决方案

#1


If you look in the Simple_Image code that you are using, you will see the imagecopyresampled function in the resize method. This is called in turn by the resizeToWidth method - so your image IS being resampled.

如果查看正在使用的Simple_Image代码,您将在resize方法中看到imagecopyresampled函数。这由resizeToWidth方法依次调用 - 因此您的图像被重新采样。

#2


This works well and is fairly quick.

这很好,而且相当快。

$img = imagecreatefromjpeg('image.jpg');
$originalWidth  = imagesx($image);
$originalHeight = imagesy($image);
$scale      = min($previewWidth/$originalWidth, $previewHeight/$originalHeight);
$newWidth  = ceil($scale*$originalWidth);
$newHeight = ceil($scale*$originalHeight);
$newPic = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
ob_start();
imagejpeg($newPic, NULL, 70);
$jpg = ob_get_clean();
ob_clean();
$fp = fopen('newPic.jpg','w');
fwrite($fp,$jpg);
fclose($fp);

#3


Here is my header and this is how i used mpdf hope this will helps you

这是我的标题,这是我如何使用mpdf希望这将有助于你

$htmlTable_header='<style></style><table width="100%" border="0" style="border-collapse:collapse;margin-top:-40px;font-family:arial !important; font-size:11px;" cellspacing="0" cellpadding="0">
              <tr>
                <td width="10">&nbsp;</td>
                <td width="150" valign="top"><img src="http://localhost/abc/images/abc_logo.png" width="150" height="100" border="0" alt="" title="abc" />';


$mpdf->SetHTMLHeader($htmlTable_header);
$mpdf->SetHTMLFooter($htmlTable_footer);

$mpdf->WriteHTML($htmlTable_body);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output('service_report_pdf/'.$_REQUEST['id'].'.pdf',F);

#1


If you look in the Simple_Image code that you are using, you will see the imagecopyresampled function in the resize method. This is called in turn by the resizeToWidth method - so your image IS being resampled.

如果查看正在使用的Simple_Image代码,您将在resize方法中看到imagecopyresampled函数。这由resizeToWidth方法依次调用 - 因此您的图像被重新采样。

#2


This works well and is fairly quick.

这很好,而且相当快。

$img = imagecreatefromjpeg('image.jpg');
$originalWidth  = imagesx($image);
$originalHeight = imagesy($image);
$scale      = min($previewWidth/$originalWidth, $previewHeight/$originalHeight);
$newWidth  = ceil($scale*$originalWidth);
$newHeight = ceil($scale*$originalHeight);
$newPic = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
ob_start();
imagejpeg($newPic, NULL, 70);
$jpg = ob_get_clean();
ob_clean();
$fp = fopen('newPic.jpg','w');
fwrite($fp,$jpg);
fclose($fp);

#3


Here is my header and this is how i used mpdf hope this will helps you

这是我的标题,这是我如何使用mpdf希望这将有助于你

$htmlTable_header='<style></style><table width="100%" border="0" style="border-collapse:collapse;margin-top:-40px;font-family:arial !important; font-size:11px;" cellspacing="0" cellpadding="0">
              <tr>
                <td width="10">&nbsp;</td>
                <td width="150" valign="top"><img src="http://localhost/abc/images/abc_logo.png" width="150" height="100" border="0" alt="" title="abc" />';


$mpdf->SetHTMLHeader($htmlTable_header);
$mpdf->SetHTMLFooter($htmlTable_footer);

$mpdf->WriteHTML($htmlTable_body);
$mpdf->SetDisplayMode('fullpage');

$mpdf->Output('service_report_pdf/'.$_REQUEST['id'].'.pdf',F);