Compress image when uploading with laravel

1

I wonder if anyone has any ideas on how to compress image when uploading, as the image is getting very large.

The routine I'm currently using is the one below:

public function providerUpdateDocuments() {
    $inputs = Input::all();
    $walker_id = Session::get('walker_id');


    foreach ($inputs as $key => $input) {
        $walker_document = WalkerDocument::where('walker_id', $walker_id)->where('document_id', $key)->first();
        if (!$walker_document) {
            $walker_document = new WalkerDocument;
        }
        $walker_document->walker_id = $walker_id;
        $walker_document->document_id = $key;

        if ($input) {
            $file_name = time();
            $file_name .= rand();
            $file_name = sha1($file_name);

            $ext = $input->getClientOriginalExtension();
            $input->move(public_path() . "/uploads", $file_name . "." . $ext);


            $local_url = $file_name . "." . $ext;

            // Upload to S3
            if (Config::get('app.s3_bucket') != "") {
                $s3 = App::make('aws')->get('s3');
                $pic = $s3->putObject(array(
                    'Bucket' => Config::get('app.s3_bucket'),
                    'Key' => $file_name,
                    'SourceFile' => public_path() . "/uploads/" . $local_url,
                ));

                $s3->putObjectAcl(array(
                    'Bucket' => Config::get('app.s3_bucket'),
                    'Key' => $file_name,
                    'ACL' => 'public-read'
                ));

                $s3_url = $s3->getObjectUrl(Config::get('app.s3_bucket'), $file_name);
            } else {
                $s3_url = asset_url() . '/uploads/' . $local_url;
            }




            $get = Walker::where('id', '=', $walker_id)->first();
            $pattern = "Olá, " . $get->first_name . ", ID " . $walker_id . " Documento enviado, aguarde a aprovação.";
            $subject = "Waiting for an Approval";
            /* email_notification('', 'admin', $pattern, $subject); */

            if (isset($walker_document->url)) {
                if ($walker_document->url != "") {
                    $icon = $walker_document->url;
                    unlink_image($icon);
                }
            }

            $walker_document->url = $s3_url;
            $walker_document->save();

            /* if ($walker_document->save()) {
              echo 'asdasd';
              } */
        }
    }

    $message = "Seus documentos foram atualizados com sucesso.";
    $type = "success";
    return Redirect::to('/provider/documents')->with('message', $message)->with('type', $type);
}

Does anyone have any idea what can be done to reduce the size of this image before uploading or even after uploading.

Thanks.

Hug.

    
asked by anonymous 02.04.2018 / 17:57

1 answer

0

I suggest using the Intervention Image package, which integrates with Laravel

A simple example uploading changes the image and saves it to disk:

<?php
    ...
    class MyController extends Controller

    public function uploadImage(Request $request) {
        // resizing an uploaded file
        Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');
    }
    
05.04.2018 / 01:40