0

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.

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.

Michael
  • 32,527
  • 49
  • 210
  • 370
Sajin
  • 57
  • 1
  • 10

3 Answers3

0

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.

foxbeefly
  • 510
  • 3
  • 13
  • Thank you foxbeefly. But here my problem is, when I generate the pdf, it shows 'failed to load pdf'. In MPDF I'm outputting the image in an image tag like this $html.='
    ';
    – Sajin May 12 '15 at 06:02
  • You need to establish whether the error is with the image creation, or with the pdf creation. Run the create pdf code without the image included - does the pdf get created now? But looks like syntax error - you concatenation is incorrect: `$html.='
    – foxbeefly May 12 '15 at 06:11
  • pdf is generating fine. If I call $image->output() then the pdf doesn't generate and gives me the error. Now I am using only the image tag like `$html.='';`. I am able to create the pdf without this image tag. I guess there is some error in attaching the image with the pdf – Sajin May 12 '15 at 06:20
  • Read the notes: "The output function lets you output the image straight to the browser without having to save the file." So I am pretty sure you need to save the image, and then include the saved image file in the pdf. – foxbeefly May 12 '15 at 06:22
  • Yeah, I also had that option in my mind. But that will only consume the space in my server, right?. However, I will try that also and thank you very much for your support. – Sajin May 12 '15 at 06:29
  • You will only be storing the downsampled image file on your server. And if that is still a problem, delete the file once the PDF is created. – foxbeefly May 12 '15 at 06:38
0

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);
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
0

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

$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);
Senanayaka
  • 309
  • 6
  • 18