image_frame_reflower.cls.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /**
  10. * Image reflower class
  11. *
  12. * @access private
  13. * @package dompdf
  14. */
  15. class Image_Frame_Reflower extends Frame_Reflower {
  16. function __construct(Image_Frame_Decorator $frame) {
  17. parent::__construct($frame);
  18. }
  19. function reflow(Block_Frame_Decorator $block = null) {
  20. $this->_frame->position();
  21. //FLOAT
  22. //$frame = $this->_frame;
  23. //$page = $frame->get_root();
  24. //$enable_css_float = $this->get_dompdf()->get_option("enable_css_float");
  25. //if ($enable_css_float && $frame->get_style()->float !== "none" ) {
  26. // $page->add_floating_frame($this);
  27. //}
  28. // Set the frame's width
  29. $this->get_min_max_width();
  30. if ( $block ) {
  31. $block->add_frame_to_line($this->_frame);
  32. }
  33. }
  34. function get_min_max_width() {
  35. if (DEBUGPNG) {
  36. // Determine the image's size. Time consuming. Only when really needed?
  37. list($img_width, $img_height) = dompdf_getimagesize($this->_frame->get_image_url());
  38. print "get_min_max_width() ".
  39. $this->_frame->get_style()->width.' '.
  40. $this->_frame->get_style()->height.';'.
  41. $this->_frame->get_parent()->get_style()->width." ".
  42. $this->_frame->get_parent()->get_style()->height.";".
  43. $this->_frame->get_parent()->get_parent()->get_style()->width.' '.
  44. $this->_frame->get_parent()->get_parent()->get_style()->height.';'.
  45. $img_width. ' '.
  46. $img_height.'|' ;
  47. }
  48. $style = $this->_frame->get_style();
  49. $width_forced = true;
  50. $height_forced = true;
  51. //own style auto or invalid value: use natural size in px
  52. //own style value: ignore suffix text including unit, use given number as px
  53. //own style %: walk up parent chain until found available space in pt; fill available space
  54. //
  55. //special ignored unit: e.g. 10ex: e treated as exponent; x ignored; 10e completely invalid ->like auto
  56. $width = ($style->width > 0 ? $style->width : 0);
  57. if ( is_percent($width) ) {
  58. $t = 0.0;
  59. for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) {
  60. $f_style = $f->get_style();
  61. $t = $f_style->length_in_pt($f_style->width);
  62. if ($t != 0) {
  63. break;
  64. }
  65. }
  66. $width = ((float)rtrim($width,"%") * $t)/100; //maybe 0
  67. } elseif ( !mb_strpos($width, 'pt') ) {
  68. // Don't set image original size if "%" branch was 0 or size not given.
  69. // Otherwise aspect changed on %/auto combination for width/height
  70. // Resample according to px per inch
  71. // See also List_Bullet_Image_Frame_Decorator::__construct
  72. $width = $style->length_in_pt($width);
  73. }
  74. $height = ($style->height > 0 ? $style->height : 0);
  75. if ( is_percent($height) ) {
  76. $t = 0.0;
  77. for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) {
  78. $f_style = $f->get_style();
  79. $t = $f_style->length_in_pt($f_style->height);
  80. if ($t != 0) {
  81. break;
  82. }
  83. }
  84. $height = ((float)rtrim($height,"%") * $t)/100; //maybe 0
  85. } elseif ( !mb_strpos($height, 'pt') ) {
  86. // Don't set image original size if "%" branch was 0 or size not given.
  87. // Otherwise aspect changed on %/auto combination for width/height
  88. // Resample according to px per inch
  89. // See also List_Bullet_Image_Frame_Decorator::__construct
  90. $height = $style->length_in_pt($height);
  91. }
  92. if ($width == 0 || $height == 0) {
  93. // Determine the image's size. Time consuming. Only when really needed!
  94. list($img_width, $img_height) = dompdf_getimagesize($this->_frame->get_image_url());
  95. // don't treat 0 as error. Can be downscaled or can be catched elsewhere if image not readable.
  96. // Resample according to px per inch
  97. // See also List_Bullet_Image_Frame_Decorator::__construct
  98. if ($width == 0 && $height == 0) {
  99. $dpi = $this->_frame->get_dompdf()->get_option("dpi");
  100. $width = (float)($img_width * 72) / $dpi;
  101. $height = (float)($img_height * 72) / $dpi;
  102. $width_forced = false;
  103. $height_forced = false;
  104. } elseif ($height == 0 && $width != 0) {
  105. $height_forced = false;
  106. $height = ($width / $img_width) * $img_height; //keep aspect ratio
  107. } elseif ($width == 0 && $height != 0) {
  108. $width_forced = false;
  109. $width = ($height / $img_height) * $img_width; //keep aspect ratio
  110. }
  111. }
  112. // Handle min/max width/height
  113. if ( $style->min_width !== "none" ||
  114. $style->max_width !== "none" ||
  115. $style->min_height !== "none" ||
  116. $style->max_height !== "none" ) {
  117. list(/*$x*/, /*$y*/, $w, $h) = $this->_frame->get_containing_block();
  118. $min_width = $style->length_in_pt($style->min_width, $w);
  119. $max_width = $style->length_in_pt($style->max_width, $w);
  120. $min_height = $style->length_in_pt($style->min_height, $h);
  121. $max_height = $style->length_in_pt($style->max_height, $h);
  122. if ( $max_width !== "none" && $width > $max_width ) {
  123. if ( !$height_forced ) {
  124. $height *= $max_width / $width;
  125. }
  126. $width = $max_width;
  127. }
  128. if ( $min_width !== "none" && $width < $min_width ) {
  129. if ( !$height_forced ) {
  130. $height *= $min_width / $width;
  131. }
  132. $width = $min_width;
  133. }
  134. if ( $max_height !== "none" && $height > $max_height ) {
  135. if ( !$width_forced ) {
  136. $width *= $max_height / $height;
  137. }
  138. $height = $max_height;
  139. }
  140. if ( $min_height !== "none" && $height < $min_height ) {
  141. if ( !$width_forced ) {
  142. $width *= $min_height / $height;
  143. }
  144. $height = $min_height;
  145. }
  146. }
  147. if (DEBUGPNG) print $width.' '.$height.';';
  148. $style->width = $width . "pt";
  149. $style->height = $height . "pt";
  150. $style->min_width = "none";
  151. $style->max_width = "none";
  152. $style->min_height = "none";
  153. $style->max_height = "none";
  154. return array( $width, $width, "min" => $width, "max" => $width);
  155. }
  156. }