api.generatecard.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Generate print card management API
  4. */
  5. class GenerateCard {
  6. private $font = 'content/documents/card_print/font/AvanteBs_ExtraBold.ttf';
  7. /**
  8. * GenerateCard constructor
  9. *
  10. * @param string $nameTemplate
  11. */
  12. public function __construct($nameTemplate)
  13. {
  14. $this->im = imagecreatefromjpeg($nameTemplate);
  15. putenv('GDFONTPATH=' . realpath('.'));
  16. }
  17. public function saveImage($name)
  18. {
  19. imagejpeg($this->im, $name);
  20. imagedestroy($this->im);
  21. }
  22. /**
  23. * @param array $data
  24. *
  25. * @return $this
  26. */
  27. public function createStringForImage(array $data)
  28. {
  29. foreach ($data as $row) {
  30. if (count(array_filter($row)) !== count($row)) {
  31. continue;
  32. }
  33. $color = $this->getColorRGB($row['color']);
  34. $imageColor = imagecolorallocate($this->im, $color['r'], $color['g'], $color['b']);
  35. imagettftext(
  36. $this->im,
  37. $row['font_size'],
  38. 0,
  39. $row['left'],
  40. $row['top'],
  41. $imageColor,
  42. $this->font,
  43. $row['text']
  44. );
  45. }
  46. return $this;
  47. }
  48. /**
  49. * @param $colorSrt
  50. *
  51. * @return array
  52. */
  53. private function getColorRGB($colorSrt)
  54. {
  55. $colorArr = explode('.', $colorSrt);
  56. $color = array('r' => (int) $colorArr[0]);
  57. $color += array('g' => (int) $colorArr[1]);
  58. $color += array('b' => (int) $colorArr[2]);
  59. return $color;
  60. }
  61. }
  62. ?>