xref: /webtrees/app/Report/ReportHtmlImage.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22/**
23 * Class ReportHtmlImage
24 */
25class ReportHtmlImage extends ReportBaseImage
26{
27    /**
28     * Image renderer
29     *
30     * @param ReportHtml $renderer
31     *
32     * @return void
33     */
34    public function render($renderer)
35    {
36        static $lastpicbottom, $lastpicpage, $lastpicleft, $lastpicright;
37
38        // Get the current positions
39        if ($this->x === ReportBaseElement::CURRENT_POSITION) {
40            $this->x = $renderer->getX();
41        }
42        if ($this->y === ReportBaseElement::CURRENT_POSITION) {
43            //-- first check for a collision with the last picture
44            if (isset($lastpicbottom)) {
45                if (($renderer->pageNo() == $lastpicpage) && ($lastpicbottom >= $renderer->getY()) && ($this->x >= $lastpicleft) && ($this->x <= $lastpicright)) {
46                    $renderer->setY($lastpicbottom + ($renderer->cPadding * 2));
47                }
48            }
49            $this->y = $renderer->getY();
50        }
51
52        // Image alignment
53        switch ($this->align) {
54            case 'L':
55                echo '<div style="position:absolute;top:', $this->y, 'pt;left:0pt;width:', $renderer->getRemainingWidth(), "pt;text-align:left;\">\n";
56                echo '<img src="', $this->file, '" style="width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n</div>\n";
57                break;
58            case 'C':
59                echo '<div style="position:absolute;top:', $this->y, 'pt;left:0pt;width:', $renderer->getRemainingWidth(), "pt;text-align:center;\">\n";
60                echo '<img src="', $this->file, '" style="width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n</div>\n";
61                break;
62            case 'R':
63                echo '<div style="position:absolute;top:', $this->y, 'pt;left:0pt;width:', $renderer->getRemainingWidth(), "pt;text-align:right;\">\n";
64                echo '<img src="', $this->file, '" style="width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n</div>\n";
65                break;
66            default:
67                echo '<img src="', $this->file, '" style="position:absolute;', $renderer->alignRTL, ':', $this->x, 'pt;top:', $this->y, 'pt;width:', $this->width, 'pt;height:', $this->height, "pt;\" alt=\"\">\n";
68        }
69
70        $lastpicpage   = $renderer->pageNo();
71        $lastpicleft   = $this->x;
72        $lastpicright  = $this->x + $this->width;
73        $lastpicbottom = $this->y + $this->height;
74        // Setup for the next line
75        if ($this->line === 'N') {
76            $renderer->setY($lastpicbottom);
77        }
78        // Keep max Y updated
79        $renderer->addMaxY($lastpicbottom);
80    }
81
82    /**
83     * Get the image height
84     * This would be called from the TextBox only for multiple images
85     * so we add a bit bottom space between the images
86     *
87     * @param ReportHtml $renderer
88     *
89     * @return float
90     */
91    public function getHeight($renderer): float
92    {
93        return $this->height + ($renderer->cPadding * 2);
94    }
95}
96