gd_adapter.cls.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: gd_adapter.cls.php,v $
  6. * Created on: 2004-06-06
  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. * @package dompdf
  37. */
  38. /* $Id: gd_adapter.cls.php 217 2010-03-11 23:03:57Z ryan.masten $ */
  39. /**
  40. * Image rendering interface
  41. *
  42. * Renders to an image format supported by GD (jpeg, gif, png, xpm).
  43. * Not super-useful day-to-day but handy nonetheless
  44. *
  45. * @package dompdf
  46. */
  47. class GD_Adapter implements Canvas {
  48. /**
  49. * Resoure handle for the image
  50. *
  51. * @var resource
  52. */
  53. private $_img;
  54. /**
  55. * Image width in pixels
  56. *
  57. * @var int
  58. */
  59. private $_width;
  60. /**
  61. * Image height in pixels
  62. *
  63. * @var int
  64. */
  65. private $_height;
  66. /**
  67. * Image antialias factor
  68. *
  69. * @var float
  70. */
  71. private $_aa_factor;
  72. /**
  73. * Allocated colors
  74. *
  75. * @var array
  76. */
  77. private $_colors;
  78. /**
  79. * Background color
  80. *
  81. * @var int
  82. */
  83. private $_bg_color;
  84. /**
  85. * Class constructor
  86. *
  87. * @param mixed $size The size of image to create: array(x1,y1,x2,y2) or "letter", "legal", etc.
  88. * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
  89. * @param float $aa_factor Anti-aliasing factor, 1 for no AA
  90. * @param array $bg_color Image background color: array(r,g,b,a), 0 <= r,g,b,a <= 1
  91. */
  92. function __construct($size, $orientation = "portrait", $aa_factor = 1, $bg_color = array(1,1,1,0) ) {
  93. if ( !is_array($size) ) {
  94. if ( isset(CPDF_Adapter::$PAPER_SIZES[ strtolower($size)]) )
  95. $size = CPDF_Adapter::$PAPER_SIZES[$size];
  96. else
  97. $size = CPDF_Adapter::$PAPER_SIZES["letter"];
  98. }
  99. if ( strtolower($orientation) === "landscape" ) {
  100. list($size[2],$size[3]) = array($size[3],$size[2]);
  101. }
  102. if ( $aa_factor < 1 )
  103. $aa_factor = 1;
  104. $this->_aa_factor = $aa_factor;
  105. $size[2] *= $aa_factor;
  106. $size[3] *= $aa_factor;
  107. $this->_width = $size[2] - $size[0];
  108. $this->_height = $size[3] - $size[1];
  109. $this->_img = imagecreatetruecolor($this->_width, $this->_height);
  110. if ( is_null($bg_color) || !is_array($bg_color) ) {
  111. // Pure white bg
  112. $bg_color = array(1,1,1,0);
  113. }
  114. $this->_bg_color = $this->_allocate_color($bg_color);
  115. imagealphablending($this->_img, false);
  116. imagesavealpha($this->_img, true);
  117. imagefill($this->_img, 0, 0, $this->_bg_color);
  118. }
  119. /**
  120. * Return the GF image resource
  121. *
  122. * @return resource
  123. */
  124. function get_image() { return $this->_img; }
  125. /**
  126. * Return the image's width in pixels
  127. *
  128. * @return float
  129. */
  130. function get_width() { return $this->_width / $this->_aa_factor; }
  131. /**
  132. * Return the image's height in pixels
  133. *
  134. * @return float
  135. */
  136. function get_height() { return $this->_height / $this->_aa_factor; }
  137. /**
  138. * Returns the current page number
  139. *
  140. * @return int
  141. */
  142. function get_page_number() {
  143. // FIXME
  144. }
  145. /**
  146. * Returns the total number of pages
  147. *
  148. * @return int
  149. */
  150. function get_page_count() {
  151. // FIXME
  152. }
  153. /**
  154. * Sets the total number of pages
  155. *
  156. * @param int $count
  157. */
  158. function set_page_count($count) {
  159. // FIXME
  160. }
  161. /**
  162. * Allocate a new color. Allocate with GD as needed and store
  163. * previously allocated colors in $this->_colors.
  164. *
  165. * @param array $color The new current color
  166. * @return int The allocated color
  167. */
  168. private function _allocate_color($color) {
  169. // Full opacity if no alpha set
  170. if ( !isset($color[3]) )
  171. $color[3] = 0;
  172. list($r,$g,$b,$a) = $color;
  173. $r *= 255;
  174. $g *= 255;
  175. $b *= 255;
  176. $a *= 127;
  177. // Clip values
  178. $r = $r > 255 ? 255 : $r;
  179. $g = $g > 255 ? 255 : $g;
  180. $b = $b > 255 ? 255 : $b;
  181. $a = $a > 127 ? 127 : $a;
  182. $r = $r < 0 ? 0 : $r;
  183. $g = $g < 0 ? 0 : $g;
  184. $b = $b < 0 ? 0 : $b;
  185. $a = $a < 0 ? 0 : $a;
  186. $key = sprintf("#%02X%02X%02X%02X", $r, $g, $b, $a);
  187. if ( isset($this->_colors[$key]) )
  188. return $this->_colors[$key];
  189. if ( $a != 0 )
  190. $this->_colors[$key] = imagecolorallocatealpha($this->_img, $r, $g, $b, $a);
  191. else
  192. $this->_colors[$key] = imagecolorallocate($this->_img, $r, $g, $b);
  193. return $this->_colors[$key];
  194. }
  195. /**
  196. * Draws a line from x1,y1 to x2,y2
  197. *
  198. * See {@link Style::munge_color()} for the format of the color array.
  199. * See {@link Cpdf::setLineStyle()} for a description of the format of the
  200. * $style parameter (aka dash).
  201. *
  202. * @param float $x1
  203. * @param float $y1
  204. * @param float $x2
  205. * @param float $y2
  206. * @param array $color
  207. * @param float $width
  208. * @param array $style
  209. */
  210. function line($x1, $y1, $x2, $y2, $color, $width, $style = null) {
  211. // Scale by the AA factor
  212. $x1 *= $this->_aa_factor;
  213. $y1 *= $this->_aa_factor;
  214. $x2 *= $this->_aa_factor;
  215. $y2 *= $this->_aa_factor;
  216. $width *= $this->_aa_factor;
  217. $c = $this->_allocate_color($color);
  218. // Convert the style array if required
  219. if ( !is_null($style) ) {
  220. $gd_style = array();
  221. if ( count($style) == 1 ) {
  222. for ($i = 0; $i < $style[0] * $this->_aa_factor; $i++) {
  223. $gd_style[] = $c;
  224. }
  225. for ($i = 0; $i < $style[0] * $this->_aa_factor; $i++) {
  226. $gd_style[] = $this->_bg_color;
  227. }
  228. } else {
  229. $i = 0;
  230. foreach ($style as $length) {
  231. if ( $i % 2 == 0 ) {
  232. // 'On' pattern
  233. for ($i = 0; $i < $style[0] * $this->_aa_factor; $i++)
  234. $gd_style[] = $c;
  235. } else {
  236. // Off pattern
  237. for ($i = 0; $i < $style[0] * $this->_aa_factor; $i++)
  238. $gd_style[] = $this->_bg_color;
  239. }
  240. $i++;
  241. }
  242. }
  243. imagesetstyle($this->_img, $gd_style);
  244. $c = IMG_COLOR_STYLED;
  245. }
  246. imagesetthickness($this->_img, $width);
  247. imageline($this->_img, $x1, $y1, $x2, $y2, $c);
  248. }
  249. /**
  250. * Draws a rectangle at x1,y1 with width w and height h
  251. *
  252. * See {@link Style::munge_color()} for the format of the color array.
  253. * See {@link Cpdf::setLineStyle()} for a description of the $style
  254. * parameter (aka dash)
  255. *
  256. * @param float $x1
  257. * @param float $y1
  258. * @param float $w
  259. * @param float $h
  260. * @param array $color
  261. * @param float $width
  262. * @param array $style
  263. */
  264. function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) {
  265. // Scale by the AA factor
  266. $x1 *= $this->_aa_factor;
  267. $y1 *= $this->_aa_factor;
  268. $w *= $this->_aa_factor;
  269. $h *= $this->_aa_factor;
  270. $c = $this->_allocate_color($color);
  271. // Convert the style array if required
  272. if ( !is_null($style) ) {
  273. $gd_style = array();
  274. foreach ($style as $length) {
  275. for ($i = 0; $i < $length; $i++) {
  276. $gd_style[] = $c;
  277. }
  278. }
  279. imagesetstyle($this->_img, $gd_style);
  280. $c = IMG_COLOR_STYLED;
  281. }
  282. imagesetthickness($this->_img, $width);
  283. imagerectangle($this->_img, $x1, $y1, $x1 + $w, $y1 + $h, $c);
  284. }
  285. /**
  286. * Draws a filled rectangle at x1,y1 with width w and height h
  287. *
  288. * See {@link Style::munge_color()} for the format of the color array.
  289. *
  290. * @param float $x1
  291. * @param float $y1
  292. * @param float $w
  293. * @param float $h
  294. * @param array $color
  295. */
  296. function filled_rectangle($x1, $y1, $w, $h, $color) {
  297. // Scale by the AA factor
  298. $x1 *= $this->_aa_factor;
  299. $y1 *= $this->_aa_factor;
  300. $w *= $this->_aa_factor;
  301. $h *= $this->_aa_factor;
  302. $c = $this->_allocate_color($color);
  303. imagefilledrectangle($this->_img, $x1, $y1, $x1 + $w, $y1 + $h, $c);
  304. }
  305. /**
  306. * Draws a polygon
  307. *
  308. * The polygon is formed by joining all the points stored in the $points
  309. * array. $points has the following structure:
  310. * <code>
  311. * array(0 => x1,
  312. * 1 => y1,
  313. * 2 => x2,
  314. * 3 => y2,
  315. * ...
  316. * );
  317. * </code>
  318. *
  319. * See {@link Style::munge_color()} for the format of the color array.
  320. * See {@link Cpdf::setLineStyle()} for a description of the $style
  321. * parameter (aka dash)
  322. *
  323. * @param array $points
  324. * @param array $color
  325. * @param float $width
  326. * @param array $style
  327. * @param bool $fill Fills the polygon if true
  328. */
  329. function polygon($points, $color, $width = null, $style = null, $fill = false) {
  330. // Scale each point by the AA factor
  331. foreach (array_keys($points) as $i)
  332. $points[$i] *= $this->_aa_factor;
  333. $c = $this->_allocate_color($color);
  334. // Convert the style array if required
  335. if ( !is_null($style) && !$fill ) {
  336. $gd_style = array();
  337. foreach ($style as $length) {
  338. for ($i = 0; $i < $length; $i++) {
  339. $gd_style[] = $c;
  340. }
  341. }
  342. imagesetstyle($this->_img, $gd_style);
  343. $c = IMG_COLOR_STYLED;
  344. }
  345. imagesetthickness($this->_img, $width);
  346. if ( $fill )
  347. imagefilledpolygon($this->_img, $points, count($points) / 2, $c);
  348. else
  349. imagepolygon($this->_img, $points, count($points) / 2, $c);
  350. }
  351. /**
  352. * Draws a circle at $x,$y with radius $r
  353. *
  354. * See {@link Style::munge_color()} for the format of the color array.
  355. * See {@link Cpdf::setLineStyle()} for a description of the $style
  356. * parameter (aka dash)
  357. *
  358. * @param float $x
  359. * @param float $y
  360. * @param float $r
  361. * @param array $color
  362. * @param float $width
  363. * @param array $style
  364. * @param bool $fill Fills the circle if true
  365. */
  366. function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) {
  367. // Scale by the AA factor
  368. $x *= $this->_aa_factor;
  369. $y *= $this->_aa_factor;
  370. $r *= $this->_aa_factor;
  371. $c = $this->_allocate_color($color);
  372. // Convert the style array if required
  373. if ( !is_null($style) && !$fill ) {
  374. $gd_style = array();
  375. foreach ($style as $length) {
  376. for ($i = 0; $i < $length; $i++) {
  377. $gd_style[] = $c;
  378. }
  379. }
  380. imagesetstyle($this->_img, $gd_style);
  381. $c = IMG_COLOR_STYLED;
  382. }
  383. imagesetthickness($this->_img, $width);
  384. if ( $fill )
  385. imagefilledellipse($this->_img, $x, $y, $r, $r, $c);
  386. else
  387. imageellipse($this->_img, $x, $y, $r, $r, $c);
  388. }
  389. /**
  390. * Add an image to the pdf.
  391. *
  392. * The image is placed at the specified x and y coordinates with the
  393. * given width and height.
  394. *
  395. * @param string $img_url the path to the image
  396. * @param string $img_type the type (e.g. extension) of the image
  397. * @param float $x x position
  398. * @param float $y y position
  399. * @param int $w width (in pixels)
  400. * @param int $h height (in pixels)
  401. */
  402. function image($img_url, $img_type, $x, $y, $w, $h) {
  403. switch ($img_type) {
  404. case "png":
  405. $src = @imagecreatefrompng($img_url);
  406. break;
  407. case "gif":
  408. $src = @imagecreatefromgif($img_url);
  409. break;
  410. case "jpg":
  411. case "jpeg":
  412. $src = @imagecreatefromjpeg($img_url);
  413. break;
  414. default:
  415. break;
  416. }
  417. if ( !$src )
  418. return; // Probably should add to $_dompdf_errors or whatever here
  419. // Scale by the AA factor
  420. $x *= $this->_aa_factor;
  421. $y *= $this->_aa_factor;
  422. $w *= $this->_aa_factor;
  423. $h *= $this->_aa_factor;
  424. $img_w = imagesx($src);
  425. $img_h = imagesy($src);
  426. imagecopyresampled($this->_img, $src, $x, $y, 0, 0, $w, $h, $img_w, $img_h);
  427. }
  428. /**
  429. * Writes text at the specified x and y coordinates
  430. *
  431. * See {@link Style::munge_color()} for the format of the color array.
  432. *
  433. * @param float $x
  434. * @param float $y
  435. * @param string $text the text to write
  436. * @param string $font the font file to use
  437. * @param float $size the font size, in points
  438. * @param array $color
  439. * @param float $adjust word spacing adjustment
  440. * @param float $angle Text angle
  441. */
  442. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0, $angle = 0) {
  443. // Scale by the AA factor
  444. $x *= $this->_aa_factor;
  445. $y *= $this->_aa_factor;
  446. $size *= $this->_aa_factor;
  447. $h = $this->get_font_height($font, $size);
  448. $c = $this->_allocate_color($color);
  449. if ( strpos($font, '.ttf') === false )
  450. $font .= ".ttf";
  451. // FIXME: word spacing
  452. imagettftext($this->_img, $size, $angle, $x, $y + $h, $c, $font, $text);
  453. }
  454. function javascript($code) {
  455. // Not implemented
  456. }
  457. /**
  458. * Add a named destination (similar to <a name="foo">...</a> in html)
  459. *
  460. * @param string $anchorname The name of the named destination
  461. */
  462. function add_named_dest($anchorname) {
  463. // Not implemented
  464. }
  465. /**
  466. * Add a link to the pdf
  467. *
  468. * @param string $url The url to link to
  469. * @param float $x The x position of the link
  470. * @param float $y The y position of the link
  471. * @param float $width The width of the link
  472. * @param float $height The height of the link
  473. */
  474. function add_link($url, $x, $y, $width, $height) {
  475. // Not implemented
  476. }
  477. /**
  478. * Calculates text size, in points
  479. *
  480. * @param string $text the text to be sized
  481. * @param string $font the desired font
  482. * @param float $size the desired font size
  483. * @param float $spacing word spacing, if any
  484. * @return float
  485. */
  486. function get_text_width($text, $font, $size, $spacing = 0) {
  487. if ( strpos($font, '.ttf') === false )
  488. $font .= ".ttf";
  489. // FIXME: word spacing
  490. list($x1,,$x2) = imagettfbbox($size, 0, $font, $text);
  491. return $x2 - $x1;
  492. }
  493. /**
  494. * Calculates font height, in points
  495. *
  496. * @param string $font
  497. * @param float $size
  498. * @return float
  499. */
  500. function get_font_height($font, $size) {
  501. if ( strpos($font, '.ttf') === false )
  502. $font .= ".ttf";
  503. // FIXME: word spacing
  504. list(,$y2,,,,$y1) = imagettfbbox($size, 0, $font, "MXjpqytfhl"); // Test string with ascenders, descenders and caps
  505. return $y2 - $y1;
  506. }
  507. /**
  508. * Starts a new page
  509. *
  510. * Subsequent drawing operations will appear on the new page.
  511. */
  512. function new_page() {
  513. // FIXME
  514. }
  515. /**
  516. * Streams the image directly to the browser
  517. *
  518. * @param string $filename the name of the image file (ignored)
  519. * @param array $options associative array, 'type' => jpeg|jpg|png, 'quality' => 0 - 100 (jpeg only)
  520. */
  521. function stream($filename, $options = null) {
  522. // Perform any antialiasing
  523. if ( $this->_aa_factor != 1 ) {
  524. $dst_w = $this->_width / $this->_aa_factor;
  525. $dst_h = $this->_height / $this->_aa_factor;
  526. $dst = imagecreatetruecolor($dst_w, $dst_h);
  527. imagecopyresampled($dst, $this->_img, 0, 0, 0, 0,
  528. $dst_w, $dst_h,
  529. $this->_width, $this->_height);
  530. } else {
  531. $dst = $this->_img;
  532. }
  533. if ( !isset($options["type"]) )
  534. $options["type"] = "png";
  535. $type = strtolower($options["type"]);
  536. header("Cache-Control: private");
  537. switch ($type) {
  538. case "jpg":
  539. case "jpeg":
  540. if ( !isset($options["quality"]) )
  541. $options["quality"] = 75;
  542. header("Content-type: image/jpeg");
  543. imagejpeg($dst, '', $options["quality"]);
  544. break;
  545. case "png":
  546. default:
  547. header("Content-type: image/png");
  548. imagepng($dst);
  549. break;
  550. }
  551. if ( $this->_aa_factor != 1 )
  552. imagedestroy($dst);
  553. }
  554. /**
  555. * Returns the PNG as a string
  556. *
  557. * @param array $options associative array, 'type' => jpeg|jpg|png, 'quality' => 0 - 100 (jpeg only)
  558. * @return string
  559. */
  560. function output($options = null) {
  561. if ( $this->_aa_factor != 1 ) {
  562. $dst_w = $this->_width / $this->_aa_factor;
  563. $dst_h = $this->_height / $this->_aa_factor;
  564. $dst = imagecreatetruecolor($dst_w, $dst_h);
  565. imagecopyresampled($dst, $this->_img, 0, 0, 0, 0,
  566. $dst_w, $dst_h,
  567. $this->_width, $this->_height);
  568. } else {
  569. $dst = $this->_img;
  570. }
  571. if ( !isset($options["type"]) )
  572. $options["type"] = "png";
  573. $type = $options["type"];
  574. ob_start();
  575. switch ($type) {
  576. case "jpg":
  577. case "jpeg":
  578. if ( !isset($options["quality"]) )
  579. $options["quality"] = 75;
  580. imagejpeg($dst, '', $options["quality"]);
  581. break;
  582. case "png":
  583. default:
  584. imagepng($dst);
  585. break;
  586. }
  587. $image = ob_get_contents();
  588. ob_end_clean();
  589. if ( $this->_aa_factor != 1 )
  590. imagedestroy($dst);
  591. return $image;
  592. }
  593. }