How to restore images using laravel 4?

I have a server configured to serve web pages on different domains (specifically mobile or localhost: 9000 where laravel is running on localhost: 8000). I am trying to return image requests for these pages to my laravel server but I am having problems. From a forum post I thought setting the headers on demand would do the trick, but when I go to / api / v 1 / images / default.jpg, the default cat image is not displayed. Instead, I get a box with no image.

The image is now in my public folder, so if I go to /public/images/default.jpg I see my cat's image, but I prefer to show the images in my / api / v 1 / ... path.

Route::get('images/{imageName}', function($imageName){
    $img = 'public/images/' . $imageName;
    // return $img;
    echo $img . "\n\n";
    if(File::exists($img)) {
        // return "true";
        // return Response::make($img, 200, array('content-type' => 'image/jpg'));
        // return Response::download($img, $imageName);
        // Set headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: inline; filename=\"".$imageName."\"");
        header("Content-Type: image/jpg");
        header("Content-Transfer-Encoding: binary");
        //stream the file out
        readfile($img);
        exit;
    } else {
        return "false";
    }
    return $img;
    // return File::exists($img);
    // return File::isFile('/images/' . $imageName);
    // return $imageName;
    // if(File::isFile('images/' + $imageName)){
    //  return Response::make('images/' + $imageName, 200, array('content-type' => 'image/jpg'));
    // }
});

      

+3


source to share


2 answers


Use the method Response::stream

for this:



Route::get('/image', function () {

      return Response::stream(function () {

              $filename = '/path/to/your/image.jpg';

              readfile($filename);

      }, 200, ['content-type' => 'image/jpeg']);
});

      

0


source


@Andrew Allbright,

If your images are inside laravel app directory you can use



$img = app_path().'/api/v1/images/' . $imageName;

      

For image manipulation, you can try intervention

-2


source







All Articles