Compress image when uploading with Expandable

Asked

Viewed 1,108 times

1

I wonder if anyone has any idea how to proceed to compress image while uploading, as the image is getting too 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.

  • You can convert to webp; Use the Imagemagick and reduce some quality; or you can use software such as jpegoptim, optipng and pngquant.

1 answer

1

I suggest using the package Intervention Image, that has integration with Laravel

A simple example that upload 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');
    }
  • this function resize() modifies the file in pixels or bytes?

  • 1

    as described in the manual http://image.intervention.io/api/resize, the parameters are width and height, (width and alt), that is in pixels ... logically the final size will also change

Browser other questions tagged

You are not signed in. Login or sign up in order to post.