image_frame_reflower.cls.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: image_frame_reflower.cls.php,v $
  6. * Created on: 2004-08-08
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  37. * @package dompdf
  38. *
  39. * Changes
  40. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  41. * @version 0.5.1.htischer.20090507
  42. * - Fix image size as percent of wrapping box
  43. * - Fix arithmetic rounding of image size
  44. * - Time consuming additional image file scan only when really needed
  45. */
  46. /* $Id: image_frame_reflower.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  47. /**
  48. * Image reflower class
  49. *
  50. * @access private
  51. * @package dompdf
  52. */
  53. class Image_Frame_Reflower extends Frame_Reflower {
  54. function __construct(Image_Frame_Decorator $frame) {
  55. parent::__construct($frame);
  56. }
  57. function reflow() {
  58. // Set the frame's width
  59. $this->get_min_max_width();
  60. }
  61. function get_min_max_width() {
  62. if (DEBUGPNG) {
  63. // Determine the image's size. Time consuming. Only when really needed?
  64. list($img_width, $img_height) = getimagesize($this->_frame->get_image_url());
  65. print "get_min_max_width() ".
  66. $this->_frame->get_style()->width.' '.
  67. $this->_frame->get_style()->height.';'.
  68. $this->_frame->get_parent()->get_style()->width." ".
  69. $this->_frame->get_parent()->get_style()->height.";".
  70. $this->_frame->get_parent()->get_parent()->get_style()->width.' '.
  71. $this->_frame->get_parent()->get_parent()->get_style()->height.';'.
  72. $img_width. ' '.
  73. $img_height.'|' ;
  74. }
  75. // We need to check both the *parent's* style as well as the current node's because images are wrapped...
  76. $style = $this->_frame->get_style();
  77. $stylep = $this->_frame->get_parent()->get_style();
  78. //own style auto or invalid value: use natural size in px
  79. //own style value: ignore suffix text including unit, use given number as px
  80. //own style %: walk up parent chain until found available space in pt; fill available space
  81. //
  82. //special ignored unit: e.g. 10ex: e treated as exponent; x ignored; 10e completely invalid ->like auto
  83. $width = ($style->width > 0 ? $style->width : ($stylep->width > 0 ? $stylep->width : 0));
  84. if ( is_percent($width) ) {
  85. $t = 0.0;
  86. for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) {
  87. $t = (float)($f->get_style()->width); //always in pt
  88. if ((float)$t != 0) {
  89. break;
  90. }
  91. }
  92. $width = ((float)rtrim($width,"%") * $t)/100; //maybe 0
  93. } else {
  94. // Don't set image original size if "%" branch was 0 or size not given.
  95. // Otherwise aspect changed on %/auto combination for width/height
  96. // Resample according to px per inch
  97. // See also List_Bullet_Image_Frame_Decorator::__construct
  98. $width = (float)($width * 72) / DOMPDF_DPI;
  99. }
  100. $height = ($style->height > 0 ? $style->height : ($stylep->height > 0 ? $stylep->height : 0));
  101. if ( is_percent($height) ) {
  102. $t = 0.0;
  103. for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) {
  104. $t = (float)($f->get_style()->height); //always in pt
  105. if ((float)$t != 0) {
  106. break;
  107. }
  108. }
  109. $height = ((float)rtrim($height,"%") * $t)/100; //maybe 0
  110. } else {
  111. // Don't set image original size if "%" branch was 0 or size not given.
  112. // Otherwise aspect changed on %/auto combination for width/height
  113. // Resample according to px per inch
  114. // See also List_Bullet_Image_Frame_Decorator::__construct
  115. $height = (float)($height * 72) / DOMPDF_DPI;
  116. }
  117. if ($width == 0 || $height == 0) {
  118. // Determine the image's size. Time consuming. Only when really needed!
  119. list($img_width, $img_height) = getimagesize($this->_frame->get_image_url());
  120. // don't treat 0 as error. Can be downscaled or can be catched elsewhere if image not readable.
  121. // Resample according to px per inch
  122. // See also List_Bullet_Image_Frame_Decorator::__construct
  123. if ($width == 0 && $height == 0) {
  124. $width = (float)($img_width * 72) / DOMPDF_DPI;
  125. $height = (float)($img_height * 72) / DOMPDF_DPI;
  126. } elseif ($height == 0 && $width != 0) {
  127. $height = ($width / $img_width) * $img_height; //keep aspect ratio
  128. } elseif ($width == 0 && $height != 0) {
  129. $width = ($height / $img_height) * $img_width; //keep aspect ratio
  130. }
  131. }
  132. if (DEBUGPNG) print $width.' '.$height.';';
  133. // Synchronize the styles
  134. $inner_style = $this->_frame->get_style();
  135. $inner_style->width = $style->width = $width . "pt";
  136. $inner_style->height = $style->height = $height . "pt";
  137. $inner_style->padding_top = $style->padding_top;
  138. $inner_style->padding_right = $style->padding_right;
  139. $inner_style->padding_bottom = $style->padding_bottom;
  140. $inner_style->padding_left = $style->padding_left;
  141. $inner_style->border_top_width = $style->border_top_width;
  142. $inner_style->border_right_width = $style->border_right_width;
  143. $inner_style->border_bottom_width = $style->border_bottom_width;
  144. $inner_style->border_left_width = $style->border_left_width;
  145. $inner_style->border_top_style = $style->border_top_style;
  146. $inner_style->border_right_style = $style->border_right_style;
  147. $inner_style->border_bottom_style = $style->border_bottom_style;
  148. $inner_style->border_left_style = $style->border_left_style;
  149. $inner_style->margin_top = $style->margin_top;
  150. $inner_style->margin_right = $style->margin_right;
  151. $inner_style->margin_bottom = $style->margin_bottom;
  152. $inner_style->margin_left = $style->margin_left;
  153. return array( $width, $width, "min" => $width, "max" => $width);
  154. }
  155. }