EpsWriter.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * (c) Jeroen van den Enden <info@endroid.nl>
  5. *
  6. * This source file is subject to the MIT license that is bundled
  7. * with this source code in the file LICENSE.
  8. */
  9. namespace Endroid\QrCode\Writer;
  10. use Endroid\QrCode\QrCodeInterface;
  11. class EpsWriter extends AbstractWriter
  12. {
  13. public function writeString(QrCodeInterface $qrCode): string
  14. {
  15. $data = $qrCode->getData();
  16. $epsData = [];
  17. $epsData[] = '%!PS-Adobe-3.0 EPSF-3.0';
  18. $epsData[] = '%%BoundingBox: 0 0 '.$data['outer_width'].' '.$data['outer_height'];
  19. $epsData[] = '/F { rectfill } def';
  20. $epsData[] = number_format($qrCode->getBackgroundColor()['r'] / 100, 2, '.', ',').' '.number_format($qrCode->getBackgroundColor()['g'] / 100, 2, '.', ',').' '.number_format($qrCode->getBackgroundColor()['b'] / 100, 2, '.', ',').' setrgbcolor';
  21. $epsData[] = '0 0 '.$data['outer_width'].' '.$data['outer_height'].' F';
  22. $epsData[] = number_format($qrCode->getForegroundColor()['r'] / 100, 2, '.', ',').' '.number_format($qrCode->getForegroundColor()['g'] / 100, 2, '.', ',').' '.number_format($qrCode->getForegroundColor()['b'] / 100, 2, '.', ',').' setrgbcolor';
  23. foreach ($data['matrix'] as $row => $values) {
  24. foreach ($values as $column => $value) {
  25. if (1 === $value) {
  26. $x = $data['margin_left'] + $data['block_size'] * $column;
  27. $y = $data['margin_left'] + $data['block_size'] * $row;
  28. $epsData[] = $x.' '.$y.' '.$data['block_size'].' '.$data['block_size'].' F';
  29. }
  30. }
  31. }
  32. return implode("\n", $epsData);
  33. }
  34. public static function getContentType(): string
  35. {
  36. return 'image/eps';
  37. }
  38. public static function getSupportedExtensions(): array
  39. {
  40. return ['eps'];
  41. }
  42. public function getName(): string
  43. {
  44. return 'eps';
  45. }
  46. }