php - 无法保存图像干预图像。 Image.php 第 138 行中的 NotWritableException

标签 php image laravel-5

我正在尝试保存经过处理的图像,我会将其推送到 s3。

我的代码有效此代码将图像直接保存在公共(public)文件夹中*

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());

    }

}

我想要实现的是能够将它保存在它自己的文件夹中。首先,在 public 文件夹的 storage 中,什么是存储博客文章图像的最佳位置?但无论如何,当我这样做时:

$image->save('blogpost/' . $postId . '/' . $file->getClientOriginalName());

// Or this

$image->save(storage_path('app/blogpost/' . $postId . '/' . $file->getClientOriginalName()));

我得到错误:

folder within public

NotWritableException in Image.php line 138: Can't write image data to path (blogpost/146/cars/image.jpg)

or

storage path

NotWritableException in Image.php line 138: Can't write image data to path /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)));

这有效

关于php - 无法保存图像干预图像。 Image.php 第 138 行中的 NotWritableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31198379/

相关文章:

php - Mysql多连接查询问题

php - 如何获取 Magento 客户 ID

php - Laravel 5 并在单个字段中验证多个电子邮件地址

php - 如何将一个表单数据更新到 Laravel 中的不同表?

php - routes.php 中的 FatalErrorException 第 113 行 : Call to a member function delete() on null in Laravel 5. 2

javascript - Ajax 发帖速度很慢

image - 在 matlab 中,如何使用 imagesc 在 2D 热图中 'smooth' 像素

html - Div 底部的边框,没有填充或边距

node.js - 使用 fs.writeFile 保存图像

PHP 数据库中存储的变量值不正确