简体   繁体   English

无法将图像数据写入路径。 干预图像 Laravel 5.2

[英]Can't write image data to path. Intervention Image Laravel 5.2

   public function newItem(Request $request){

        $image = $request->file('image');
        $img = time().'.'.$image->getClientOriginalExtension();
        $watermark = Image::make('images/watermark.png');
        $destinationPath = public_path('/products');
        $img = Image::make($image->getRealPath());
        $img->resize(300, 365, function ($constraint) {
            $constraint->aspectRatio();
        })->insert($watermark, 'center');
        File::exists($destinationPath) or File::makeDirectory($destinationPath);
        $img->save($destinationPath.'/'.$img);

} }

I keep getting Can't write image data to path Can anyone figure out what I'm doing wrong?我不断收到无法将图像数据写入路径有人能弄清楚我做错了什么吗? The question might seem duplicate, but other suggestions in similar questions did not work for me.这个问题可能看起来重复,但类似问题中的其他建议对我不起作用。

Thanks in advance提前致谢

Try this code it's worked for me试试这个代码它对我有用

public function newItem(){

    $image = Input::file('image');
    $destinationPath = '/products';
    $img = time().'.'.$image->getClientOriginalExtension();
    $watermark = Image::make('images/watermark.png');
    $img = Image::make($image->getRealPath());
    $img->resize(300, 365, function ($constraint) {
        $constraint->aspectRatio();
    })->insert($watermark, 'center');
    File::exists($destinationPath) or File::makeDirectory($destinationPath);
    $img->save($destinationPath.'/'.$img);
}

For the sake of others that might have the same issue.为了其他可能有同样问题的人。 This is how I solved it:我是这样解决的:

 $image = $request->file('image');
    $img = time().'.'.$image->getClientOriginalExtension();

$watermark = Image::make('images/watermark.png');
$destinationPath = public_path('/products');
Image::make($image->getRealPath())->resize(300, 365, function ($constraint) {
    $constraint->aspectRatio();
})->insert($watermark, 'center')->save($destinationPath.'/'.$img);

The mistake I was making was assigning Image::make() to a variable.我犯的错误是将 Image::make() 分配给一个变量。 You can look at my code here and the one above in my question.您可以在此处查看我的代码以及我的问题中的上述代码。

Make sure to create mentioned path folders (passing with image save) in laravel public folder.确保在 Laravel 公共文件夹中创建提到的路径文件夹(通过图像保存传递)。 This will work automatically.这将自动工作。

If somebody use File Facade:如果有人使用 File Facade:

    File::exists($destinationPath) or File::makeDirectory($destinationPath);

You have to remember that if your $destinationPath contains more than 1 folder, you have to set 2nd & 3rd parameters like $mode & $recursive to create final destination folder and prepare directory for file upload.你必须记住,如果你的$destinationPath包含 1 个以上的文件夹,你必须设置第二和第三个参数,如$mode$recursive来创建最终目标文件夹并准备文件上传目录。

Example:示例:

    File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM