Sometimes, we may require to make grayscale image when user upload image. grayscale image looks better if you have an good layout website. in article we will learn how to create grayscale image using intervention package in laravel. intervention package provide a lots of things to do with image like resize, Grayscale, write on image etc. intervention package is also perfect to generate grayscale image.
So, let's follow few step to generate grayscale image and upload it to our server. You can make grayscale image as like bellow preview, i added one originally image and another grayscale image looks.
Original Image:
Grayscale Image:
Lets Start,
First install intervention/image for generate grayscale image. this package through we can generate grayscale image for our project. so first fire bellow command in your cmd or terminal:
composer require intervention/image
Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.
config/app.php
return [$provides => ['Intervention\Image\ImageServiceProvider'],$aliases => ['Image' => 'Intervention\Image\Facades\Image']]
app/Http/Controllers/ImageController.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Http\Requests;use Image;class ImageController extends Controller{public function grayscaleImagePost(Request $request){$this->validate($request, ['title' => 'required','image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);$image = $request->file('image');$input['imagename'] = time().'.'.$image->getClientOriginalExtension();$destinationPath = public_path('/images');$img = Image::make($image->getRealPath());$img->greyscale()->save($destinationPath.'/'.$input['imagename']);/*Write Your Insert data code*/return back()->with('success','Image Upload successful')->with('imageName',$input['imagename']);}}
Step 5: Blade and Create Upload directory
Ok, in this last step we will create grayscaleImage.blade.php file for photo upload form and manage error message and also success message. So first create grayscaleImage.blade.php file and put bellow code:
resources/views/grayscaleImage.blade.php
@extends('layouts.app')@section('content')<div class="container"><h1>Grayscale Image Uploading Demo</h1>@if (count($errors) > 0)<div class="alert alert-danger"><strong>Whoops!</strong> There were some problems with your input.<br><br><ul>@foreach ($errors->all() as $error)<li>{{ $error }}</li>@endforeach</ul></div>@endif@if ($message = Session::get('success'))<div class="alert alert-success alert-block"><button type="button" class="close" data-dismiss="alert">×</button><strong>{{ $message }}</strong></div><div class="row"><div class="col-md-4"><strong>Grayscale Image:</strong><br/><img src="/images/{{ Session::get('imageName') }}" width="500px" /></div></div>@endif{!! Form::open(array('route' => 'grayscaleImagePost','enctype' => 'multipart/form-data')) !!}<div class="row"><div class="col-md-4"><strong>Add Title:</strong>{!! Form::text('title', null,array('class' => 'form-control','placeholder'=>'Add Title')) !!}</div><div class="col-md-12"><strong>Add Image:</strong>{!! Form::file('image', array('class' => 'image')) !!}</div><div class="col-md-12"><br/><button type="submit" class="btn btn-success">Upload Image</button></div></div>{!! Form::close() !!}</div>@endsection
Ok, at last create directory in your public folder images and please give permission to that folder and check....
Goyou Casino Resort (Las Vegas) - Get a Hotel Deal on the Go
ReplyDeleteGoyou Hotel & 포커 족보 Casino offers 먹튀 true Las Vegas style gaming. Play all your favourite 메이플 캐릭터 슬롯 slot 다파벳 machines and table games. Choose from sporting100 a variety of gaming machines