Удалить imgur из загрузки изображений - PullRequest
0 голосов
/ 16 июня 2019


Несколько месяцев назад мой друг добавил в мою cms созданную в laravel загрузку изображений через imgur, только то, что я хотел бы удалить, на cms, однако изображения сохраняются (локально), я быхотел бы удалить загрузку на imgur, и я хотел бы, чтобы изображения оставались локально

public function imageProfile(Request $request)
    {
        $user = Auth::user();
        $rules = array(
            'profile-image' => 'required|image|mimes:jpeg,png,jpg,gif|max:8192|dimensions:min_width=160,min_height=160',
        );

        $customMessages = [
            'profile-image.required' => 'E\' richiesta una immagine per cambiare immagine di profilo.',
            'profile-image.image' => 'Devi inserire un immagine valida.',
            'profile-image.mimes' => 'L\'immagine inserita non ha un formato adatto.',
            'profile-image.dimensions' => 'L\'immagine deve essere minimo 160x160.',
        ];

        $validator = Validator::make(Input::all(), $rules, $customMessages);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'error' => $this->validationErrorsToString($validator->errors())]);
        }

        if ($request->hasFile('profile-image')) {
            $number = mt_rand(1,1000000);
            $image = $request->file('profile-image');
            $name = $user->username.'-'.Carbon::now()->toDateString().'-'.$number.'.'.$image->getClientOriginalExtension();
            $destinationPath = public_path('/uploads/profile');
            $imagePath = $destinationPath. "/".  $name;
            $image->move($destinationPath, $name);      

            $image = Imgur::setHeaders([
            'headers' => [
                'authorization' => 'Client-ID MY CLIENT ID',
                'content-type' => 'application/x-www-form-urlencoded',
            ]
            ])->setFormParams([
                'form_params' => [
                    'image' => URL::to("/").'/uploads/profile/'. $name,
                ]
            ])->upload(URL::to("/").'/uploads/profile/'. $name);

            \File::delete('uploads/profile/' .$name);


            $user->image_profile = $image->link();
            $user->save();
            $html =  $image->link();



            return response()->json(['success' => true, 'html' => $html, 'image' => $image->link()]);
        }
    }

Мой сервер работает под управлением Ubuntu 16.04 + Laravel 5.5

С наилучшими пожеланиями

1 Ответ

0 голосов
/ 16 июня 2019

Этот код будет загружать фото только в ваш локальный каталог.

public function imageProfile(Request $request)
    {
        $user = Auth::user();
        $rules = array(
            'profile-image' => 'required|image|mimes:jpeg,png,jpg,gif|max:8192|dimensions:min_width=160,min_height=160',
        );

        $customMessages = [
            'profile-image.required' => 'E\' richiesta una immagine per cambiare immagine di profilo.',
            'profile-image.image' => 'Devi inserire un immagine valida.',
            'profile-image.mimes' => 'L\'immagine inserita non ha un formato adatto.',
            'profile-image.dimensions' => 'L\'immagine deve essere minimo 160x160.',
        ];

        $validator = Validator::make(Input::all(), $rules, $customMessages);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'error' => $this->validationErrorsToString($validator->errors())]);
        }

        if ($request->hasFile('profile-image')) {
            $number = mt_rand(1,1000000);
            $image = $request->file('profile-image');
            $name = $user->username.'-'.Carbon::now()->toDateString().'-'.$number.'.'.$image->getClientOriginalExtension();
            $destinationPath = public_path('/uploads/profile');
            $imagePath = $destinationPath. "/".  $name;
            $image->move($destinationPath, $name);  

            // remove this commented portion
            // $image = Imgur::setHeaders([
            // 'headers' => [
            //     'authorization' => 'Client-ID MY CLIENT ID',
            //    'content-type' => 'application/x-www-form-urlencoded',
            // ]
            // ])->setFormParams([
            //     'form_params' => [
            //         'image' => URL::to("/").'/uploads/profile/'. $name,
            //     ]
            // ])->upload(URL::to("/").'/uploads/profile/'. $name);

            // \File::delete('uploads/profile/' .$name);


            // $user->image_profile = $image->link();
            // $user->save();
            // $html =  $image->link();

            // update this portion to

            $user->image_profile = $imagePath;
            $user->save();
            $html =  $imagePath;



            // return response()->json(['success' => true, 'html' => $html, 'image' => $image->link()]);
            // also update this portion to
            return response()->json(['success' => true, 'html' => $html, 'image' => $imagePath]);
        }
    }
...