tcoulson
tcoulson

Reputation: 624

Dompdf max-height max-width supported or not?

I am trying to restrict images to a certain height and width as a maximum. So naturally I am using max-height and max-width so the images can stay in their aspect ratios when they hit their max. Works in html, here is my code snippets

'<table class="seperate" id="images-table">'.   
        '<tbody>'.
          '<tr>'.
            '<td>&nbsp;</td>'.
            '<td class="uscs-logo" ><img src="http://localhost/dompdfTest/dompdf/uscompliancesystems_logo.png" /></td>'.

            '<td class="signature-logo" ><img src="http://localhost/dompdfTest/dompdf/USCSDefaultSignature.jpg" /></td>'.
            '<td>&nbsp;</td>'.
          '</tr>'.
        '</tbody>'.
    '</table>'.

And the css:

table#images-table .uscs-logo {
  height: 100px;
  text-align: left;

}

table#images-table .uscs-logo img{
    max-height:200px;
    max-width: 200px;
}
table#images-table .signature {
  height: 125px;
  text-align: right;
  width: 300px;
  height: auto;
  max-height:200px;
  max-width: 200px;
}

But what I get in the pdf is a two page pdf with no images on the page because they are coming in at full size. If I render in html to the page, it comes out fine.

So my question is, is max-width and max-height really supported with dompdf on img tag?

Upvotes: 0

Views: 3882

Answers (1)

Misunderstood
Misunderstood

Reputation: 5661

It's not that difficult to resize images on the fly with the GD PHP Extension. As far as time cost, the Browser has to scale if you don't.

$filename ='/home/user/public_html/images/image.jpg';
$image = @imagecreatefromjpeg();
$originalWidth  = imagesx($image);
$originalHeight = imagesy($image);
$scale      = min($desiredWidth/$originalWidth, $desiredHeight/$originalHeight);
$newWidth  = ceil($scale*$originalWidth);
$newHeight = ceil($scale*$originalHeight);
$newPic = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
if (imagejpeg($newPic,$tmpfile)){rename($tmpfile,$filename);}

And restore memory cleanup.

imagedestroy($image);
imagedestroy($newPic);

Upvotes: 1

Related Questions