PartKeepr\ImageBundle\Controller\ImageController::getImageAction PHP Method

getImageAction() public method

public getImageAction ( Request $request, $id ) : ImageResponse | Response
$request Symfony\Component\HttpFoundation\Request
$id
return PartKeepr\ImageBundle\Response\ImageResponse | Symfony\Component\HttpFoundation\Response
    public function getImageAction(Request $request, $id)
    {
        /**
         * @var EntityManager
         */
        $em = $this->getDoctrine()->getManager();
        /**
         * @var PartKeeprImage
         */
        $image = $em->find($this->getEntityClass(), $id);
        $width = $request->get('maxWidth');
        $height = $request->get('maxHeight');
        if ($width === null) {
            $width = 200;
        }
        if ($height === null) {
            $height = 200;
        }
        if ($image === null) {
            return new ImageResponse($width, $height, 404, '404 not found');
        }
        try {
            $file = $this->fitWithin($image, $width, $height);
        } catch (FileNotFound $e) {
            $this->get('logger')->error($e->getMessage());
            return new ImageResponse($width, $height, 404, '404 not found');
        } catch (\Exception $e) {
            $this->get('logger')->error($e->getMessage());
            return new ImageResponse($width, $height, 500, '500 Server Error');
        }
        return new Response(file_get_contents($file), 200, ['Content-Type' => 'image/png']);
    }