Brave publisher PUBLISHER
Web developer Front-End in Oise, France - Gary Deshayes

Symfony 5 | Return an image via a controller route

Web developer Front-End in Oise, France - Gary Deshayes <--Traduction

Created at : Wednesday 1 September 2021

Installation of the road

First of all you need a controller with a route ready to receive an image name, and then return the image.

Then you need to install the following package: compose require symfony/mime which will allow Symfony to automatically detect the mime type to return.

You also need to import these packages on top of the controller.

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;

My file tree

folders symfony 5

My image is in the uploads folder

Once the Controller and the route are ready, here is the code to enter to return an image under Symfony 5 :


    /**
     * @Route("/img-route/{img}", name="img_route")
     * A route with one parameter
     */
    public function index($img): Response
    {
        //Retrieve the root folder with the kernel and then add the location of the 
        //file
        $filename = $this->getParameter('kernel.project_dir') . '/uploads/' . $img;
        //If the file exists then we return it, otherwise return 404
        if (file_exists($filename)) {
            //return a new BinaryFileResponse with the file name
            return new BinaryFileResponse($filename);
        } else {
            return new JsonResponse(null, 404);
        }
    }

Result of the route

symfony 5 route return img

Here is the result by typing the route in your browser, it is possible to call this route with an api or a <img> tag to display the image.

This tutorial is now over, thanks for reading and feel free to share :)

News publications :

Symfony 5 | Return an image via a controller route

Symfony

Created at : Wednesday 1 September 2021

VueJS 3, ExpressJS 4.17 | Uploading and resizing an image

Javascript Framework JS

Created at : Saturday 28 November 2020

Symfony Event Subscriber | Restricting an ip range with an Event Subscriber

Symfony

Created at : Saturday 14 November 2020

Angular, ExpressJS | JWT authentication with Angular 10 and ExpressJS 4 (MySQL)

Javascript SQL Framework JS

Created at : Sunday 20 September 2020

Symfony, Excel, CSV | Generating a CSV file for Excel with Symfony

Symfony

Created at : Thursday 6 August 2020

Symfony FormType Choice | Set data default to group of radio button

Symfony

Created at : Sunday 26 July 2020

Doctrine, Symfony | Difference between two dates in MySQL (Day, month, year...)

Symfony SQL

Created at : Thursday 18 June 2020

JavaScript, jQuery et Regex | Secure a password in real time with JS

Javascript jQuery

Created at : Monday 23 March 2020

Symfony, Doctrine | Retrieve old data of FormType

Symfony

Created at : Friday 14 February 2020

jQuery, Webpack and Symfony | Call jQuery in your twig files

Symfony Bugs

Created at : Sunday 26 January 2020