File indexing completed on 2025-01-26 05:29:13

0001 <?php
0002 
0003 namespace Intervention\Image\Commands;
0004 
0005 use GuzzleHttp\Psr7\Response;
0006 
0007 class PsrResponseCommand extends AbstractCommand
0008 {
0009     /**
0010      * Builds PSR7 compatible response. May replace "response" command in
0011      * some future.
0012      *
0013      * Method will generate binary stream and put it inside PSR-7
0014      * ResponseInterface. Following code can be optimized using native php
0015      * streams and more "clean" streaming, however drivers has to be updated
0016      * first.
0017      *
0018      * @param  \Intervention\Image\Image $image
0019      * @return boolean
0020      */
0021     public function execute($image)
0022     {
0023         $format = $this->argument(0)->value();
0024         $quality = $this->argument(1)->between(0, 100)->value();
0025 
0026         //Encoded property will be populated at this moment
0027         $stream = $image->stream($format, $quality);
0028 
0029         $mimetype = finfo_buffer(
0030             finfo_open(FILEINFO_MIME_TYPE),
0031             $image->getEncoded()
0032         );
0033 
0034         $this->setOutput(new Response(
0035             200,
0036             [
0037                 'Content-Type'   => $mimetype,
0038                 'Content-Length' => strlen($image->getEncoded())
0039             ],
0040             $stream
0041         ));
0042 
0043         return true;
0044     }
0045 }