Vali
Vali

Reputation: 11

Intervention/Image save function always create corrupted files (0 byte)

I want to save images with the Image class from Intervention after they have been cropped. My Problem is, that images created this way dont show in the browser. I cant even open them in my filesystem => all the files have 0 bytes.

My Controller:

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

This should create a new cropped image in the given public path. But it only creates a 0 byte file.

When I dont try $imageToCrop->...->save(); but $imageToCrop->...->response(); and return this as response of the store function, the cropped image is shown in my browser.

Upvotes: 0

Views: 654

Answers (1)

Vali
Vali

Reputation: 11

tcj answered/resolved my question/problem! The problem was the missing / at the end of the $carouselImagesPath.

So i changed it to: $carouselImagesPath = public_path('/storage/resources/images/carousel/') . $filename;

Now everything works as expected.

Upvotes: 1

Related Questions