| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package dompdf |
| 4 |
* @link https://github.com/dompdf/dompdf |
| 5 |
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 6 |
*/ |
| 7 |
namespace Dompdf\FrameDecorator; |
| 8 |
|
| 9 |
use Dompdf\Dompdf; |
| 10 |
use Dompdf\Frame; |
| 11 |
use Dompdf\Helpers; |
| 12 |
use Dompdf\Image\Cache; |
| 13 |
|
| 14 |
/** |
| 15 |
* Decorates frames for image layout and rendering |
| 16 |
* |
| 17 |
* @package dompdf |
| 18 |
*/ |
| 19 |
class Image extends AbstractFrameDecorator |
| 20 |
{ |
| 21 |
|
| 22 |
/** |
| 23 |
* The path to the image file (note that remote images are |
| 24 |
* downloaded locally to Options:tempDir). |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $_image_url; |
| 29 |
|
| 30 |
/** |
| 31 |
* The image's file error message |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $_image_msg; |
| 36 |
|
| 37 |
/** |
| 38 |
* Class constructor |
| 39 |
* |
| 40 |
* @param Frame $frame the frame to decorate |
| 41 |
* @param DOMPDF $dompdf the document's dompdf object (required to resolve relative & remote urls) |
| 42 |
*/ |
| 43 |
function __construct(Frame $frame, Dompdf $dompdf) |
| 44 |
{ |
| 45 |
parent::__construct($frame, $dompdf); |
| 46 |
$url = $frame->get_node()->getAttribute("src"); |
| 47 |
|
| 48 |
$debug_png = $dompdf->getOptions()->getDebugPng(); |
| 49 |
if ($debug_png) { |
| 50 |
print '[__construct ' . $url . ']'; |
| 51 |
} |
| 52 |
|
| 53 |
list($this->_image_url, /*$type*/, $this->_image_msg) = Cache::resolve_url( |
| 54 |
$url, |
| 55 |
$dompdf->getProtocol(), |
| 56 |
$dompdf->getBaseHost(), |
| 57 |
$dompdf->getBasePath(), |
| 58 |
$dompdf->getOptions() |
| 59 |
); |
| 60 |
|
| 61 |
if (Cache::is_broken($this->_image_url) && |
| 62 |
$alt = $frame->get_node()->getAttribute("alt") |
| 63 |
) { |
| 64 |
$fontMetrics = $dompdf->getFontMetrics(); |
| 65 |
$style = $frame->get_style(); |
| 66 |
$font = $style->font_family; |
| 67 |
$size = $style->font_size; |
| 68 |
$word_spacing = $style->word_spacing; |
| 69 |
$letter_spacing = $style->letter_spacing; |
| 70 |
|
| 71 |
$style->width = (4 / 3) * $fontMetrics->getTextWidth($alt, $font, $size, $word_spacing, $letter_spacing); |
| 72 |
$style->height = $fontMetrics->getFontHeight($font, $size); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get the intrinsic pixel dimensions of the image. |
| 78 |
* |
| 79 |
* @return array Width and height as `float|int`. |
| 80 |
*/ |
| 81 |
public function get_intrinsic_dimensions(): array |
| 82 |
{ |
| 83 |
[$width, $height] = Helpers::dompdf_getimagesize($this->_image_url, $this->_dompdf->getHttpContext()); |
| 84 |
|
| 85 |
return [$width, $height]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Resample the given pixel length according to dpi. |
| 90 |
* |
| 91 |
* @param float|int $length |
| 92 |
* @return float |
| 93 |
*/ |
| 94 |
public function resample($length): float |
| 95 |
{ |
| 96 |
$dpi = $this->_dompdf->getOptions()->getDpi(); |
| 97 |
return ($length * 72) / $dpi; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Return the image's url |
| 102 |
* |
| 103 |
* @return string The url of this image |
| 104 |
*/ |
| 105 |
function get_image_url() |
| 106 |
{ |
| 107 |
return $this->_image_url; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Return the image's error message |
| 112 |
* |
| 113 |
* @return string The image's error message |
| 114 |
*/ |
| 115 |
function get_image_msg() |
| 116 |
{ |
| 117 |
return $this->_image_msg; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|