Response.php 1.5 KB

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