Laravel 5 - blurry image after upload - intervention

Posted 4 years ago by IamCrazyD

I am making an eCommerce web portal. I am trying to upload the image in different resolutions, which is working completely fine.

But the problem is that, if I view it in the browser after uploading, I get to see the blurry image which is not what I want. I want that after the upload, the image should be clearly seen and it should not be blurry.

Here's the controller method for it:

public function store( AddProductRequest $request ) {
    if(\Auth::guest()) {
        return redirect('/admin');
    }

    $imageType = [
        'thumbnail' => [
            'height' => 75,
            'width' => 75
        ],
        'product' => [
            'height' => 450,
            'width' => 450
        ],
        'cart' => [
            'height' => 200,
            'width' => 200
        ]
    ];

    if ( $request->file( 'image_file' ) ) {
        $fileName = $request->file( 'image_file' )->getClientOriginalName();

        $fileName = explode( '.', $fileName );
        $image = \Image::make( $request->file( 'image_file' ) );
        $path = public_path( 'images/uploads/' );

        foreach ($imageType as $key => $value) {
            if ($key == 'thumbnail') {
                $image->resize( $value['width'], $value['height'], function( $constraint ) {
                    $constraint->aspectRatio();
                });
                $image->save( $path.$fileName[0]."-thumbnail-". $value['width'] ."-".$value['height'] .".jpg", 100 );
            } elseif ( $key == 'product' ) {
                $image->resize( $value['width'], $value['height'], function( $constraint ) {
                    $constraint->aspectRatio();
                });
                $image->save( $path.$fileName[0]."-product-". $value['width'] ."-".$value['height'] .".jpg", 100 );
            } elseif ( $key == 'cart' ) {
                $image->resize( $value['width'], $value['height'], function( $constraint ) {
                    $constraint->aspectRatio();
                });
                $image->save( $path.$fileName[0]."-cart-". $value['width'] ."-".$value['height'] .".jpg", 100 );
            }
        }
    }       

    $product = Product::create( $request->all() );

    $product->tags()->attach( $request->input('tags') );

    \Session::flash('product_added', 'Product has been successfully added in the database.');
    return redirect('admin/products');
}

How do I avoid displaying blurry image after upload ?

Kindly help me. Thanks in advance.

Please sign in or create an account to participate in this conversation.