composer package updates
[openemr.git] / vendor / dompdf / dompdf / src / FrameReflower / Image.php
blob805523c52ceaa46a7297b82749195d346437c889
1 <?php
2 /**
3 * @package dompdf
4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Fabien Ménager <fabien.menager@gmail.com>
7 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
8 */
9 namespace Dompdf\FrameReflower;
11 use Dompdf\Helpers;
12 use Dompdf\FrameDecorator\Block as BlockFrameDecorator;
13 use Dompdf\FrameDecorator\Image as ImageFrameDecorator;
15 /**
16 * Image reflower class
18 * @package dompdf
20 class Image extends AbstractFrameReflower
23 /**
24 * Image constructor.
25 * @param ImageFrameDecorator $frame
27 function __construct(ImageFrameDecorator $frame)
29 parent::__construct($frame);
32 /**
33 * @param BlockFrameDecorator|null $block
35 function reflow(BlockFrameDecorator $block = null)
37 $this->_frame->position();
39 //FLOAT
40 //$frame = $this->_frame;
41 //$page = $frame->get_root();
43 //if ($frame->get_style()->float !== "none" ) {
44 // $page->add_floating_frame($this);
45 //}
47 // Set the frame's width
48 $this->get_min_max_width();
50 if ($block) {
51 $block->add_frame_to_line($this->_frame);
55 /**
56 * @return array
58 function get_min_max_width()
60 if ($this->get_dompdf()->getOptions()->getDebugPng()) {
61 // Determine the image's size. Time consuming. Only when really needed?
62 list($img_width, $img_height) = Helpers::dompdf_getimagesize($this->_frame->get_image_url(), $this->get_dompdf()->getHttpContext());
63 print "get_min_max_width() " .
64 $this->_frame->get_style()->width . ' ' .
65 $this->_frame->get_style()->height . ';' .
66 $this->_frame->get_parent()->get_style()->width . " " .
67 $this->_frame->get_parent()->get_style()->height . ";" .
68 $this->_frame->get_parent()->get_parent()->get_style()->width . ' ' .
69 $this->_frame->get_parent()->get_parent()->get_style()->height . ';' .
70 $img_width . ' ' .
71 $img_height . '|';
74 $style = $this->_frame->get_style();
76 $width_forced = true;
77 $height_forced = true;
79 //own style auto or invalid value: use natural size in px
80 //own style value: ignore suffix text including unit, use given number as px
81 //own style %: walk up parent chain until found available space in pt; fill available space
83 //special ignored unit: e.g. 10ex: e treated as exponent; x ignored; 10e completely invalid ->like auto
85 $width = ($style->width > 0 ? $style->width : 0);
86 if (Helpers::is_percent($width)) {
87 $t = 0.0;
88 for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) {
89 $f_style = $f->get_style();
90 $t = $f_style->length_in_pt($f_style->width);
91 if ($t != 0) {
92 break;
95 $width = ((float)rtrim($width, "%") * $t) / 100; //maybe 0
96 } else {
97 // Don't set image original size if "%" branch was 0 or size not given.
98 // Otherwise aspect changed on %/auto combination for width/height
99 // Resample according to px per inch
100 // See also ListBulletImage::__construct
101 $width = $style->length_in_pt($width);
104 $height = ($style->height > 0 ? $style->height : 0);
105 if (Helpers::is_percent($height)) {
106 $t = 0.0;
107 for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) {
108 $f_style = $f->get_style();
109 $t = (float)$f_style->length_in_pt($f_style->height);
110 if ($t != 0) {
111 break;
114 $height = ((float)rtrim($height, "%") * $t) / 100; //maybe 0
115 } else {
116 // Don't set image original size if "%" branch was 0 or size not given.
117 // Otherwise aspect changed on %/auto combination for width/height
118 // Resample according to px per inch
119 // See also ListBulletImage::__construct
120 $height = $style->length_in_pt($height);
123 if ($width == 0 || $height == 0) {
124 // Determine the image's size. Time consuming. Only when really needed!
125 list($img_width, $img_height) = Helpers::dompdf_getimagesize($this->_frame->get_image_url(), $this->get_dompdf()->getHttpContext());
127 // don't treat 0 as error. Can be downscaled or can be catched elsewhere if image not readable.
128 // Resample according to px per inch
129 // See also ListBulletImage::__construct
130 if ($width == 0 && $height == 0) {
131 $dpi = $this->_frame->get_dompdf()->getOptions()->getDpi();
132 $width = (float)($img_width * 72) / $dpi;
133 $height = (float)($img_height * 72) / $dpi;
134 $width_forced = false;
135 $height_forced = false;
136 } elseif ($height == 0 && $width != 0) {
137 $height_forced = false;
138 $height = ($width / $img_width) * $img_height; //keep aspect ratio
139 } elseif ($width == 0 && $height != 0) {
140 $width_forced = false;
141 $width = ($height / $img_height) * $img_width; //keep aspect ratio
145 // Handle min/max width/height
146 if ($style->min_width !== "none" ||
147 $style->max_width !== "none" ||
148 $style->min_height !== "none" ||
149 $style->max_height !== "none"
152 list( /*$x*/, /*$y*/, $w, $h) = $this->_frame->get_containing_block();
154 $min_width = $style->length_in_pt($style->min_width, $w);
155 $max_width = $style->length_in_pt($style->max_width, $w);
156 $min_height = $style->length_in_pt($style->min_height, $h);
157 $max_height = $style->length_in_pt($style->max_height, $h);
159 if ($max_width !== "none" && $width > $max_width) {
160 if (!$height_forced) {
161 $height *= $max_width / $width;
164 $width = $max_width;
167 if ($min_width !== "none" && $width < $min_width) {
168 if (!$height_forced) {
169 $height *= $min_width / $width;
172 $width = $min_width;
175 if ($max_height !== "none" && $height > $max_height) {
176 if (!$width_forced) {
177 $width *= $max_height / $height;
180 $height = $max_height;
183 if ($min_height !== "none" && $height < $min_height) {
184 if (!$width_forced) {
185 $width *= $min_height / $height;
188 $height = $min_height;
192 if ($this->get_dompdf()->getOptions()->getDebugPng()) {
193 print $width . ' ' . $height . ';';
196 $style->width = $width . "pt";
197 $style->height = $height . "pt";
199 $style->min_width = "none";
200 $style->max_width = "none";
201 $style->min_height = "none";
202 $style->max_height = "none";
204 return array($width, $width, "min" => $width, "max" => $width);