vendor/dompdf/dompdf/src/FrameDecorator/Image.php line 45

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