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
9 namespace Dompdf\Adapter
;
13 use Dompdf\Image\Cache
;
17 * Image rendering interface
19 * Renders to an image format supported by GD (jpeg, gif, png, xpm).
20 * Not super-useful day-to-day but handy nonetheless
24 class GD
implements Canvas
32 * Resource handle for the image
39 * Resource handle for the image
46 * Apparent canvas width in pixels
53 * Apparent canvas height in pixels
60 * Actual image width in pixels
64 private $_actual_width;
67 * Actual image height in pixels
71 private $_actual_height;
78 private $_page_number;
81 * Total number of pages
88 * Image antialias factor
109 * Background color array
113 private $_bg_color_array;
123 * Amount to scale font sizes
125 * Font sizes are 72 DPI, GD internally uses 96. Scale them proportionally.
130 const FONT_SCALE
= 0.75;
135 * @param mixed $size The size of image to create: array(x1,y1,x2,y2) or "letter", "legal", etc.
136 * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
137 * @param Dompdf $dompdf
138 * @param float $aa_factor Anti-aliasing factor, 1 for no AA
139 * @param array $bg_color Image background color: array(r,g,b,a), 0 <= r,g,b,a <= 1
141 function __construct($size = 'letter', $orientation = "portrait", Dompdf
$dompdf, $aa_factor = 1.0, $bg_color = array(1, 1, 1, 0))
144 if (!is_array($size)) {
145 $size = strtolower($size);
147 if (isset(CPDF
::$PAPER_SIZES[$size])) {
148 $size = CPDF
::$PAPER_SIZES[$size];
150 $size = CPDF
::$PAPER_SIZES["letter"];
154 if (strtolower($orientation) === "landscape") {
155 list($size[2], $size[3]) = array($size[3], $size[2]);
158 $this->_dompdf
= $dompdf;
160 $this->dpi
= $this->get_dompdf()->get_option('dpi');
162 if ($aa_factor < 1) {
166 $this->_aa_factor
= $aa_factor;
168 $size[2] *= $aa_factor;
169 $size[3] *= $aa_factor;
171 $this->_width
= $size[2] - $size[0];
172 $this->_height
= $size[3] - $size[1];
174 $this->_actual_width
= $this->_upscale($this->_width
);
175 $this->_actual_height
= $this->_upscale($this->_height
);
177 if (is_null($bg_color) ||
!is_array($bg_color)) {
179 $bg_color = array(1, 1, 1, 0);
182 $this->_bg_color_array
= $bg_color;
187 function get_dompdf()
189 return $this->_dompdf
;
193 * Return the GF image resource
203 * Return the image's width in pixels
209 return $this->_width
/ $this->_aa_factor
;
213 * Return the image's height in pixels
217 function get_height()
219 return $this->_height
/ $this->_aa_factor
;
223 * Returns the current page number
226 function get_page_number()
228 return $this->_page_number
;
232 * Returns the total number of pages in the document
235 function get_page_count()
237 return $this->_page_count
;
241 * Sets the current page number
245 function set_page_number($num)
247 $this->_page_number
= $num;
251 * Sets the page count
255 function set_page_count($count)
257 $this->_page_count
= $count;
266 function set_opacity($opacity, $mode = "Normal")
272 * Allocate a new color. Allocate with GD as needed and store
273 * previously allocated colors in $this->_colors.
275 * @param array $color The new current color
276 * @return int The allocated color
278 private function _allocate_color($color)
281 if (isset($color["c"])) {
282 $color = Helpers
::cmyk_to_rgb($color);
285 // Full opacity if no alpha set
286 if (!isset($color[3]))
289 list($r, $g, $b, $a) = $color;
297 $r = $r > 255 ?
255 : $r;
298 $g = $g > 255 ?
255 : $g;
299 $b = $b > 255 ?
255 : $b;
300 $a = $a > 127 ?
127 : $a;
302 $r = $r < 0 ?
0 : $r;
303 $g = $g < 0 ?
0 : $g;
304 $b = $b < 0 ?
0 : $b;
305 $a = $a < 0 ?
0 : $a;
307 $key = sprintf("#%02X%02X%02X%02X", $r, $g, $b, $a);
309 if (isset($this->_colors
[$key]))
310 return $this->_colors
[$key];
313 $this->_colors
[$key] = imagecolorallocatealpha($this->get_image(), $r, $g, $b, $a);
315 $this->_colors
[$key] = imagecolorallocate($this->get_image(), $r, $g, $b);
317 return $this->_colors
[$key];
322 * Scales value up to the current canvas DPI from 72 DPI
324 * @param float $length
327 private function _upscale($length)
329 return ($length * $this->dpi
) / 72 * $this->_aa_factor
;
333 * Scales value down from the current canvas DPI to 72 DPI
335 * @param float $length
338 private function _downscale($length)
340 return ($length / $this->dpi
* 72) / $this->_aa_factor
;
344 * Draws a line from x1,y1 to x2,y2
346 * See {@link Style::munge_color()} for the format of the color array.
347 * See {@link Cpdf::setLineStyle()} for a description of the format of the
348 * $style parameter (aka dash).
354 * @param array $color
355 * @param float $width
356 * @param array $style
358 function line($x1, $y1, $x2, $y2, $color, $width, $style = null)
361 // Scale by the AA factor and DPI
362 $x1 = $this->_upscale($x1);
363 $y1 = $this->_upscale($y1);
364 $x2 = $this->_upscale($x2);
365 $y2 = $this->_upscale($y2);
366 $width = $this->_upscale($width);
368 $c = $this->_allocate_color($color);
370 // Convert the style array if required
371 if (is_array($style) && count($style) > 0) {
374 if (count($style) == 1) {
375 for ($i = 0; $i < $style[0] * $this->_aa_factor
; $i++
) {
379 for ($i = 0; $i < $style[0] * $this->_aa_factor
; $i++
) {
380 $gd_style[] = $this->_bg_color
;
386 foreach ($style as $length) {
390 for ($i = 0; $i < $style[0] * $this->_aa_factor
; $i++
)
395 for ($i = 0; $i < $style[0] * $this->_aa_factor
; $i++
)
396 $gd_style[] = $this->_bg_color
;
403 if(!empty($gd_style)) {
404 imagesetstyle($this->get_image(), $gd_style);
405 $c = IMG_COLOR_STYLED
;
409 imagesetthickness($this->get_image(), $width);
411 imageline($this->get_image(), $x1, $y1, $x2, $y2, $c);
415 function arc($x1, $y1, $r1, $r2, $astart, $aend, $color, $width, $style = array())
421 * Draws a rectangle at x1,y1 with width w and height h
423 * See {@link Style::munge_color()} for the format of the color array.
424 * See {@link Cpdf::setLineStyle()} for a description of the $style
425 * parameter (aka dash)
431 * @param array $color
432 * @param float $width
433 * @param array $style
435 function rectangle($x1, $y1, $w, $h, $color, $width, $style = null)
438 // Scale by the AA factor and DPI
439 $x1 = $this->_upscale($x1);
440 $y1 = $this->_upscale($y1);
441 $w = $this->_upscale($w);
442 $h = $this->_upscale($h);
443 $width = $this->_upscale($width);
445 $c = $this->_allocate_color($color);
447 // Convert the style array if required
448 if (is_array($style) && count($style) > 0) {
451 foreach ($style as $length) {
452 for ($i = 0; $i < $length; $i++
) {
457 if(!empty($gd_style)) {
458 imagesetstyle($this->get_image(), $gd_style);
459 $c = IMG_COLOR_STYLED
;
463 imagesetthickness($this->get_image(), $width);
465 imagerectangle($this->get_image(), $x1, $y1, $x1 +
$w, $y1 +
$h, $c);
470 * Draws a filled rectangle at x1,y1 with width w and height h
472 * See {@link Style::munge_color()} for the format of the color array.
478 * @param array $color
480 function filled_rectangle($x1, $y1, $w, $h, $color)
483 // Scale by the AA factor and DPI
484 $x1 = $this->_upscale($x1);
485 $y1 = $this->_upscale($y1);
486 $w = $this->_upscale($w);
487 $h = $this->_upscale($h);
489 $c = $this->_allocate_color($color);
491 imagefilledrectangle($this->get_image(), $x1, $y1, $x1 +
$w, $y1 +
$h, $c);
496 * Starts a clipping rectangle at x1,y1 with width w and height h
503 function clipping_rectangle($x1, $y1, $w, $h)
508 function clipping_roundrectangle($x1, $y1, $w, $h, $rTL, $rTR, $rBR, $rBL)
514 * Ends the last clipping shape
516 function clipping_end()
523 $this->get_dompdf()->set_option('dpi', 72);
528 $this->get_dompdf()->set_option('dpi', $this->dpi
);
531 function rotate($angle, $x, $y)
536 function skew($angle_x, $angle_y, $x, $y)
541 function scale($s_x, $s_y, $x, $y)
546 function translate($t_x, $t_y)
551 function transform($a, $b, $c, $d, $e, $f)
559 * The polygon is formed by joining all the points stored in the $points
560 * array. $points has the following structure:
570 * See {@link Style::munge_color()} for the format of the color array.
571 * See {@link Cpdf::setLineStyle()} for a description of the $style
572 * parameter (aka dash)
574 * @param array $points
575 * @param array $color
576 * @param float $width
577 * @param array $style
578 * @param bool $fill Fills the polygon if true
580 function polygon($points, $color, $width = null, $style = null, $fill = false)
583 // Scale each point by the AA factor and DPI
584 foreach (array_keys($points) as $i)
585 $points[$i] = $this->_upscale($points[$i]);
587 $c = $this->_allocate_color($color);
589 // Convert the style array if required
590 if (is_array($style) && count($style) > 0 && !$fill) {
593 foreach ($style as $length) {
594 for ($i = 0; $i < $length; $i++
) {
599 if(!empty($gd_style)) {
600 imagesetstyle($this->get_image(), $gd_style);
601 $c = IMG_COLOR_STYLED
;
605 imagesetthickness($this->get_image(), $width);
608 imagefilledpolygon($this->get_image(), $points, count($points) / 2, $c);
610 imagepolygon($this->get_image(), $points, count($points) / 2, $c);
615 * Draws a circle at $x,$y with radius $r
617 * See {@link Style::munge_color()} for the format of the color array.
618 * See {@link Cpdf::setLineStyle()} for a description of the $style
619 * parameter (aka dash)
624 * @param array $color
625 * @param float $width
626 * @param array $style
627 * @param bool $fill Fills the circle if true
629 function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false)
632 // Scale by the AA factor and DPI
633 $x = $this->_upscale($x);
634 $y = $this->_upscale($y);
635 $r = $this->_upscale($r);
637 $c = $this->_allocate_color($color);
639 // Convert the style array if required
640 if (is_array($style) && count($style) > 0 && !$fill) {
643 foreach ($style as $length) {
644 for ($i = 0; $i < $length; $i++
) {
649 if(!empty($gd_style)) {
650 imagesetstyle($this->get_image(), $gd_style);
651 $c = IMG_COLOR_STYLED
;
655 imagesetthickness($this->get_image(), $width);
658 imagefilledellipse($this->get_image(), $x, $y, $r, $r, $c);
660 imageellipse($this->get_image(), $x, $y, $r, $r, $c);
665 * Add an image to the pdf.
666 * The image is placed at the specified x and y coordinates with the
667 * given width and height.
669 * @param string $img_url the path to the image
670 * @param float $x x position
671 * @param float $y y position
672 * @param int $w width (in pixels)
673 * @param int $h height (in pixels)
674 * @param string $resolution
677 * @internal param string $img_type the type (e.g. extension) of the image
679 function image($img_url, $x, $y, $w, $h, $resolution = "normal")
681 $img_type = Cache
::detect_type($img_url, $this->get_dompdf()->getHttpContext());
687 $func = "imagecreatefrom$img_type";
688 if (!function_exists($func_name)) {
689 if (!method_exists("Dompdf\Helpers", $func_name)) {
690 throw new Exception("Function $func_name() not found. Cannot convert $type image: $image_url. Please install the image PHP extension.");
692 $func_name = "\\Dompdf\\Helpers::" . $func_name;
694 $src = @call_user_func
($func_name, $image_url);
697 return; // Probably should add to $_dompdf_errors or whatever here
700 // Scale by the AA factor and DPI
701 $x = $this->_upscale($x);
702 $y = $this->_upscale($y);
704 $w = $this->_upscale($w);
705 $h = $this->_upscale($h);
707 $img_w = imagesx($src);
708 $img_h = imagesy($src);
710 imagecopyresampled($this->get_image(), $src, $x, $y, 0, 0, $w, $h, $img_w, $img_h);
715 * Writes text at the specified x and y coordinates
716 * See {@link Style::munge_color()} for the format of the color array.
720 * @param string $text the text to write
721 * @param string $font the font file to use
722 * @param float $size the font size, in points
723 * @param array $color
724 * @param float $word_spacing word spacing adjustment
725 * @param float $char_spacing
726 * @param float $angle Text angle
730 function text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_spacing = 0.0, $char_spacing = 0.0, $angle = 0.0)
733 // Scale by the AA factor and DPI
734 $x = $this->_upscale($x);
735 $y = $this->_upscale($y);
736 $size = $this->_upscale($size) * self
::FONT_SCALE
;
738 $h = $this->get_font_height_actual($font, $size);
739 $c = $this->_allocate_color($color);
741 // imagettftext() converts numeric entities to their respective
742 // character. Preserve any originally double encoded entities to be
743 // represented as is.
744 // eg: &#160; will render   rather than its character.
745 $text = preg_replace('/&(#(?:x[a-fA-F0-9]+|[0-9]+);)/', '&\1', $text);
747 $text = mb_encode_numericentity($text, array(0x0080, 0xff, 0, 0xff), 'UTF-8');
749 $font = $this->get_ttf_file($font);
751 // FIXME: word spacing
752 imagettftext($this->get_image(), $size, $angle, $x, $y +
$h, $c, $font, $text);
756 function javascript($code)
762 * Add a named destination (similar to <a name="foo">...</a> in html)
764 * @param string $anchorname The name of the named destination
766 function add_named_dest($anchorname)
772 * Add a link to the pdf
774 * @param string $url The url to link to
775 * @param float $x The x position of the link
776 * @param float $y The y position of the link
777 * @param float $width The width of the link
778 * @param float $height The height of the link
780 function add_link($url, $x, $y, $width, $height)
786 * Add meta information to the PDF
788 * @param string $label label of the value (Creator, Producer, etc.)
789 * @param string $value the text to set
791 function add_info($label, $value)
796 function set_default_view($view, $options = array())
802 * Calculates text size, in points
804 * @param string $text the text to be sized
805 * @param string $font the desired font
806 * @param float $size the desired font size
807 * @param float $word_spacing word spacing, if any
808 * @param float $char_spacing char spacing, if any
812 function get_text_width($text, $font, $size, $word_spacing = 0.0, $char_spacing = 0.0)
814 $font = $this->get_ttf_file($font);
815 $size = $this->_upscale($size) * self
::FONT_SCALE
;
817 // imagettfbbox() converts numeric entities to their respective
818 // character. Preserve any originally double encoded entities to be
819 // represented as is.
820 // eg: &#160; will render   rather than its character.
821 $text = preg_replace('/&(#(?:x[a-fA-F0-9]+|[0-9]+);)/', '&\1', $text);
823 $text = mb_encode_numericentity($text, array(0x0080, 0xffff, 0, 0xffff), 'UTF-8');
825 // FIXME: word spacing
826 list($x1, , $x2) = imagettfbbox($size, 0, $font, $text);
828 // Add additional 1pt to prevent text overflow issues
829 return $this->_downscale($x2 - $x1) +
1;
832 function get_ttf_file($font)
834 if (strpos($font, '.ttf') === false)
837 /*$filename = substr(strtolower(basename($font)), 0, -4);
839 if ( in_array($filename, Dompdf::$native_fonts) ) {
847 * Calculates font height, in points
849 * @param string $font
853 function get_font_height($font, $size)
855 $size = $this->_upscale($size) * self
::FONT_SCALE
;
857 $height = $this->get_font_height_actual($font, $size);
859 return $this->_downscale($height);
862 private function get_font_height_actual($font, $size)
864 $font = $this->get_ttf_file($font);
865 $ratio = $this->_dompdf
->get_option("font_height_ratio");
867 // FIXME: word spacing
868 list(, $y2, , , , $y1) = imagettfbbox($size, 0, $font, "MXjpqytfhl"); // Test string with ascenders, descenders and caps
869 return ($y2 - $y1) * $ratio;
872 function get_font_baseline($font, $size)
874 $ratio = $this->_dompdf
->get_option("font_height_ratio");
875 return $this->get_font_height($font, $size) / $ratio;
881 * Subsequent drawing operations will appear on the new page.
885 $this->_page_number++
;
886 $this->_page_count++
;
888 $this->_img
= imagecreatetruecolor($this->_actual_width
, $this->_actual_height
);
890 $this->_bg_color
= $this->_allocate_color($this->_bg_color_array
);
891 imagealphablending($this->_img
, true);
892 imagesavealpha($this->_img
, true);
893 imagefill($this->_img
, 0, 0, $this->_bg_color
);
895 $this->_imgs
[] = $this->_img
;
898 function open_object()
903 function close_object()
908 function add_object()
919 * Streams the image directly to the browser
921 * @param string $filename the name of the image file (ignored)
922 * @param array $options associative array, 'type' => jpeg|jpg|png, 'quality' => 0 - 100 (jpeg only)
924 function stream($filename, $options = null)
927 $img = $this->_imgs
[0];
929 if (isset($options['page']) && isset($this->_imgs
[$options['page'] - 1])) {
930 $img = $this->_imgs
[$options['page'] - 1];
933 // Perform any antialiasing
934 if ($this->_aa_factor
!= 1) {
935 $dst_w = $this->_actual_width
/ $this->_aa_factor
;
936 $dst_h = $this->_actual_height
/ $this->_aa_factor
;
937 $dst = imagecreatetruecolor($dst_w, $dst_h);
938 imagecopyresampled($dst, $img, 0, 0, 0, 0,
940 $this->_actual_width
, $this->_actual_height
);
945 if (!isset($options["type"]))
946 $options["type"] = "png";
948 $type = strtolower($options["type"]);
950 header("Cache-Control: private");
952 $filename = str_replace(array("\n", "'"), "", basename($filename));
965 $attach = (isset($options["Attachment"]) && $options["Attachment"]) ?
"attachment" : "inline";
967 // detect the character encoding of the incoming file
968 $encoding = mb_detect_encoding($filename);
969 $fallbackfilename = mb_convert_encoding($filename, "ISO-8859-1", $encoding);
970 $encodedfallbackfilename = rawurlencode($fallbackfilename);
971 $encodedfilename = rawurlencode($filename);
973 header("Content-Disposition: $attach; filename=". $encodedfallbackfilename ."; filename*=UTF-8''$encodedfilename");
979 if (!isset($options["quality"]))
980 $options["quality"] = 75;
982 header("Content-type: image/jpeg");
983 imagejpeg($dst, '', $options["quality"]);
988 header("Content-type: image/png");
993 if ($this->_aa_factor
!= 1)
998 * Returns the PNG as a string
1000 * @param array $options associative array, 'type' => jpeg|jpg|png, 'quality' => 0 - 100 (jpeg only)
1003 function output($options = null)
1006 $img = $this->_imgs
[0];
1008 if (isset($options['page']) && isset($this->_imgs
[$options['page'] - 1])) {
1009 $img = $this->_imgs
[$options['page'] - 1];
1012 if ($this->_aa_factor
!= 1) {
1013 $dst_w = $this->_actual_width
/ $this->_aa_factor
;
1014 $dst_h = $this->_actual_height
/ $this->_aa_factor
;
1015 $dst = imagecreatetruecolor($dst_w, $dst_h);
1016 imagecopyresampled($dst, $img, 0, 0, 0, 0,
1018 $this->_actual_width
, $this->_actual_height
);
1023 if (!isset($options["type"]))
1024 $options["type"] = "png";
1026 $type = $options["type"];
1034 if (!isset($options["quality"]))
1035 $options["quality"] = 75;
1037 imagejpeg($dst, '', $options["quality"]);
1046 $image = ob_get_clean();
1048 if ($this->_aa_factor
!= 1)