【问题标题】:How do I restfully return images using laravel 4?如何使用 laravel 4 从容地返回图像?
【发布时间】:2014-10-06 07:17:52
【问题描述】:

我设置了一个服务器来提供不同域上的网页(特别是移动设备或 localhost:9000,其中 laravel 在 localhost:8000 上提供服务)。我正在尝试将这些页面上的图像请求返回到我的 laravel 服务器,但我遇到了问题。从论坛帖子中,我认为在请求上设置标头可以解决问题,但是当我导航到 /api/v1/images/default.jpg 时,没有显示默认的猫图像。相反,我得到一个没有图像的盒子。

现在,图像在我的公用文件夹中,所以如果我浏览到 /public/images/default.jpg 我确实看到了我的猫图像,但我宁愿在我的 /api/v1/... 路由中提供图像.

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

【问题讨论】:

标签: php laravel laravel-4 laravel-routing


【解决方案1】:

使用Response::stream 方法来做:

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

      return Response::stream(function () {

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

              readfile($filename);

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

【讨论】:

    【解决方案2】:

    如果您想从 ftp 服务器流式传输您的图像($imageName 是参数)

        $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)
    

    【讨论】:

      【解决方案3】:

      @安德鲁·奥尔布赖特,

      如果您的图像位于 laravel 应用目录中,那么您可以使用

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

      对于图像处理,您可以尝试干预

      【讨论】:

      • 它在我的公用文件夹中,即使我的公用文件夹在我的应用程序文件夹中,也返回 app_path() 。 '/公共/图像/' 。 $图像名称;只返回路径而不是图像。我不想做图像处理,只是提供图像。
      • @Andrew Allbright,你可以使用 $image = File::get(PATH_TO_YOUR_IMAGE);然后返回 Response::make($image, 200, ['content-type' => 'image/jpg']);
      • 由于某种原因也不起作用...啊,我放弃了,我将在客户端代码中维护一个单独的变量,该变量指向公共文件夹以获取图像。它会使我的客户端代码更加复杂,但提供图像作为响应似乎让我难以理解。无论如何感谢您的帮助!
      • 应该是 storage_path().'/app/public/images/....';
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 2021-07-30
      • 2021-05-25
      • 2020-12-15
      相关资源
      最近更新 更多