Home > Software design >  PDFlib PHP image boxsize with set height and dynamic width
PDFlib PHP image boxsize with set height and dynamic width

Time:11-18

In PDFlib we can define the boxsize for an image using boxsize={65 65} (for example). Since we need two values, is there any way to set the height value and leave the width value to be dynamically? In my use case I want to add all images with a set height of 65, but the images can be different in their width so the boxsize should dynamically update according to the image.

I also tried using scale in the option list, but that makes no sense, since the height than also changes according to the image.

Currently I load all images I want to place using a for loop, which looks like this:

foreach ($awardImages as $awardImage) {
            $image = $p->load_image('auto', $awardImage, '');
            if (0 == $image) {
                echo "Couldn't load $image: ".$p->get_errmsg();
                exit(1);
            }

            if ($x > (565 - 70)) {
                $y = $y - 65;
                $x = $elementStartLeft   2;
            }

            
            // $buf = "scale=1 position={left center} matchbox={name=awardimage}";
            $buf = 'boxsize={65 65} fitmethod=auto showborder position={left center} matchbox={name=awardimage}';
            $p->fit_image($image, $x, $y, $buf);
              $awardNo;
            $awardX2 = $p->info_matchbox('awardimage', $awardNo, 'x2');
            $x = $awardX2   5;
        }

CodePudding user response:

I think the easiest way is simply to specify the height or width a large value. This could look like this, for example

$p->fit_image($image, 0.0, 0.0, "boxsize {65 1000} fitmethod=meet position={left center}");
$p->fit_image($image, 0, 300,  "boxsize {1000 65} fitmethod=meet position={left center}");

It could make sense to set the large width or height (here 1000) to a value that might prevent the content from protruding over the page.

The value here (1000) would suffice for images with a ratio of 1:15 at the value 65.

In general, you can also use info_image() to determine the height and width of the image and thus determine the aspect ratio. Based on this, you could also determine and specify the box size yourself.

  • Related