public function generateResponse(ResponseInterface $response) : HttpResponse { /** @var File $file */ $file = $response['file']; $image = $response['image']; $imageStream = tmpfile(); fputs($imageStream, $this->imageEditor->output($file, $image)); rewind($imageStream); return new \Zend\Diactoros\Response($imageStream, 200, ['Content-Type' => $file->getMimeType()->toString(), 'Content-Disposition' => 'attachment; filename="' . $file->getName()->toString() . '"']); }
public function it_can_execute_a_request(RequestInterface $request, File $file, ImageInterface $image, File $newImage) { $fullPath = '/path/to/file.ext'; $operations = ['resize' => ['width' => 320, 'height' => 480]]; $request->offsetGet('path')->willReturn($fullPath); $request->offsetGet('operations')->willReturn($operations); $request->getAcceptContentType()->willReturn('*/*'); $this->imageRepository->getByFullPath($fullPath)->willReturn($file); $this->imageEditor->process($file, $operations)->willReturn($image); $this->imageEditor->output($file, $image)->willReturn('abcdefg'); $this->imageRepository->createImage($file, new Argument\Token\TypeToken('resource'))->willReturn($newImage); $response = $this->executeRequest($request); $response->shouldHaveType(ResponseInterface::class); $response->offsetGet('data')->shouldReturn($newImage); }
public function it_can_generate_an_image_response(ResponseInterface $response, File $file, ImageInterface $image) { $responseMock = 'i-am-not-really-an-image'; $fileName = FileNameValue::get('image.png'); $fileMimeType = MimeTypeValue::get('image/png'); $response->offsetGet('image')->willReturn($image); $response->offsetGet('file')->willReturn($file); $file->getName()->willReturn($fileName); $file->getMimeType()->willReturn($fileMimeType); $this->imageEditor->output($file, $image)->willReturn($responseMock); $httpResponse = $this->generateResponse($response); $httpResponse->shouldHaveType(HttpResponse::class); $httpResponse->getStatusCode()->shouldReturn(200); $httpResponse->getHeaderLine('Content-Type')->shouldReturn($fileMimeType->toString()); $httpResponse->getBody()->getContents()->shouldReturn($responseMock); }
public function executeRequest(RequestInterface $request) : ResponseInterface { try { // Fetch existing image and process operations on it $file = $this->imageRepository->getByFullPath($request['path']); $image = $this->imageEditor->process($file, $request['operations']); // Get resulting image and store in stream $contents = tmpfile(); fwrite($contents, $this->imageEditor->output($file, $image)); $newImage = $this->imageRepository->createImage($file, $contents); return new Response(self::MESSAGE, ['data' => $newImage], $request); } catch (NoUniqueResultException $e) { return new NotFoundResponse([], $request); } catch (\Throwable $exception) { $this->log(LogLevel::ERROR, $exception->getMessage()); throw new ResponseException('An error occurred during GetEditedImageHandler.', new ServerErrorResponse([], $request)); } }