Response.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Intervention\Image;
  3. use Illuminate\Support\Facades\Response as IlluminateResponse;
  4. use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
  5. class Response
  6. {
  7. /**
  8. * Image that should be displayed by response
  9. *
  10. * @var Image
  11. */
  12. public $image;
  13. /**
  14. * Format of displayed image
  15. *
  16. * @var string
  17. */
  18. public $format;
  19. /**
  20. * Quality of displayed image
  21. *
  22. * @var int
  23. */
  24. public $quality;
  25. /**
  26. * Creates a new instance of response
  27. *
  28. * @param Image $image
  29. * @param string $format
  30. * @param int $quality
  31. */
  32. public function __construct(Image $image, $format = null, $quality = null)
  33. {
  34. $this->image = $image;
  35. $this->format = $format ? $format : $image->mime;
  36. $this->quality = $quality ? $quality : 90;
  37. }
  38. /**
  39. * Builds response according to settings
  40. *
  41. * @return mixed
  42. */
  43. public function make()
  44. {
  45. $this->image->encode($this->format, $this->quality);
  46. $data = $this->image->getEncoded();
  47. $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
  48. $length = strlen($data);
  49. if (function_exists('app') && is_a($app = app(), 'Illuminate\Foundation\Application')) {
  50. $response = IlluminateResponse::make($data);
  51. $response->header('Content-Type', $mime);
  52. $response->header('Content-Length', $length);
  53. } elseif (class_exists('\Symfony\Component\HttpFoundation\Response')) {
  54. $response = SymfonyResponse::create($data);
  55. $response->headers->set('Content-Type', $mime);
  56. $response->headers->set('Content-Length', $length);
  57. } else {
  58. header('Content-Type: ' . $mime);
  59. header('Content-Length: ' . $length);
  60. $response = $data;
  61. }
  62. return $response;
  63. }
  64. }