人人商城

BCGFontPhp.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Holds font for PHP.
  6. *
  7. *--------------------------------------------------------------------
  8. * Copyright (C) Jean-Sebastien Goupil
  9. * http://www.barcodephp.com
  10. */
  11. include_once('BCGFont.php');
  12. include_once('BCGColor.php');
  13. class BCGFontPhp implements BCGFont {
  14. private $font;
  15. private $text;
  16. private $rotationAngle;
  17. private $backgroundColor;
  18. private $foregroundColor;
  19. /**
  20. * Constructor.
  21. *
  22. * @param int $font
  23. */
  24. public function __construct($font) {
  25. $this->font = max(0, intval($font));
  26. $this->backgroundColor = new BCGColor('white');
  27. $this->foregroundColor = new BCGColor('black');
  28. $this->setRotationAngle(0);
  29. }
  30. /**
  31. * Gets the text associated to the font.
  32. *
  33. * @return string
  34. */
  35. public function getText() {
  36. return $this->text;
  37. }
  38. /**
  39. * Sets the text associated to the font.
  40. *
  41. * @param string text
  42. */
  43. public function setText($text) {
  44. $this->text = $text;
  45. }
  46. /**
  47. * Gets the rotation in degree.
  48. *
  49. * @return int
  50. */
  51. public function getRotationAngle() {
  52. return (360 - $this->rotationAngle) % 360;
  53. }
  54. /**
  55. * Sets the rotation in degree.
  56. *
  57. * @param int
  58. */
  59. public function setRotationAngle($rotationAngle) {
  60. $this->rotationAngle = (int)$rotationAngle;
  61. if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
  62. $this->rotationAngle = 0;
  63. }
  64. $this->rotationAngle = (360 - $this->rotationAngle) % 360;
  65. }
  66. /**
  67. * Gets the background color.
  68. *
  69. * @return BCGColor
  70. */
  71. public function getBackgroundColor() {
  72. return $this->backgroundColor;
  73. }
  74. /**
  75. * Sets the background color.
  76. *
  77. * @param BCGColor $backgroundColor
  78. */
  79. public function setBackgroundColor($backgroundColor) {
  80. $this->backgroundColor = $backgroundColor;
  81. }
  82. /**
  83. * Gets the foreground color.
  84. *
  85. * @return BCGColor
  86. */
  87. public function getForegroundColor() {
  88. return $this->foregroundColor;
  89. }
  90. /**
  91. * Sets the foreground color.
  92. *
  93. * @param BCGColor $foregroundColor
  94. */
  95. public function setForegroundColor($foregroundColor) {
  96. $this->foregroundColor = $foregroundColor;
  97. }
  98. /**
  99. * Returns the width and height that the text takes to be written.
  100. *
  101. * @return int[]
  102. */
  103. public function getDimension() {
  104. $w = imagefontwidth($this->font) * strlen($this->text);
  105. $h = imagefontheight($this->font);
  106. $rotationAngle = $this->getRotationAngle();
  107. if ($rotationAngle === 90 || $rotationAngle === 270) {
  108. return array($h, $w);
  109. } else {
  110. return array($w, $h);
  111. }
  112. }
  113. /**
  114. * Draws the text on the image at a specific position.
  115. * $x and $y represent the left bottom corner.
  116. *
  117. * @param resource $im
  118. * @param int $x
  119. * @param int $y
  120. */
  121. public function draw($im, $x, $y) {
  122. if ($this->getRotationAngle() !== 0) {
  123. if (!function_exists('imagerotate')) {
  124. throw new BCGDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
  125. }
  126. $w = imagefontwidth($this->font) * strlen($this->text);
  127. $h = imagefontheight($this->font);
  128. $gd = imagecreatetruecolor($w, $h);
  129. imagefilledrectangle($gd, 0, 0, $w - 1, $h - 1, $this->backgroundColor->allocate($gd));
  130. imagestring($gd, $this->font, 0, 0, $this->text, $this->foregroundColor->allocate($gd));
  131. $gd = imagerotate($gd, $this->rotationAngle, 0);
  132. imagecopy($im, $gd, $x, $y, 0, 0, imagesx($gd), imagesy($gd));
  133. } else {
  134. imagestring($im, $this->font, $x, $y, $this->text, $this->foregroundColor->allocate($im));
  135. }
  136. }
  137. }
  138. ?>