Laravel,从public / uploads文件夹中获取图像

时间:2016-01-13 22:42:04

标签: laravel-5 laravel-5.1

控制器:

public function index()
{
    if (!file_exists("uploads/profiles/".\Auth::User()->id.".jpeg")) {
        $image_content = File::get("uploads/profiles/default.jpg");
        $image =  response($image_content, 200)->header('Content-Type', 'image/jpeg');
        $size = getimagesize($image);
        $aspectratio = $size[0]/$size[1];
        $img_thumbnail = Image::make($image)->resize(50*$aspectratio,50);
        $img_profile = Image::make($image)->resize(160*$aspectratio,160);
        $imgname = \Auth::User()->id;
        $img_thumbnail->save('uploads/thumbnails/'.$imgname.".jpeg");
        $img_profile->save('uploads/profiles/'.$imgname.".jpeg");
}

错误::无法打开流:没有此类文件或目录。

实际上,我想获取default.jpg图像并将其保存到另外两个具有不同扩展名的文件夹中。

3 个答案:

答案 0 :(得分:1)

        if( !file_exists('uploads/thumbnails/'. $folder)){
            @mkdir('uploads/thumbnails/'. $folder, 0755);
        }
        if( !file_exists('uploads/profiles/'. $folder)){
            @mkdir('uploads/profiles/'. $folder, 0755);
        }

        $img = 'uploads/profiles/'. \Auth::User()->id. '.jpeg';
        if ( !file_exists($img)){
            $save_extension = '.jpeg';
            Image::make($img)
                ->resize(50, null, function ($constraint) {
                    $constraint->aspectRatio();
                })->save('uploads/thumbnails/' . \Auth::User()->id . $save_extension);
            Image::make($img)
                ->resize(160, null, function ($constraint) {
                    $constraint->aspectRatio();
                })->save('uploads/profiles/' . \Auth::User()->id . $save_extension);
        }

http://image.intervention.io/api/resize

答案 1 :(得分:0)

public function index()
{
    if (!file_exists("uploads/profiles/".\Auth::User()->id.".jpeg")) {
        $image = File::get("uploads/profiles/default.jpg");
        return response($image)->header('Content-Type', 'image/jpeg');
}

答案 2 :(得分:0)

确保您拥有uploads/profiles/

的正确权限
chmod -R 777 /uploads/profiles/