Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active October 1, 2023 15:47
Show Gist options
  • Save nasrulhazim/f3ca4c231216a491b423e06e069473ae to your computer and use it in GitHub Desktop.
Save nasrulhazim/f3ca4c231216a491b423e06e069473ae to your computer and use it in GitHub Desktop.

Revisions

  1. nasrulhazim revised this gist Jan 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AppServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ public function boot()

    Validator::extend('base64image', function ($attribute, $value, $parameters, $validator) {
    $explode = explode(',', $value);
    $allow = ['png', 'jpg', 'png'];
    $allow = ['png', 'jpg', 'svg'];
    $format = str_replace(
    [
    'data:image/',
  2. nasrulhazim created this gist Jan 5, 2017.
    66 changes: 66 additions & 0 deletions AppServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    <?php

    namespace App\Providers;

    use Illuminate\Support\Facades\Validator;
    use Illuminate\Support\ServiceProvider;

    class AppServiceProvider extends ServiceProvider
    {
    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
    Validator::extend('base64', function ($attribute, $value, $parameters, $validator) {
    if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $value)) {
    return true;
    } else {
    return false;
    }
    });

    Validator::extend('base64image', function ($attribute, $value, $parameters, $validator) {
    $explode = explode(',', $value);
    $allow = ['png', 'jpg', 'png'];
    $format = str_replace(
    [
    'data:image/',
    ';',
    'base64',
    ],
    [
    '', '', '',
    ],
    $explode[0]
    );

    // check file format
    if (!in_array($format, $allow)) {
    return false;
    }

    // check base64 format
    if (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $explode[1])) {
    return false;
    }

    return true;
    });

    }

    /**
    * Register any application services.
    *
    * @return void
    */
    public function register()
    {
    if ($this->app->environment() == 'local') {
    $this->app->register(\MaddHatter\ViewGenerator\ServiceProvider::class);
    }
    }
    }