我试图保存一个被操纵的图像,我会把它推到s3。
我的工作代码此代码将图像直接保存在公用文件夹*中
public function store(Filesystem $filesystem)
{
$request = Input::all();
$validator = Validator::make($request, [
'images' => 'image'
]);
if ($validator->fails()) {
return response()->json(['upload' => 'false']);
}
$postId = $request['id'];
$files = $request['file'];
$media = [];
$watermark = Image::make(public_path('img/watermark.png'));
foreach($files as $file) {
$image = Image::make($file->getRealPath());
$image->crop(730, 547);
$image->insert($watermark, 'center');
$image->save($file->getClientOriginalName());
}
}
我想要实现的是能够将它保存在自己的文件夹中。首先,在公共文件夹的存储中,存储博客文章图像的最佳位置是什么?但不管怎样,当我这样做的时候:
$image->save('blogpost/' . $postId . '/' . $file->getClientOriginalName());
// Or this
$image->save(storage_path('app/blogpost/' . $postId . '/' . $file->getClientOriginalName()));
我得到错误:
公用中的文件夹
Image.php第138行出现NotWritableException:无法将图像数据写入路径(blogpost/146/cars/image.jpg(
或
存储路径
Image.php第138行出现NotWritableException:无法将图像数据写入路径/code/websites/blog/storage/app/blogpost/146/image.jpg
我试过
cd storage/app/
chmod -R 755 blogpost
而且它仍然不能工作
感谢您阅读此
好的,下面是我解决它的方法,我在存储之前先创建了目录,
Storage::disk('local')->makeDirectory('blogpost/' . $postId);
一旦创建了文件夹,我就继续存储操作过的图像,比如:
$image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName));
然后将图像推送到S3
$filesystem->put('blogpost/' . $postId . '/' . $imageName, file_get_contents(storage_path('app/blogpost/' . $postId . '/' . $imageName)));
这起到了的作用
您可以通过使用函数stream
将Intervation/Image
变量强制转换为数据流来解决此问题。然后使用Storage
Laravel立面来保存图像。
$img = Image::make('path-to-the-image.png')->crop(...)->insert->stream('jpg', 90)
Storage::put('where_I_want_the_image_to_be_stored.jpg', $img);
我正在改进@Amol Bansode的答案。
您收到此错误是因为指定的路径中不存在$postId
文件夹。
你可以这样做:
//I suggest you store blog images in public folder
//I assume you have created this folder `public'blogpost`
$path = public_path("blogpost/{$postId}");
//Lets create path for post_id if it doesn't exist yet e.g `public'blogpost'23`
if(!File::exists($path)) File::makeDirectory($path, 775);
//Lets save the image
$image->save($path . '/' . $file->getClientOriginalName());
Laravel 5需要权限才能写入整个Storage
文件夹,请尝试以下操作,
sudo chmod 755 -R storage
如果755
不起作用,请尝试777
。
在我的案例中,我将一个项目从Windows10迁移到Parrot(Debian-Linux(,我遇到了同样的问题,结果发现斜杠是向后斜杠,Linux对它们的解释不同。与Windows不同,它并不重要。
//Windows Code:
$image_resize->save(public_path(''storage'Features/' .'Name'.".".'png'));
//Linux Code (Working):
$image_resize->save(public_path('storage/Features/' .'Name'.".".'jpg'));
我知道这个问题与Laravel有关,但我是在使用WordPress时遇到的。如果有人来自WordPress世界,此代码有效(我收到了相同的"无法写入"错误(,并且更改目录权限将不起作用,因为库可能需要相对路径(下面的代码适用于通过composer将Intervention直接安装到任何PHP应用程序,而不是Laravel本身(-
require 'vendor/autoload.php';
use Intervention'Image'ImageManager;
$manager = new ImageManager(array('driver' => 'imagick'));
$image = $manager->make('PUBLIC IMAGE URL/LOCAL IMAGE PATH');
$image->crop(20, 20, 40, 40);
$image->save(__DIR__ . '/img/bar.png');
在我的例子中:
我在laravel 的config文件夹中的filesystem.php中仔细检查了我的符号链接
并删除所有符号链接,然后重新生成
php artisan storage:link
在我的情况下,我的fileName不是在windows中命名的有效格式,但它在linux或docker 上工作
$make_name = date('Y-m-d-H:i:s') . hexdec(uniqid()) . '.' . $img->getClientOriginalExtension();
当我删除这个date('Y-m-d-H:i:s')
时,它也工作得很好
我也遇到过同样的问题,但775权限没有对其进行排序,所以我将文件夹(以及子文件夹(的写入权限更改为777以解决这个问题