图像干预保存路径

时间:2019-09-24 22:47:04

标签: laravel intervention

我的问题是当我需要保护图像时如何创建合适的路径。我尝试了教程,并且先前的SO干预回答了问题,但是无法应用建议的解决方案。我在Laravel中使用图像干预。

我当前的路径是:

            $filenameWithExtThumb = $request->file("thumbnail")->getClientOriginalName();
            $filenameThumb = pathinfo($filenameWithExtThumb, PATHINFO_FILENAME);
            $extensionThumb = $request->file("thumbnail")->getClientOriginalExtension();
            $fileNameToStoreThumb = $filenameThumb."_".time().".".$extensionThumb;

            $image_resize = Image::make($request->file("thumbnail")->getRealPath());              
            $image_resize->resize(320, 240);

            $pathThumb = $request->file("thumbnail")->storeAs("public/".auth()->user()->name."'s Thumbnails", $fileNameToStoreThumb);

如果我只是将其存储为未调整大小,则效果很好。如果我使用

$pathThumb = $image_resize->storeAs("public/".auth()->user()->name."'s Thumbnails", $fileNameToStoreThumb);

它不起作用。 我也尝试过:

$image_resize->save("/public/storage/".auth()->user()->name."'s Thumbnails");

这也不起作用。 和

$pathThumb = $image_resize->save("/public/storage/".auth()->user()->name."'s Thumbnails");

我想用这个:

$pathThumb = $request->file("thumbnail")->storeAs("public/".auth()->user()->name."'s Thumbnails", $fileNameToStoreThumb);

$image_resize。有可能吗?

我的路径是: “公共/存储” .auth()-> user()->名称。“的缩略图”,$ fileNameToStoreThumb 示例:public/storage/ -> Johnny's Thumbnails -> nameOfTheFile.jpg

编辑1: 通过执行以下路径来解决: $pathThumb = $image_resize->save(public_path("storage/".auth()->user()->name."'s Thumbnails/".$fileNameToStoreThumb));

现在我要删除以前的缩略图,除非它不是“ nothumbnail.jpg”。

                if ($request->hasFile('thumbnail')) {

                    if($video->thumbnail==="../nothumbnail.jpg"){

                        $video->thumbnail = $fileNameToStoreThumb;
                    }       

                    if($video->thumbnail!=="../nothumbnail.jpg"){
                        Storage::delete("public/storage/".auth()->user()->name."'s Thumbnails/".$video->thumbnail);

                        $video->thumbnail = $fileNameToStoreThumb;
                    }   

                }
                else{

                    if($video->thumbnail==="../nothumbnail.jpg"){

                        $video->thumbnail = "../nothumbnail.jpg";
                    } 

                    if($video->thumbnail!=="../nothumbnail.jpg"){
                        Storage::delete("public/storage/".auth()->user()->name."'s Thumbnails/".$video->thumbnail);

                        $video->thumbnail = "../nothumbnail.jpg";
                    } 

                }

这又是一个路径问题:(

Edit2 :解决方案1: 很多小时后,我没有发现错误,因为我做了很多修改,因此我一生都找不到它。 因此,我意外地设计了一种非常偶然性质的解决方案:

                $filenameWithExtThumb = null;
                $filenameThumb = null;
                $extensionThumb = null;
                $fileNameToStoreThumb = null;
                $image_resize = null;
                $pathThumb = null;

                if($request->hasFile("thumbnail")){

                    $filenameWithExtThumb = $request->file("thumbnail")->getClientOriginalName();
                    $filenameThumb = pathinfo($filenameWithExtThumb, PATHINFO_FILENAME);
                    $extensionThumb = $request->file("thumbnail")->getClientOriginalExtension();
                    $fileNameToStoreThumb = $filenameThumb."_".time().".".$extensionThumb;

                    $image_resize = Image::make($request->file("thumbnail")->getRealPath());              
                    $image_resize->resize(320, 240);

                    $pathThumb = $image_resize->save(public_path("storage/".auth()->user()->name."'s Thumbnails/".$fileNameToStoreThumb));

                    if($video->thumbnail!=="../nothumbnail.jpg"){

                        unlink(public_path("storage/".auth()->user()->name."'s Thumbnails/".$video->thumbnail));
                        $video->thumbnail = $fileNameToStoreThumb;

                    }

                    if($video->thumbnail==="../nothumbnail.jpg"){

                        $video->thumbnail = $fileNameToStoreThumb;

                    }

                }
                else{

                    if($video->thumbnail!=="../nothumbnail.jpg"){

                        unlink(public_path("storage/".auth()->user()->name."'s Thumbnails/".$video->thumbnail));
                        $video->thumbnail = "../nothumbnail.jpg";

                    }

                    if($video->thumbnail==="../nothumbnail.jpg"){

                        $video->thumbnail = "../nothumbnail.jpg";

                    }

                }

虽然没有使用Storage Facade,但是香草php取消链接以删除文件。可以不使用Laravel的内置功能而不是香草的功能吗?

1 个答案:

答案 0 :(得分:0)

我认为存储路径有些混乱。尝试使用此代码,并确保您已完成php artisan storage:link Docs

if ($request->hasFile('thumbnail') && $request->file('thumbnail')->isValid()) {
    $extension = $request->file("thumbnail")->getClientOriginalExtension();
    $image_resize = Image::make($request->file('thumbnail'));
    $image_resize->resize(320, 240);
    $hash = md5($image_resize->__toString());
    $filename = $hash . "." . $extension;
    $image_resize->save(storage_path("app/public/" . auth()->user()->name . "'s Thumbnails/" . $filename)); 
}

这应该将文件保存到 / public / storage / User's Thumbnails / filename.ext

链接的 storage / app / public / User's Thumbnails / filename.ext >