BCGDrawing.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Holds the drawing $im
  6. * You can use get_im() to add other kind of form not held into these classes.
  7. *
  8. *--------------------------------------------------------------------
  9. * Copyright (C) Jean-Sebastien Goupil
  10. * http://www.barcodephp.com
  11. */
  12. include_once('BCGBarcode.php');
  13. include_once('BCGColor.php');
  14. include_once('BCGDrawException.php');
  15. include_once('drawer/BCGDrawJPG.php');
  16. include_once('drawer/BCGDrawPNG.php');
  17. class BCGDrawing {
  18. const IMG_FORMAT_PNG = 1;
  19. const IMG_FORMAT_JPEG = 2;
  20. const IMG_FORMAT_GIF = 3;
  21. const IMG_FORMAT_WBMP = 4;
  22. private $w, $h; // int
  23. private $color; // BCGColor
  24. private $filename; // char *
  25. private $im; // {object}
  26. private $barcode; // BCGBarcode
  27. private $dpi; // float
  28. private $rotateDegree; // float
  29. /**
  30. * Constructor.
  31. *
  32. * @param int $w
  33. * @param int $h
  34. * @param string filename
  35. * @param BCGColor $color
  36. */
  37. public function __construct($filename = null, BCGColor $color) {
  38. $this->im = null;
  39. $this->setFilename($filename);
  40. $this->color = $color;
  41. $this->dpi = null;
  42. $this->rotateDegree = 0.0;
  43. }
  44. /**
  45. * Destructor.
  46. */
  47. public function __destruct() {
  48. $this->destroy();
  49. }
  50. /**
  51. * Gets the filename.
  52. *
  53. * @return string
  54. */
  55. public function getFilename() {
  56. return $this->filename;
  57. }
  58. /**
  59. * Sets the filename.
  60. *
  61. * @param string $filaneme
  62. */
  63. public function setFilename($filename) {
  64. $this->filename = $filename;
  65. }
  66. /**
  67. * @return resource.
  68. */
  69. public function get_im() {
  70. return $this->im;
  71. }
  72. /**
  73. * Sets the image.
  74. *
  75. * @param resource $im
  76. */
  77. public function set_im($im) {
  78. $this->im = $im;
  79. }
  80. /**
  81. * Gets barcode for drawing.
  82. *
  83. * @return BCGBarcode
  84. */
  85. public function getBarcode() {
  86. return $this->barcode;
  87. }
  88. /**
  89. * Sets barcode for drawing.
  90. *
  91. * @param BCGBarcode $barcode
  92. */
  93. public function setBarcode(BCGBarcode $barcode) {
  94. $this->barcode = $barcode;
  95. }
  96. /**
  97. * Gets the DPI for supported filetype.
  98. *
  99. * @return float
  100. */
  101. public function getDPI() {
  102. return $this->dpi;
  103. }
  104. /**
  105. * Sets the DPI for supported filetype.
  106. *
  107. * @param float $dpi
  108. */
  109. public function setDPI($dpi) {
  110. $this->dpi = $dpi;
  111. }
  112. /**
  113. * Gets the rotation angle in degree clockwise.
  114. *
  115. * @return float
  116. */
  117. public function getRotationAngle() {
  118. return $this->rotateDegree;
  119. }
  120. /**
  121. * Sets the rotation angle in degree clockwise.
  122. *
  123. * @param float $degree
  124. */
  125. public function setRotationAngle($degree) {
  126. $this->rotateDegree = (float)$degree;
  127. }
  128. /**
  129. * Draws the barcode on the image $im.
  130. */
  131. public function draw() {
  132. $size = $this->barcode->getDimension(0, 0);
  133. $this->w = max(1, $size[0]);
  134. $this->h = max(1, $size[1]);
  135. $this->init();
  136. $this->barcode->draw($this->im);
  137. }
  138. /**
  139. * Saves $im into the file (many format available).
  140. *
  141. * @param int $image_style
  142. * @param int $quality
  143. */
  144. public function finish($image_style = self::IMG_FORMAT_PNG, $quality = 100) {
  145. $drawer = null;
  146. $im = $this->im;
  147. if ($this->rotateDegree > 0.0) {
  148. if (function_exists('imagerotate')) {
  149. $im = imagerotate($this->im, 360 - $this->rotateDegree, $this->color->allocate($this->im));
  150. } else {
  151. throw new BCGDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
  152. }
  153. }
  154. if ($image_style === self::IMG_FORMAT_PNG) {
  155. $drawer = new BCGDrawPNG($im);
  156. $drawer->setFilename($this->filename);
  157. $drawer->setDPI($this->dpi);
  158. } elseif ($image_style === self::IMG_FORMAT_JPEG) {
  159. $drawer = new BCGDrawJPG($im);
  160. $drawer->setFilename($this->filename);
  161. $drawer->setDPI($this->dpi);
  162. $drawer->setQuality($quality);
  163. } elseif ($image_style === self::IMG_FORMAT_GIF) {
  164. // Some PHP versions have a bug if passing 2nd argument as null.
  165. if ($this->filename === null || $this->filename === '') {
  166. imagegif($im);
  167. } else {
  168. imagegif($im, $this->filename);
  169. }
  170. } elseif ($image_style === self::IMG_FORMAT_WBMP) {
  171. imagewbmp($im, $this->filename);
  172. }
  173. if ($drawer !== null) {
  174. $drawer->draw();
  175. }
  176. }
  177. /**
  178. * Writes the Error on the picture.
  179. *
  180. * @param Exception $exception
  181. */
  182. public function drawException($exception) {
  183. $this->w = 1;
  184. $this->h = 1;
  185. $this->init();
  186. // Is the image big enough?
  187. $w = imagesx($this->im);
  188. $h = imagesy($this->im);
  189. $text = 'Error: ' . $exception->getMessage();
  190. $width = imagefontwidth(2) * strlen($text);
  191. $height = imagefontheight(2);
  192. if ($width > $w || $height > $h) {
  193. $width = max($w, $width);
  194. $height = max($h, $height);
  195. // We change the size of the image
  196. $newimg = imagecreatetruecolor($width, $height);
  197. imagefill($newimg, 0, 0, imagecolorat($this->im, 0, 0));
  198. imagecopy($newimg, $this->im, 0, 0, 0, 0, $w, $h);
  199. $this->im = $newimg;
  200. }
  201. $black = new BCGColor('black');
  202. imagestring($this->im, 2, 0, 0, $text, $black->allocate($this->im));
  203. }
  204. /**
  205. * Free the memory of PHP (called also by destructor).
  206. */
  207. public function destroy() {
  208. @imagedestroy($this->im);
  209. }
  210. /**
  211. * Init Image and color background.
  212. */
  213. private function init() {
  214. if ($this->im === null) {
  215. $this->im = imagecreatetruecolor($this->w, $this->h)
  216. or die('Can\'t Initialize the GD Libraty');
  217. imagefilledrectangle($this->im, 0, 0, $this->w - 1, $this->h - 1, $this->color->allocate($this->im));
  218. }
  219. }
  220. }
  221. ?>