【问题标题】:Intervention/Image save function always create corrupted files (0 byte)干预/图像保存功能总是创建损坏的文件(0 字节)
【发布时间】:2019-10-03 04:27:23
【问题描述】:

我想在裁剪后使用 Intervention 中的 Image 类保存图像。我的问题是,以这种方式创建的图像不会显示在浏览器中。我什至无法在我的文件系统中打开它们 => 所有文件都有 0 字节。

我的控制器:

        public function store(Request $request)
        {
            $this->validate($request, [
                'image' => 'required',
                'link' => 'nullable|url|max:255',
                'description' => 'max:255'
            ]);

            $image = $request->file('image');
            $filename = 'carousel_' . time() . '.' . $image->getClientOriginalExtension();
            $this->cropAndSaveCarouselimage($image, $request->image_crop_x, $request->image_crop_y, $request->image_crop_width, $request->image_crop_height);

            $carousel = new CarouselImage();
            $carousel->link = $request->link;
            $carousel->description = $request->description;
            $carousel->filename = $filename;
            $carousel->save();

            return redirect(route('carouselimages.index'))->with('success', 'Carouselimage added successfully...');
        }

        public function cropAndSaveCarouselimage($originalImage, $image_crop_x, $image_crop_y, $image_crop_width, $image_crop_height)
        {
            if (isset($originalImage)) {
                $filename = 'carousel_' . time() . '.' . $originalImage->getClientOriginalExtension();
                $carouselImagesPath = public_path('/storage/resources/images/carousel') . $filename;

                $imageToCrop = Image::make(File::get($originalImage));
                $imageToCrop
                    ->crop($image_crop_width, $image_crop_height, $image_crop_x, $image_crop_y)
                    ->save($carouselImagesPath, 100);
            }
        }

这应该在给定的公共路径中创建一个新的裁剪图像。但它只会创建一个 0 字节的文件。

当我不尝试 $imageToCrop->...->save(); 但是 $imageToCrop->...->response(); 并将其作为 store 函数的响应返回,裁剪后的图像会显示在我的浏览器中。

【问题讨论】:

  • 因为图片名称变了?
  • 您确定$carouselImagesPath 是正确的路径吗?我可以看到您没有在路径末尾添加/,除非您将其添加到$filename,但这不是一个好习惯。尝试将 public_path 更改为 '/storage/resources/images/carousel/' 看看你得到了什么。此外,在保存图像时,您应该尝试在不使用 100% 质量参数的情况下保存它,以查看是否存在问题。
  • time() 第一次和第二次不一样
  • @AhmedAboud 谢谢你在我的代码中展示了这个混乱,但这并没有解决问题。
  • @tcj 我不敢相信我监督了这一切,你是个英雄!

标签: php laravel intervention


【解决方案1】:

tcj 回答/解决了我的问题/问题! 问题是$carouselImagesPath 末尾缺少/。

所以我把它改成了:$carouselImagesPath = public_path('/storage/resources/images/carousel/') . $filename;

现在一切正常。

【讨论】:

    猜你喜欢
    • 2014-11-16
    • 1970-01-01
    • 2019-07-11
    • 2017-01-27
    • 2021-09-26
    • 2023-03-19
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    相关资源
    最近更新 更多