PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/lib/core/Zend/Barcode/Renderer/Image.php

https://bitbucket.org/Sinfin/pawtucket
PHP | 470 lines | 292 code | 35 blank | 143 comment | 41 complexity | 2aab53b3994047cbc7ab172e6eced112 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Barcode
  17. * @subpackage Renderer
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Image.php 20367 2010-01-18 04:51:23Z ralph $
  21. */
  22. /** @see Zend_Barcode_Renderer_RendererAbstract*/
  23. require_once 'Zend/Barcode/Renderer/RendererAbstract.php';
  24. /**
  25. * Class for rendering the barcode as image
  26. *
  27. * @category Zend
  28. * @package Zend_Barcode
  29. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Barcode_Renderer_Image extends Zend_Barcode_Renderer_RendererAbstract
  33. {
  34. /**
  35. * List of authorized output format
  36. * @var array
  37. */
  38. protected $_allowedImageType = array(
  39. 'png',
  40. 'jpeg',
  41. 'gif',
  42. );
  43. /**
  44. * Image format
  45. * @var string
  46. */
  47. protected $_imageType = 'png';
  48. /**
  49. * Resource for the image
  50. * @var resource
  51. */
  52. protected $_resource = null;
  53. /**
  54. * Resource for the font and bars color of the image
  55. * @var integer
  56. */
  57. protected $_imageForeColor = null;
  58. /**
  59. * Resource for the background color of the image
  60. * @var integer
  61. */
  62. protected $_imageBackgroundColor = null;
  63. /**
  64. * Height of the rendered image wanted by user
  65. * @var integer
  66. */
  67. protected $_userHeight = 0;
  68. /**
  69. * Width of the rendered image wanted by user
  70. * @var integer
  71. */
  72. protected $_userWidth = 0;
  73. public function __construct($options = null)
  74. {
  75. if (!function_exists('gd_info')) {
  76. require_once 'Zend/Barcode/Renderer/Exception.php';
  77. throw new Zend_Barcode_Renderer_Exception('Zend_Barcode_Renderer_Image requires the GD extension');
  78. }
  79. parent::__construct($options);
  80. }
  81. /**
  82. * Set height of the result image
  83. * @param null|integer $value
  84. * @return Zend_Image_Barcode_Abstract
  85. * @throw Zend_Image_Barcode_Exception
  86. */
  87. public function setHeight($value)
  88. {
  89. if (!is_numeric($value) || intval($value) < 0) {
  90. require_once 'Zend/Barcode/Renderer/Exception.php';
  91. throw new Zend_Barcode_Renderer_Exception(
  92. 'Image height must be greater than or equals 0'
  93. );
  94. }
  95. $this->_userHeight = intval($value);
  96. return $this;
  97. }
  98. /**
  99. * Get barcode height
  100. *
  101. * @return int
  102. */
  103. public function getHeight()
  104. {
  105. return $this->_userHeight;
  106. }
  107. /**
  108. * Set barcode width
  109. *
  110. * @param mixed $value
  111. * @return void
  112. */
  113. public function setWidth($value)
  114. {
  115. if (!is_numeric($value) || intval($value) < 0) {
  116. require_once 'Zend/Barcode/Renderer/Exception.php';
  117. throw new Zend_Barcode_Renderer_Exception(
  118. 'Image width must be greater than or equals 0'
  119. );
  120. }
  121. $this->_userWidth = intval($value);
  122. return $this;
  123. }
  124. /**
  125. * Get barcode width
  126. *
  127. * @return int
  128. */
  129. public function getWidth()
  130. {
  131. return $this->_userWidth;
  132. }
  133. /**
  134. * Set an image resource to draw the barcode inside
  135. *
  136. * @param resource $value
  137. * @return Zend_Barcode_Renderer
  138. * @throw Zend_Barcode_Renderer_Exception
  139. */
  140. public function setResource($image)
  141. {
  142. if (gettype($image) != 'resource' || get_resource_type($image) != 'gd') {
  143. require_once 'Zend/Barcode/Renderer/Exception.php';
  144. throw new Zend_Barcode_Renderer_Exception(
  145. 'Invalid image resource provided to setResource()'
  146. );
  147. }
  148. $this->_resource = $image;
  149. return $this;
  150. }
  151. /**
  152. * Set the image type to produce (png, jpeg, gif)
  153. *
  154. * @param string $value
  155. * @return Zend_Barcode_RendererAbstract
  156. * @throw Zend_Barcode_Renderer_Exception
  157. */
  158. public function setImageType($value)
  159. {
  160. if ($value == 'jpg') {
  161. $value = 'jpeg';
  162. }
  163. if (!in_array($value, $this->_allowedImageType)) {
  164. require_once 'Zend/Barcode/Renderer/Exception.php';
  165. throw new Zend_Barcode_Renderer_Exception(sprintf(
  166. 'Invalid type "%s" provided to setImageType()',
  167. $value
  168. ));
  169. }
  170. $this->_imageType = $value;
  171. return $this;
  172. }
  173. /**
  174. * Retrieve the image type to produce
  175. *
  176. * @return string
  177. */
  178. public function getImageType()
  179. {
  180. return $this->_imageType;
  181. }
  182. /**
  183. * Initialize the image resource
  184. *
  185. * @return void
  186. */
  187. protected function _initRenderer()
  188. {
  189. if (!extension_loaded('gd')) {
  190. require_once 'Zend/Barcode/Exception.php';
  191. $e = new Zend_Barcode_Exception(
  192. 'Gd extension must be loaded to render barcode as image'
  193. );
  194. $e->setIsRenderable(false);
  195. throw $e;
  196. }
  197. $barcodeWidth = $this->_barcode->getWidth(true);
  198. $barcodeHeight = $this->_barcode->getHeight(true);
  199. if ($this->_resource !== null) {
  200. $foreColor = $this->_barcode->getForeColor();
  201. $backgroundColor = $this->_barcode->getBackgroundColor();
  202. $this->_imageBackgroundColor = imagecolorallocate(
  203. $this->_resource,
  204. ($backgroundColor & 0xFF0000) >> 16,
  205. ($backgroundColor & 0x00FF00) >> 8,
  206. $backgroundColor & 0x0000FF
  207. );
  208. $this->_imageForeColor = imagecolorallocate(
  209. $this->_resource,
  210. ($foreColor & 0xFF0000) >> 16,
  211. ($foreColor & 0x00FF00) >> 8,
  212. $foreColor & 0x0000FF
  213. );
  214. } else {
  215. $width = $barcodeWidth;
  216. $height = $barcodeHeight;
  217. if ($this->_userWidth && $this->_barcode->getType() != 'error') {
  218. $width = $this->_userWidth;
  219. }
  220. if ($this->_userHeight && $this->_barcode->getType() != 'error') {
  221. $height = $this->_userHeight;
  222. }
  223. $foreColor = $this->_barcode->getForeColor();
  224. $backgroundColor = $this->_barcode->getBackgroundColor();
  225. $this->_resource = imagecreatetruecolor($width, $height);
  226. $this->_imageBackgroundColor = imagecolorallocate(
  227. $this->_resource,
  228. ($backgroundColor & 0xFF0000) >> 16,
  229. ($backgroundColor & 0x00FF00) >> 8,
  230. $backgroundColor & 0x0000FF
  231. );
  232. $this->_imageForeColor = imagecolorallocate(
  233. $this->_resource,
  234. ($foreColor & 0xFF0000) >> 16,
  235. ($foreColor & 0x00FF00) >> 8,
  236. $foreColor & 0x0000FF
  237. );
  238. $white = imagecolorallocate($this->_resource, 255, 255, 255);
  239. imagefilledrectangle($this->_resource, 0, 0, $width - 1, $height - 1, $white);
  240. }
  241. $this->_adjustPosition(imagesy($this->_resource), imagesx($this->_resource));
  242. imagefilledrectangle(
  243. $this->_resource,
  244. $this->_leftOffset,
  245. $this->_topOffset,
  246. $this->_leftOffset + $barcodeWidth - 1,
  247. $this->_topOffset + $barcodeHeight - 1,
  248. $this->_imageBackgroundColor
  249. );
  250. }
  251. /**
  252. * Check barcode parameters
  253. *
  254. * @return void
  255. */
  256. protected function _checkParams()
  257. {
  258. $this->_checkDimensions();
  259. }
  260. /**
  261. * Check barcode dimensions
  262. *
  263. * @return void
  264. */
  265. protected function _checkDimensions()
  266. {
  267. if ($this->_resource !== null) {
  268. if (imagesy($this->_resource) < $this->_barcode->getHeight(true)) {
  269. require_once 'Zend/Barcode/Renderer/Exception.php';
  270. throw new Zend_Barcode_Renderer_Exception(
  271. 'Barcode is define outside the image (height)'
  272. );
  273. }
  274. } else {
  275. if ($this->_userHeight) {
  276. $height = $this->_barcode->getHeight(true);
  277. if ($this->_userHeight < $height) {
  278. require_once 'Zend/Barcode/Renderer/Exception.php';
  279. throw new Zend_Barcode_Renderer_Exception(sprintf(
  280. "Barcode is define outside the image (calculated: '%d', provided: '%d')",
  281. $height,
  282. $this->_userHeight
  283. ));
  284. }
  285. }
  286. }
  287. if ($this->_resource !== null) {
  288. if (imagesx($this->_resource) < $this->_barcode->getWidth(true)) {
  289. require_once 'Zend/Barcode/Renderer/Exception.php';
  290. throw new Zend_Barcode_Renderer_Exception(
  291. 'Barcode is define outside the image (width)'
  292. );
  293. }
  294. } else {
  295. if ($this->_userWidth) {
  296. $width = $this->_barcode->getWidth(true);
  297. if ($this->_userWidth < $width) {
  298. require_once 'Zend/Barcode/Renderer/Exception.php';
  299. throw new Zend_Barcode_Renderer_Exception(sprintf(
  300. "Barcode is define outside the image (calculated: '%d', provided: '%d')",
  301. $width,
  302. $this->_userWidth
  303. ));
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * Draw and render the barcode with correct headers
  310. *
  311. * @return mixed
  312. */
  313. public function render()
  314. {
  315. $this->draw();
  316. header("Content-Type: image/" . $this->_imageType);
  317. $functionName = 'image' . $this->_imageType;
  318. call_user_func($functionName, $this->_resource);
  319. @imagedestroy($this->_resource);
  320. }
  321. /**
  322. * Draw a polygon in the image resource
  323. *
  324. * @param array $points
  325. * @param integer $color
  326. * @param boolean $filled
  327. */
  328. protected function _drawPolygon($points, $color, $filled = true)
  329. {
  330. $newPoints = array(
  331. $points[0][0] + $this->_leftOffset,
  332. $points[0][1] + $this->_topOffset,
  333. $points[1][0] + $this->_leftOffset,
  334. $points[1][1] + $this->_topOffset,
  335. $points[2][0] + $this->_leftOffset,
  336. $points[2][1] + $this->_topOffset,
  337. $points[3][0] + $this->_leftOffset,
  338. $points[3][1] + $this->_topOffset,
  339. );
  340. $allocatedColor = imagecolorallocate(
  341. $this->_resource,
  342. ($color & 0xFF0000) >> 16,
  343. ($color & 0x00FF00) >> 8,
  344. $color & 0x0000FF
  345. );
  346. if ($filled) {
  347. imagefilledpolygon($this->_resource, $newPoints, 4, $allocatedColor);
  348. } else {
  349. imagepolygon($this->_resource, $newPoints, 4, $allocatedColor);
  350. }
  351. }
  352. /**
  353. * Draw a polygon in the image resource
  354. *
  355. * @param string $text
  356. * @param float $size
  357. * @param array $position
  358. * @param string $font
  359. * @param integer $color
  360. * @param string $alignment
  361. * @param float $orientation
  362. */
  363. protected function _drawText($text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
  364. {
  365. $allocatedColor = imagecolorallocate(
  366. $this->_resource,
  367. ($color & 0xFF0000) >> 16,
  368. ($color & 0x00FF00) >> 8,
  369. $color & 0x0000FF
  370. );
  371. if ($font == null) {
  372. $font = 3;
  373. }
  374. $position[0] += $this->_leftOffset;
  375. $position[1] += $this->_topOffset;
  376. if (is_numeric($font)) {
  377. if ($orientation) {
  378. /**
  379. * imagestring() doesn't allow orientation, if orientation
  380. * needed: a TTF font is required.
  381. * Throwing an exception here, allow to use automaticRenderError
  382. * to informe user of the problem instead of simply not drawing
  383. * the text
  384. */
  385. require_once 'Zend/Barcode/Renderer/Exception.php';
  386. throw new Zend_Barcode_Renderer_Exception(
  387. 'No orientation possible with GD internal font'
  388. );
  389. }
  390. $fontWidth = imagefontwidth($font);
  391. $positionY = $position[1] - imagefontheight($font) + 1;
  392. switch ($alignment) {
  393. case 'left':
  394. $positionX = $position[0];
  395. break;
  396. case 'center':
  397. $positionX = $position[0] - ceil(($fontWidth * strlen($text)) / 2);
  398. break;
  399. case 'right':
  400. $positionX = $position[0] - ($fontWidth * strlen($text));
  401. break;
  402. }
  403. imagestring($this->_resource, $font, $positionX, $positionY, $text, $color);
  404. } else {
  405. if (!function_exists('imagettfbbox')) {
  406. require_once 'Zend/Barcode/Renderer/Exception.php';
  407. throw new Zend_Barcode_Renderer_Exception(
  408. 'A font was provided, but this instance of PHP does not have TTF (FreeType) support'
  409. );
  410. }
  411. $box = imagettfbbox($size, 0, $font, $text);
  412. switch ($alignment) {
  413. case 'left':
  414. $width = 0;
  415. break;
  416. case 'center':
  417. $width = ($box[2] - $box[0]) / 2;
  418. break;
  419. case 'right':
  420. $width = ($box[2] - $box[0]);
  421. break;
  422. }
  423. imagettftext(
  424. $this->_resource,
  425. $size,
  426. $orientation,
  427. $position[0] - ($width * cos(pi() * $orientation / 180)),
  428. $position[1] + ($width * sin(pi() * $orientation / 180)),
  429. $allocatedColor,
  430. $font,
  431. $text
  432. );
  433. }
  434. }
  435. }