[3019] | 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.digitaljunkies.ca/dompdf |
---|
| 32 | * |
---|
| 33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
| 34 | * @copyright 2004 Benj Carson |
---|
| 35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 36 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 37 | * @package dompdf |
---|
| 38 | * @version 0.5.1 |
---|
| 39 | * |
---|
| 40 | * Changes |
---|
| 41 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 42 | * @version 0.5.1.htischer.20090507 |
---|
| 43 | * - Fix image size as percent of wrapping box |
---|
| 44 | * - Fix arithmetic rounding of image size |
---|
| 45 | * - Time consuming additional image file scan only when really needed |
---|
| 46 | */ |
---|
| 47 | |
---|
| 48 | /* $Id: image_frame_reflower.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Image reflower class |
---|
| 52 | * |
---|
| 53 | * @access private |
---|
| 54 | * @package dompdf |
---|
| 55 | */ |
---|
| 56 | class Image_Frame_Reflower extends Frame_Reflower { |
---|
| 57 | |
---|
| 58 | function __construct(Image_Frame_Decorator $frame) { |
---|
| 59 | parent::__construct($frame); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | function reflow() { |
---|
| 63 | |
---|
| 64 | // Set the frame's width |
---|
| 65 | $this->get_min_max_width(); |
---|
| 66 | |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function get_min_max_width() { |
---|
| 70 | |
---|
| 71 | if (DEBUGPNG) { |
---|
| 72 | // Determine the image's size. Time consuming. Only when really needed? |
---|
| 73 | list($img_width, $img_height) = getimagesize($this->_frame->get_image_url()); |
---|
| 74 | print "get_min_max_width() ". |
---|
| 75 | $this->_frame->get_style()->width.' '. |
---|
| 76 | $this->_frame->get_style()->height.';'. |
---|
| 77 | $this->_frame->get_parent()->get_style()->width." ". |
---|
| 78 | $this->_frame->get_parent()->get_style()->height.";". |
---|
| 79 | $this->_frame->get_parent()->get_parent()->get_style()->width.' '. |
---|
| 80 | $this->_frame->get_parent()->get_parent()->get_style()->height.';'. |
---|
| 81 | $img_width. ' '. |
---|
| 82 | $img_height.'|' ; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // We need to grab our *parent's* style because images are wrapped... |
---|
| 86 | $style = $this->_frame->get_parent()->get_style(); |
---|
| 87 | |
---|
| 88 | //own style auto or invalid value: use natural size in px |
---|
| 89 | //own style value: ignore suffix text including unit, use given number as px |
---|
| 90 | //own style %: walk up parent chain until found available space in pt; fill available space |
---|
| 91 | // |
---|
| 92 | //special ignored unit: e.g. 10ex: e treated as exponent; x ignored; 10e completely invalid ->like auto |
---|
| 93 | |
---|
| 94 | $width = $this->_frame->get_style()->width; |
---|
| 95 | if ( is_percent($width) ) { |
---|
| 96 | $t = 0.0; |
---|
| 97 | for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) { |
---|
| 98 | $t = (float)($f->get_style()->width); //always in pt |
---|
| 99 | if ((float)$t != 0) { |
---|
| 100 | break; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | $width = ((float)rtrim($width,"%") * $t)/100; //maybe 0 |
---|
| 104 | } else { |
---|
| 105 | // Don't set image original size if "%" branch was 0 or size not given. |
---|
| 106 | // Otherwise aspect changed on %/auto combination for width/height |
---|
| 107 | // Resample according to px per inch |
---|
| 108 | // See also List_Bullet_Image_Frame_Decorator::__construct |
---|
| 109 | $width = (float)($width * 72) / DOMPDF_DPI; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | $height = $this->_frame->get_style()->height; |
---|
| 113 | if ( is_percent($height) ) { |
---|
| 114 | $t = 0.0; |
---|
| 115 | for ($f = $this->_frame->get_parent(); $f; $f = $f->get_parent()) { |
---|
| 116 | $t = (float)($f->get_style()->height); //always in pt |
---|
| 117 | if ((float)$t != 0) { |
---|
| 118 | break; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | $height = ((float)rtrim($height,"%") * $t)/100; //maybe 0 |
---|
| 122 | } else { |
---|
| 123 | // Don't set image original size if "%" branch was 0 or size not given. |
---|
| 124 | // Otherwise aspect changed on %/auto combination for width/height |
---|
| 125 | // Resample according to px per inch |
---|
| 126 | // See also List_Bullet_Image_Frame_Decorator::__construct |
---|
| 127 | $height = (float)($height * 72) / DOMPDF_DPI; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | if ($width == 0 && $height == 0) { |
---|
| 131 | // Determine the image's size. Time consuming. Only when really needed! |
---|
| 132 | list($img_width, $img_height) = getimagesize($this->_frame->get_image_url()); |
---|
| 133 | // don't treat 0 as error. Can be downscaled or can be catched elsewhere if image not readable. |
---|
| 134 | // Resample according to px per inch |
---|
| 135 | // See also List_Bullet_Image_Frame_Decorator::__construct |
---|
| 136 | $width = (float)($img_width * 72) / DOMPDF_DPI; |
---|
| 137 | $height = (float)($img_height * 72) / DOMPDF_DPI; |
---|
| 138 | } else if ($height == 0) { |
---|
| 139 | list($img_width, $img_height) = getimagesize($this->_frame->get_image_url()); |
---|
| 140 | //On error don't divide by 0 |
---|
| 141 | if ($img_width == 0) { |
---|
| 142 | throw new DOMPDF_Exception('Image width not detected: '.$this->_frame->get_image_url()); |
---|
| 143 | } |
---|
| 144 | $height = ($width / $img_width) * $img_height; //keep aspect ratio |
---|
| 145 | } else if ($width == 0) { |
---|
| 146 | list($img_width, $img_height) = getimagesize($this->_frame->get_image_url()); |
---|
| 147 | if ($img_height == 0) { |
---|
| 148 | throw new DOMPDF_Exception('Image height not detected: '.$this->_frame->get_image_url()); |
---|
| 149 | } |
---|
| 150 | $width = ($height / $img_height) * $img_width; //keep aspect ratio |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | if (DEBUGPNG) print $width.' '.$height.';'; |
---|
| 154 | |
---|
| 155 | // Synchronize the styles |
---|
| 156 | $inner_style = $this->_frame->get_style(); |
---|
| 157 | $inner_style->width = $style->width = $width . "pt"; |
---|
| 158 | $inner_style->height = $style->height = $height . "pt"; |
---|
| 159 | |
---|
| 160 | $inner_style->padding_top = $style->padding_top; |
---|
| 161 | $inner_style->padding_right = $style->padding_right; |
---|
| 162 | $inner_style->padding_bottom = $style->padding_bottom; |
---|
| 163 | $inner_style->padding_left = $style->padding_left; |
---|
| 164 | |
---|
| 165 | $inner_style->border_top_width = $style->border_top_width; |
---|
| 166 | $inner_style->border_right_width = $style->border_right_width; |
---|
| 167 | $inner_style->border_bottom_width = $style->border_bottom_width; |
---|
| 168 | $inner_style->border_left_width = $style->border_left_width; |
---|
| 169 | |
---|
| 170 | $inner_style->border_top_style = $style->border_top_style; |
---|
| 171 | $inner_style->border_right_style = $style->border_right_style; |
---|
| 172 | $inner_style->border_bottom_style = $style->border_bottom_style; |
---|
| 173 | $inner_style->border_left_style = $style->border_left_style; |
---|
| 174 | |
---|
| 175 | $inner_style->margin_top = $style->margin_top; |
---|
| 176 | $inner_style->margin_right = $style->margin_right; |
---|
| 177 | $inner_style->margin_bottom = $style->margin_bottom; |
---|
| 178 | $inner_style->margin_left = $style->margin_left; |
---|
| 179 | |
---|
| 180 | return array( $width, $width, "min" => $width, "max" => $width); |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | ?> |
---|