PsrResponseCommand.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Intervention\Image\Commands;
  3. use GuzzleHttp\Psr7\Response;
  4. class PsrResponseCommand extends AbstractCommand
  5. {
  6. /**
  7. * Builds PSR7 compatible response. May replace "response" command in
  8. * some future.
  9. *
  10. * Method will generate binary stream and put it inside PSR-7
  11. * ResponseInterface. Following code can be optimized using native php
  12. * streams and more "clean" streaming, however drivers has to be updated
  13. * first.
  14. *
  15. * @param \Intervention\Image\Image $image
  16. * @return boolean
  17. */
  18. public function execute($image)
  19. {
  20. $format = $this->argument(0)->value();
  21. $quality = $this->argument(1)->between(0, 100)->value();
  22. //Encoded property will be populated at this moment
  23. $stream = $image->stream($format, $quality);
  24. $mimetype = finfo_buffer(
  25. finfo_open(FILEINFO_MIME_TYPE),
  26. $image->getEncoded()
  27. );
  28. $this->setOutput(new Response(
  29. 200,
  30. [
  31. 'Content-Type' => $mimetype,
  32. 'Content-Length' => strlen($image->getEncoded())
  33. ],
  34. $stream
  35. ));
  36. return true;
  37. }
  38. }