我最难使用Laravel图像干预调整图像大小。我可以正常命名和保存图像,但是当我添加图像干预时,它不会将创建的新文件保存到文件夹中。
这是我的控制器中的内容
//This all works
$title = str_slug(request('title'));
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('cover_image')->getClientOriginalExtension();
$fileNameToStore= $title.'.'.$extension;
$thumbnailpic= 'thumb'.'-'.$fileNameToStore;
//store image
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
//Here is where I am trying to resize and it breaks
$file = Input::file('cover_image');
Image::make( $file->getRealPath() )->fit(340, 340)->save('public/cover_images/' . $thumbnailpic);
这是我得到的错误
"Can't write image data to path (public/cover_images/thumb-imagename.png)"
如果我删除两行调整大小代码,一切正常。我正在本地运行它,并已将所有内容完全开放以获取权限。不知道还能做什么。 谢谢!
问题似乎与保存路径有关,您可以尝试像这样给出完整路径吗,
$targetPath = storage_path().'/app/public/cover_images/';
...
->save($targetPath . $thumbnailpic);
我希望这会有所帮助
所以我终于想通了。我正在保存图像干预显然不喜欢的public_path。所以我使用了下面的代码
$source = storage_path().'/app/public/cover_images/'.$fileNameToStore;
$target = storage_path().'/app/public/cover_images/' . $thumbnailpic;
Image::make($source)->fit(140, 140)->save($target);
它奏效了!