Image.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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$
  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. /**
  74. * Set height of the result image
  75. * @param null|integer $value
  76. * @return Zend_Image_Barcode_Abstract
  77. * @throw Zend_Image_Barcode_Exception
  78. */
  79. public function setHeight($value)
  80. {
  81. if (!is_numeric($value) || intval($value) < 0) {
  82. require_once 'Zend/Barcode/Renderer/Exception.php';
  83. throw new Zend_Barcode_Renderer_Exception(
  84. 'Image height must be greater than or equals 0'
  85. );
  86. }
  87. $this->_userHeight = intval($value);
  88. return $this;
  89. }
  90. /**
  91. * Get barcode height
  92. *
  93. * @return int
  94. */
  95. public function getHeight()
  96. {
  97. return $this->_userHeight;
  98. }
  99. /**
  100. * Set barcode width
  101. *
  102. * @param mixed $value
  103. * @return void
  104. */
  105. public function setWidth($value)
  106. {
  107. if (!is_numeric($value) || intval($value) < 0) {
  108. require_once 'Zend/Barcode/Renderer/Exception.php';
  109. throw new Zend_Barcode_Renderer_Exception(
  110. 'Image width must be greater than or equals 0'
  111. );
  112. }
  113. $this->_userWidth = intval($value);
  114. return $this;
  115. }
  116. /**
  117. * Get barcode width
  118. *
  119. * @return int
  120. */
  121. public function getWidth()
  122. {
  123. return $this->_userWidth;
  124. }
  125. /**
  126. * Set an image resource to draw the barcode inside
  127. *
  128. * @param resource $value
  129. * @return Zend_Barcode_Renderer
  130. * @throw Zend_Barcode_Renderer_Exception
  131. */
  132. public function setResource($image)
  133. {
  134. if (gettype($image) != 'resource' || get_resource_type($image) != 'gd') {
  135. require_once 'Zend/Barcode/Renderer/Exception.php';
  136. throw new Zend_Barcode_Renderer_Exception(
  137. 'Invalid image resource provided to setResource()'
  138. );
  139. }
  140. $this->_resource = $image;
  141. return $this;
  142. }
  143. /**
  144. * Set the image type to produce (png, jpeg, gif)
  145. *
  146. * @param string $value
  147. * @return Zend_Barcode_RendererAbstract
  148. * @throw Zend_Barcode_Renderer_Exception
  149. */
  150. public function setImageType($value)
  151. {
  152. if ($value == 'jpg') {
  153. $value = 'jpeg';
  154. }
  155. if (!in_array($value, $this->_allowedImageType)) {
  156. require_once 'Zend/Barcode/Renderer/Exception.php';
  157. throw new Zend_Barcode_Renderer_Exception(sprintf(
  158. 'Invalid type "%s" provided to setImageType()',
  159. $value
  160. ));
  161. }
  162. $this->_imageType = $value;
  163. return $this;
  164. }
  165. /**
  166. * Retrieve the image type to produce
  167. *
  168. * @return string
  169. */
  170. public function getImageType()
  171. {
  172. return $this->_imageType;
  173. }
  174. /**
  175. * Initialize the image resource
  176. *
  177. * @return void
  178. */
  179. protected function _initRenderer()
  180. {
  181. if (!extension_loaded('gd')) {
  182. require_once 'Zend/Barcode/Exception.php';
  183. $e = new Zend_Barcode_Exception(
  184. 'Gd extension must be loaded to render barcode as image'
  185. );
  186. $e->setIsRenderable(false);
  187. throw $e;
  188. }
  189. $barcodeWidth = $this->_barcode->getWidth(true);
  190. $barcodeHeight = $this->_barcode->getHeight(true);
  191. if ($this->_resource !== null) {
  192. $foreColor = $this->_barcode->getForeColor();
  193. $backgroundColor = $this->_barcode->getBackgroundColor();
  194. $this->_imageBackgroundColor = imagecolorallocate(
  195. $this->_resource,
  196. ($backgroundColor & 0xFF0000) >> 16,
  197. ($backgroundColor & 0x00FF00) >> 8,
  198. $backgroundColor & 0x0000FF
  199. );
  200. $this->_imageForeColor = imagecolorallocate(
  201. $this->_resource,
  202. ($foreColor & 0xFF0000) >> 16,
  203. ($foreColor & 0x00FF00) >> 8,
  204. $foreColor & 0x0000FF
  205. );
  206. } else {
  207. $width = $barcodeWidth;
  208. $height = $barcodeHeight;
  209. if ($this->_userWidth && $this->_barcode->getType() != 'error') {
  210. $width = $this->_userWidth;
  211. }
  212. if ($this->_userHeight && $this->_barcode->getType() != 'error') {
  213. $height = $this->_userHeight;
  214. }
  215. $foreColor = $this->_barcode->getForeColor();
  216. $backgroundColor = $this->_barcode->getBackgroundColor();
  217. $this->_resource = imagecreatetruecolor($width, $height);
  218. $this->_imageBackgroundColor = imagecolorallocate(
  219. $this->_resource,
  220. ($backgroundColor & 0xFF0000) >> 16,
  221. ($backgroundColor & 0x00FF00) >> 8,
  222. $backgroundColor & 0x0000FF
  223. );
  224. $this->_imageForeColor = imagecolorallocate(
  225. $this->_resource,
  226. ($foreColor & 0xFF0000) >> 16,
  227. ($foreColor & 0x00FF00) >> 8,
  228. $foreColor & 0x0000FF
  229. );
  230. $white = imagecolorallocate($this->_resource, 255, 255, 255);
  231. imagefilledrectangle($this->_resource, 0, 0, $width - 1, $height - 1, $white);
  232. }
  233. $this->_adjustPosition(imagesy($this->_resource), imagesx($this->_resource));
  234. imagefilledrectangle(
  235. $this->_resource,
  236. $this->_leftOffset,
  237. $this->_topOffset,
  238. $this->_leftOffset + $barcodeWidth - 1,
  239. $this->_topOffset + $barcodeHeight - 1,
  240. $this->_imageBackgroundColor
  241. );
  242. }
  243. /**
  244. * Check barcode parameters
  245. *
  246. * @return void
  247. */
  248. protected function _checkParams()
  249. {
  250. $this->_checkDimensions();
  251. }
  252. /**
  253. * Check barcode dimensions
  254. *
  255. * @return void
  256. */
  257. protected function _checkDimensions()
  258. {
  259. if ($this->_resource !== null) {
  260. if (imagesy($this->_resource) < $this->_barcode->getHeight(true)) {
  261. require_once 'Zend/Barcode/Renderer/Exception.php';
  262. throw new Zend_Barcode_Renderer_Exception(
  263. 'Barcode is define outside the image (height)'
  264. );
  265. }
  266. } else {
  267. if ($this->_userHeight) {
  268. $height = $this->_barcode->getHeight(true);
  269. if ($this->_userHeight < $height) {
  270. require_once 'Zend/Barcode/Renderer/Exception.php';
  271. throw new Zend_Barcode_Renderer_Exception(sprintf(
  272. "Barcode is define outside the image (calculated: '%d', provided: '%d')",
  273. $height,
  274. $this->_userHeight
  275. ));
  276. }
  277. }
  278. }
  279. if ($this->_resource !== null) {
  280. if (imagesx($this->_resource) < $this->_barcode->getWidth(true)) {
  281. require_once 'Zend/Barcode/Renderer/Exception.php';
  282. throw new Zend_Barcode_Renderer_Exception(
  283. 'Barcode is define outside the image (width)'
  284. );
  285. }
  286. } else {
  287. if ($this->_userWidth) {
  288. $width = $this->_barcode->getWidth(true);
  289. if ($this->_userWidth < $width) {
  290. require_once 'Zend/Barcode/Renderer/Exception.php';
  291. throw new Zend_Barcode_Renderer_Exception(sprintf(
  292. "Barcode is define outside the image (calculated: '%d', provided: '%d')",
  293. $width,
  294. $this->_userWidth
  295. ));
  296. }
  297. }
  298. }
  299. }
  300. /**
  301. * Draw and render the barcode with correct headers
  302. *
  303. * @return mixed
  304. */
  305. public function render()
  306. {
  307. $this->draw();
  308. header("Content-Type: image/" . $this->_imageType);
  309. $functionName = 'image' . $this->_imageType;
  310. call_user_func($functionName, $this->_resource);
  311. @imagedestroy($this->_resource);
  312. }
  313. /**
  314. * Draw a polygon in the image resource
  315. *
  316. * @param array $points
  317. * @param integer $color
  318. * @param boolean $filled
  319. */
  320. protected function _drawPolygon($points, $color, $filled = true)
  321. {
  322. $newPoints = array(
  323. $points[0][0] + $this->_leftOffset,
  324. $points[0][1] + $this->_topOffset,
  325. $points[1][0] + $this->_leftOffset,
  326. $points[1][1] + $this->_topOffset,
  327. $points[2][0] + $this->_leftOffset,
  328. $points[2][1] + $this->_topOffset,
  329. $points[3][0] + $this->_leftOffset,
  330. $points[3][1] + $this->_topOffset,
  331. );
  332. $allocatedColor = imagecolorallocate(
  333. $this->_resource,
  334. ($color & 0xFF0000) >> 16,
  335. ($color & 0x00FF00) >> 8,
  336. $color & 0x0000FF
  337. );
  338. if ($filled) {
  339. imagefilledpolygon($this->_resource, $newPoints, 4, $allocatedColor);
  340. } else {
  341. imagepolygon($this->_resource, $newPoints, 4, $allocatedColor);
  342. }
  343. }
  344. /**
  345. * Draw a polygon in the image resource
  346. *
  347. * @param string $text
  348. * @param float $size
  349. * @param array $position
  350. * @param string $font
  351. * @param integer $color
  352. * @param string $alignment
  353. * @param float $orientation
  354. */
  355. protected function _drawText($text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
  356. {
  357. $allocatedColor = imagecolorallocate(
  358. $this->_resource,
  359. ($color & 0xFF0000) >> 16,
  360. ($color & 0x00FF00) >> 8,
  361. $color & 0x0000FF
  362. );
  363. if ($font == null) {
  364. $font = 3;
  365. }
  366. $position[0] += $this->_leftOffset;
  367. $position[1] += $this->_topOffset;
  368. if (is_numeric($font)) {
  369. if ($orientation) {
  370. /**
  371. * imagestring() doesn't allow orientation, if orientation
  372. * needed: a TTF font is required.
  373. * Throwing an exception here, allow to use automaticRenderError
  374. * to informe user of the problem instead of simply not drawing
  375. * the text
  376. */
  377. require_once 'Zend/Barcode/Renderer/Exception.php';
  378. throw new Zend_Barcode_Renderer_Exception(
  379. 'No orientation possible with GD internal font'
  380. );
  381. }
  382. $fontWidth = imagefontwidth($font);
  383. $positionY = $position[1] - imagefontheight($font) + 1;
  384. switch ($alignment) {
  385. case 'left':
  386. $positionX = $position[0];
  387. break;
  388. case 'center':
  389. $positionX = $position[0] - ceil(($fontWidth * strlen($text)) / 2);
  390. break;
  391. case 'right':
  392. $positionX = $position[0] - ($fontWidth * strlen($text));
  393. break;
  394. }
  395. imagestring($this->_resource, $font, $positionX, $positionY, $text, $color);
  396. } else {
  397. $box = imagettfbbox($size, 0, $font, $text);
  398. switch ($alignment) {
  399. case 'left':
  400. $width = 0;
  401. break;
  402. case 'center':
  403. $width = ($box[2] - $box[0]) / 2;
  404. break;
  405. case 'right':
  406. $width = ($box[2] - $box[0]);
  407. break;
  408. }
  409. imagettftext(
  410. $this->_resource,
  411. $size,
  412. $orientation,
  413. $position[0] - ($width * cos(pi() * $orientation / 180)),
  414. $position[1] + ($width * sin(pi() * $orientation / 180)),
  415. $allocatedColor,
  416. $font,
  417. $text
  418. );
  419. }
  420. }
  421. }