首页 > 解决方案 > 在 laravel 存储中保存原始图像和转换为 webp

问题描述

当我在我的项目中存储图像时,我正在尝试将原始 jpg 图像和转换后的 webp 图像保存到我的存储中。

 public function store($data)
    {
        if (!$data->hasFile('fileName')) {
            return response()->json(['upload_file_not_found'], 400);
        }

        $allowedfileExtension = ['jpg',  'jpeg'];
        $files = $data->file('fileName');
        $errors = [];
        $images = [];

        foreach ($data->fileName as $mediaFiles) {
           
            $extension = $mediaFiles->getClientOriginalExtension();
            

            $check = in_array($extension, $allowedfileExtension);

            if ($check) {
                // $path = $mediaFiles->store('public/images');
                $name = $mediaFiles->getClientOriginalName();
              

                //store image file into directory and db
                $image = new Image();
                $image->title = $name;
                // $image->path = substr($path, 7);
                $image->description = $data->description;
                $image->author_id = $data->author_id;
                $image->save();

                //put image to storage wih unique folder
                $path = $mediaFiles->storeAs('public/images/article-images/'.$image->id, $name);

                //try to convert to webp and add to storage
                
                $image = InterventionImage::make($mediaFiles)->encode('webp', 90)->resize(200, 250)->save('public/images/'  .  $name . '.webp');   
                //

                //update images path in database
                $image->update(['path' => substr($path, 7)]);


                //add to attach the article id
                if ($data->article_id) {
                    $image->articles()->attach($data->article_id);
                }

                array_push($images, $image);
            } else {
                return response()->json(['invalid_file_format'], 422);
            }
        }

        return $images;
    }


我正在使用干预库,但是当我尝试将转换后的图像保存到我的存储时,我收到错误“消息”:“无法将图像数据写入路径(public/images/newimage.jpg.webp)”, 有人可以帮忙吗有了这个或有任何其他建议如何做到这一点?

标签: laravelwebpintervention

解决方案


如果有人遇到同样的问题,我会这样做

public function newStore($data)
    {
        if (!$data->hasFile('fileName')) {
            return response()->json(['upload_file_not_found'], 400);
        }

        $allowedfileExtension = ['jpg', 'jpeg'];
        $files = $data->file('fileName');
        $errors = [];
        $images = [];

        foreach ($data->fileName as $mediaFiles) {

            $extension = $mediaFiles->getClientOriginalExtension();

            $check = in_array($extension, $allowedfileExtension);

            if ($check) {
                // $path = $mediaFiles->store('public/images');
                $name = $mediaFiles->getClientOriginalName();

                //getting the name of the file without extension
                $filename = pathinfo($name, PATHINFO_FILENAME);

                //store image file into directory and db
                $image = new Image();
                $image->title = $name;
                // $image->path = substr($path, 7);
                $image->description = $data->description;
                $image->author_id = $data->author_id;
                $image->save();

                //put image to storage in unique folder
                $path = $mediaFiles->storeAs('public/images/article-images/' . $image->id, $name);

                //imege conversion in webp format and move to right folder
                $interventionImage = InterventionImage::make($mediaFiles)->stream("webp", 100);
                Storage::disk('local')->put($filename . '.webp', $interventionImage, 'public');
                Storage::move($filename . '.webp', 'public/images/article-images/' . $image->id . '/' . $filename . '.webp');

                //update images path in database
                $image->update(['path' => substr($path, 7)]);

                //add to attach the article id
                if ($data->article_id) {
                    $image->articles()->attach($data->article_id);
                }

                array_push($images, $image);
            } else {
                return response()->json(['invalid_file_format'], 422);
            }
        }

        return $images;
    }

我的文件夹结构如下所示: 在此处输入图像描述


推荐阅读