仅上传第一张图片,在Laravel中分别上传多张图片

时间:2019-11-28 03:32:33

标签: php arrays laravel

Laravel Newbie在这里,想问一下如何一张一张地上传多张图片。可以选择多个图像,但是将一个图像一个个地上传是行不通的,并且第一个图像仅在提交后显示。请帮忙。

控制器

    if (\Input::file('photos')) {
                $base_path =
                    dirname(__FILE__) . "/../../../../uploads/img/gallery/" . $add->id . "/";
                ini_set('gd.jpeg_ignore_warning', 1);
                //get last uploaded file name
                $files = (\Input::file('photos'));
                $list_image = $temp = explode(",", $request->list_image);
                foreach ($files as $key => $value) {
                        if (!is_null($value)) {
                        $temp = explode(".", $value->getClientOriginalName());
                        $newfilename = $value->getClientOriginalName();
                        if (!file_exists($base_path)) {
                            mkdir($base_path);
                            chmod($base_path, 0777);
                        }
                        $key = array_search($newfilename, $list_image);
                        if ($key == 0) {
                            $newfilename = $add->id . '_primary' . '.' . end($temp);
                        } else {
                                $newfilename = chr(64 + $key) . '_' . $newfilename;
                        }
                        $image = \Image::make($value);
                        // perform orientation using intervention
                        $image->orientate();
                        $new_width = $image->width();
                        $new_height = $image->height();
                        if ($new_width > 800) {
                            $percent = 0.7;
                            $new_width = $new_width * $percent;
                            $new_height = $new_height * $percent;
                        }

                        // resize image to fixed size
                        $image->resize($new_width, $new_height);

                        // create a new Image instance for inserting
                        $watermark = \Image::make(
                            public_path() . '/assets/img/icons/watermark.png',

                        );
                        // insert watermark at bottom-right corner with 10px offset
                        $image->insert(
                            public_path() . '/assets/img/icons/watermark.png',
                            'bottom-right',
                            10,
                            10,
                        );
                        $image->save($base_path . $newfilename);
                    }
                }

        }

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作。

    $list_image = $temp = explode(",", $request->list_image);

if($request->hasfile('photos'))
        {
         $destinationPath = public_path('uploads/img/gallery/'. $add->id.'/'); 
            foreach($request->file('photos') as $k => $file)
            {

            $temp = explode(".", $file->getClientOriginalName());
            $newfilename = $file->getClientOriginalName();

            $key = array_search($newfilename, $list_image);
            if ($key == 0) {
                  $newfilename = $add->id . '_primary' . '.' . end($temp);
            } else {
                  $newfilename = chr(64 + $key) . '_' . $newfilename;
            }

                $name = $newfilename.'.'.$file->getClientOriginalExtension();           

                File::makeDirectory($destinationPath, $mode = 0777, true, true);
            $file->move($destinationPath, $name);
            $tdestinationPath = public_path('uploads/img/gallery/thumb'. $add->id.'/');
            try {
                    $img = Image::make($file->getRealPath());
                    $img->resize(50, 50, function ($constraint) {
                        $constraint->aspectRatio();
                    })->save($tdestinationPath.'/'.$name);      

                    $img->destroy(); 

                } catch (Exception $e) {
                    $response_data=array('images'=>"",'success'=>0,'message'=>$e->getMessage());
            }

                $image_arr[] = $name;
            }
            $uploaded_img = implode(',',$image_arr);    
            if(!empty($hidden_property_img)){
                $uploaded_img = $hidden_property_img.','.$uploaded_img;
            }   
            $data['images'] = $uploaded_img;    

        }
相关问题