我正在努力用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)"
如果我删除两行调整大小代码,一切都很完美。我在本地运行它已经完全打开所有权限。不知道还能做什么。谢谢!
答案 0 :(得分:0)
问题似乎与保存路径有关,你能试试这样的完整路径,
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py"
in get_response
174. response =
self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py"
in get_response
172. response = response.render()
File "/usr/lib/python2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content
File "/usr/lib/python2.7/site-packages/django/template/response.py" in
rendered_content
137. content = template.render(context, self._request)
File "/usr/lib/python2.7/site-packages/django/template/backends/django.py"
in render
95. return self.template.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
204. with context.bind_template(self):
File "/usr/lib64/python2.7/contextlib.py" in __enter__
17. return self.gen.next()
exceptions must be old-style classes or derived from BaseException, not str
Exception Location:
/usr/lib/python2.7/site-packages/django/utils/module_loading.py in
import_string, line 23
我希望这会有所帮助
答案 1 :(得分:0)
所以我终于明白了。我保存到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);
它有效!