3

I have a server set up to serve web pages on different domains (specifically a mobile device or localhost:9000 where laravel is serving on localhost:8000). I'm trying to return image requests on these pages to my laravel server but I'm running into problems. From a forum post, I thought that setting headers on a request would do the trick but, when I navigate to /api/v1/images/default.jpg, no default cat image is shown. Instead, I get a box with no image.

Now, the image is in my public folder so if I browse to /public/images/default.jpg I do see my cat image, but I'd rather serve images within my /api/v1/... route.

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'));
    // }
});
1

3 Answers 3

0

Use the Response::stream method to do it:

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

      return Response::stream(function () {

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

              readfile($filename);

      }, 200, ['content-type' => 'image/jpeg']);
});
Sign up to request clarification or add additional context in comments.

Comments

0

In case you want to stream your image from a ftp server ($imageName is the parameter)

    $server   = \Config::get('ftpconfig.server');
    $usuario  = \Config::get('ftpconfig.user');
    $password = \Config::get('ftpconfig.password');
    $path = \Config::get('ftpconfig.path');

    $file_location = "ftp://$usuario:".urlencode($password)."@".$server.$path.$imageName;

    $headers = [
        "Content-Type" => "image/jpeg",
        "Content-Length" => filesize($file_location),
        "Content-disposition" => "inline; filename=\"" . basename($file_location) . "\"",
    ];

    return \Response::stream(function () use ($file_location){

        readfile($file_location);
    }, 200, $headers)

Comments

-2

@Andrew Allbright,

If your images are located inside the laravel app directory then you can use

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

For image manipulation you can try Intervention

4 Comments

It's within my public folder and, even if my public folder is within my app folder, return app_path() . '/public/images/' . $imageName; only returns the path not the image. I'm not trying to do image manipulation, just serve the image.
@Andrew Allbright, you could use $image = File::get(PATH_TO_YOUR_IMAGE); and then return Response::make($image, 200, ['content-type' => 'image/jpg']);
Doesn't work either, for some reason... Ah I give up, I'll just maintain a separate variable in my client-side code that points to the public folder to get images. It'll make my client-side code more complicated but serving images as a response seems to elude me. Thanks for your help anyways!
It should be storage_path().'/app/public/images/....';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.