EpsWriter.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // Please note an EPS has a reversed Y axis compared to PNG and SVG
  24. $data['matrix'] = array_reverse($data['matrix']);
  25. foreach ($data['matrix'] as $row => $values) {
  26. foreach ($values as $column => $value) {
  27. if (1 === $value) {
  28. $x = $data['margin_left'] + $data['block_size'] * $column;
  29. $y = $data['margin_left'] + $data['block_size'] * $row;
  30. $epsData[] = $x.' '.$y.' '.$data['block_size'].' '.$data['block_size'].' F';
  31. }
  32. }
  33. }
  34. return implode("\n", $epsData);
  35. }
  36. public static function getContentType(): string
  37. {
  38. return 'image/eps';
  39. }
  40. public static function getSupportedExtensions(): array
  41. {
  42. return ['eps'];
  43. }
  44. public function getName(): string
  45. {
  46. return 'eps';
  47. }
  48. }