Saturday, March 23, 2019

The Image Library for Laravel: Intervention

The Image Library for Laravel: Intervention

Intervention Image is an open source PHP image handling and manipulation library. It provides an easy way to manipulate images. In addition, it integrates tightly with Laravel.

Installation

  1. Install package
> composer require intervention/image
  1. Integration with Laravel: becoming a Service Provider

Add the following lines into Laravel config file config/app.php. it will be added into the $providers array, which is the service providers for this package.

config/app.php

'providers' => [
      :
      :
   /*
    * Package Service Providers...
    */    
   Intervention\Image\ImageServiceProvider::class,
],
  1. Add the facade of this package to the $aliases array.

config/app.php

'aliases' => [
      :
      :
  'Image' => Intervention\Image\Facades\Image::class,
],  

Now the Image Class will be auto-loaded by Laravel.

Usage

  • View file:

app/resources/views/products/edit.blade.php

        :
<form method="post" action="{{ route('products.update', $product->id) }}" enctype="multipart/form-data">
  @method('PATCH')
  @csrf
	:
  <!-- upload pictures -->
  <div id="upload_pictures">
    @foreach ($images as $image)
      <div class="form-group">
        <div class="col-md-10">
          <input type="file" class="form-control-file" name="prod_img[]" id="prod_img[]">
        </div>
      </div>
    @endforeach
  </div>
  <!-- upload pictures -->

  <a class="btn btn-secondary" href="{{ URL::previous() }}">Back</a>
  <button type="submit" class="btn btn-primary">Update</button>
</form>
  • Controller:

app/Http/Controllers/ProductController.php

use Image;
    :
    :
  public function update(Request $request, $id)
  {
            :
     // Save upload files
     $images = $request->file('prod_img');
     foreach ($images as $key => $image) {
       $img = Image::make($image);
       $img->fit(600,600);
       $img->save(public_path('/prod_imgs').'/'.$id.'_'.$key.'.png');
     }
            :
  }

Thursday, March 21, 2019

My Cryptocurrency: 1. Create A New Wallet

Here is how to create a new wallet for my cryptocurrency at MyEtherWallet.

1. Go to MyEtherWallet web site.
2. Enter a password with at least 9 characters.
3. Press the button "Create New Wallet".


4. After few moment, you will get an address number and a private key.
Remember to save them in a secure place.

If you need to view your wallet information next time, just go to the "View Wallet Info" page and paste your private key.  The "unlock" button will show up after private key is entered. Press it to unlock your wallet information.

This is a easy step, although MyEtherWallet does not recommend. 

5. It shows the wallet info.


6. If there is a custom token which is not recognized by MyEtherWallet, you can add it by yourself.
 

7. Fill up the columns described below and it's done.


8. The wallet has been created! 

Thursday, March 7, 2019

How To Enable Email Verification Functionality in Laravel 5.7

How To Enable Email Verification Functionality in Laravel 5.7

The way to enable email verification functionality in Laravel (>= 5.7) is as follows:

1. Model User preparation

Implement the MustVerifyEmail contract.

app/User.php

class User extends Authenticatable 

change to:

class User extends Authenticatable implements MustVerifyEmail

2. Database table field

Table users must contain an email_varified_at column.

3. Routing

Add verify option to the Auth::routes method in order to activate the Auth\VerificationController.

app/routes/web.php

auth::routes(['verify' => true]);

4. .env configuration

Check the .env file configuration.

5. Default mail host: using Mailtrap.io as mail host

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=xxx
MAIL_PASSWORD=xxx
MAIL_ENCRYPTION=null

Option 1: using Google Mail SMTP server as mail host:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xxxxxxx@gmail.com
MAIL_PASSWORD=xxxxxxxxxx
MAIL_ENCRYPTION=tls

Note: The “low security application access” of your Google account must be enable.
And Google will send you a notice and suggest not to enable it for security reason.

Option 2: using Mailgun

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=xxxxxxx@gmail.com
MAIL_PASSWORD=xxxxxxxxx
MAIL_ENCRYPTION=null

Note: Need to join a pay plan.

6. Views

Make sure app/resources/views/auth/verify.blade.php exists.

7. Where to Redirect after verifying Emails

We can modify the route in VerificationController.

app/Http/Controllers/Auth/VerificationController.php

protected $redirectTo = '/home';