Image.php 15 KB

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