PHP code example of oza / user-images-manager

1. Go to this page and download the library: Download oza/user-images-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oza / user-images-manager example snippets


    
  
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use Oza\UserImagesManager\Traits\UserImagesManager;
    
    /**
     * App\Profile
     *
     * @mixin \Eloquent
     */
    class Profile extends Model
    {
        use UserImagesManager;
    
        protected $guarded = [];
        protected $table = "profiles";
    }

    

    
    
    return [
         /*
         |--------------------------------------------------------------------------
         | USER ID FIELD
         |--------------------------------------------------------------------------
         | this value is the field that contains the user_id
         |
         */
       
         "user_id_field" =>  'user_id',
     
         /*
         |--------------------------------------------------------------------------
         | AVATAR FIELD
         |--------------------------------------------------------------------------
         | this value represent the field that contains avatars json
         |
         */
       
         "avatars_field" => "avatars",
       
         /*
         |--------------------------------------------------------------------------
         | AVATAR FIELD
         |--------------------------------------------------------------------------
         | this value represent the field that contains covers json
         |
         */
       
         "covers_field" => "covers",
       
         /*
         |--------------------------------------------------------------------------
         | Default Images domain
         |--------------------------------------------------------------------------
         | The default images api domain
         |
         */
       
         "default_images_domain" => "https://source.unsplash.com/",
     
         /*
         |--------------------------------------------------------------------------
         | Default Avatar Size
         |--------------------------------------------------------------------------
         | The default avatars size
         |
         */
           
         "default_avatars_size" => "400x400",
       
         /*
         |--------------------------------------------------------------------------
         | Default Cover Size
         |--------------------------------------------------------------------------
         | The default cover size
         |
         */
       
         "default_cover_size" => "1000x800",
     

        
    ];
    

     
    
    class ProfileController extends \App\Http\Controllers\Controller 
     {
      
       public function index(\Illuminate\Http\Request $request) {  
         
         /*-------------------------------------------------------
          | Retrieve User and Profile
          |--------------------------------------------------------
          | retrieve the user and with Relationships get his profile
          | 
          |
          */
       
         $user = App\User::find($request->get('user_id'));
         $profile = $user->profile();
          
          // Then you can get avatarManager Like this
          $manager = $profile->avatarManager(); // return an instance of Avatar
          $manager->getAvatar(); // return the current Avatar
          
          // Or get CoverManager
          $coverManager = $profile->coverManager();
          $cover = $coverManager->getCover();
     }
    }
    
 
           $profile()->avatarManager(); 
        
 
           $profile()->coverManager(); 
         

          $profile->avatarManager()->current(); // return a string
        

          $profile->avatarManager()->currentInArray(); // return an array
          
        

          $profile->avatarManager()->all(); // return an array
        

          $profile->avatarManager()->getById("cc5088ab-0a68-4459-890d-f949cfed235e"); // return an array
         

          $profile->avatarManager()->getBySrc("https://source.unsplash.com/random/400x400"); // return an array
        

          $profile->avatarManager()->others(); // return an array
        

          $profile->avatarManager()->set("http://lorempicsum.com/random/100"); // return the current avatar
        

          /*-------------------------------------------------------
          |  First image
          |--------------------------------------------------------
          | put the first image to database
          */
          
          $profile->avatarManager()->set("http://lorempicsum.com/random/100");
          
          // After that, we retrieve the current image id
          $id = $profile->avatarManager()->currentInArray()['id'];
          
          /*-------------------------------------------------------
          | AND WE PUT ANOTHER IMAGE
          |--------------------------------------------------------
          |
          */
          
          $profile->avatarManager()->set("http://loremimage.com/80");
          
          /*-------------------------------------------------------
          | PUTTING OLD IMAGE AS THE CURRENT AVATAR
          |--------------------------------------------------------
          | After that, the current avatar is the last record.
          | with $id above, I can now tell him to put the image corresponding
          |  to this identifier as the current avatar.
          |  This can be useful if for example when the user wants to 
          |  change his avatar he is shown all the images that
          |  had chosen it before and he has the choice 
          |  to put an old image or a new image
          */
          
          $profile->avatarManager()->setById($id); // return the current avatar
        

          $profile->avatarManager()->setRandom(); // return the current avatar
         

          $profile->avatarManager()->change('cc5088ab-0a68-4459-890d-f949cfed235e', 'https://source.unsplash.com/random/400x400'); // change the image src
          $profile->avatarManager()->change('cc5088ab-0a68-4459-890d-f949cfed235e', now()->addDay(2)->format('Y-m-d'), 'set_at'); // change set_at value
          $profile->avatarManager()->change('cc5088ab-0a68-4459-890d-f949cfed235e', 'yes', 'archived'); // add a new field to collection
         

      
      /**
       * @author Aboubacar Ouattara <[email protected]>
       * @license MIT   
       */
      
      namespace Oza\UserImagesManager\Interfaces;
      
      
      interface MethodsInterface
      {
          /**
           * Get the current  object
           *
           * @return mixed
           */
          public function current(): string;
      
          /**
           * @return array
           */
          public function currentInArray(): array;
      
          /**
           * Get all
           *
           * @return array
           */
          public function all(): array;
      
          /**
           * Get Others
           *
           * @return array
           *
           */
          public function others(): array;
      
          /**
           *  Fill All Array and return an instance
           *
           * @return MethodsInterface
           */
          public function getAll(): MethodsInterface;
      
          /**
           * @param string $id
           * @return array|null
           */
          public function getById(string $id): ?array;
      
          /**
           * @param string $id
           * @return array|null
           */
          public function getBySrc(string $id): ?array;
      
          /**
           * set to profile
           *
           * @param string $src
           * @return string|null
           */
          public function set(string $src): string;
      
          /**
           * Set Random image
           *
           * @return string
           */
          public function setRandom(): string;
      
          /**
           * Set by an id
           *
           * @param string $id
           * @return mixed
           */
          public function setById(string $id): ?string;
      
          /**
           * @param string $id
           * @return bool
           */
          public function remove(string $id): bool;
      
          /**
           * @return bool
           */
          public function removeAll(): bool;
      
          /**
           * @param string $id
           * @param null|string $field
           * @param $value
           * @return bool
           */
          public function change(string $id, ?string $field= null , $value) : bool;
      }
         

     
  
    class MyCustomManager extends \Oza\UserImagesManager\Manager implements \Oza\UserImagesManager\Interfaces\MethodsInterface
    {
      
      use \Oza\UserImagesManager\Traits\Methods;
      private $field;
      private $defaultSize;
    
        /**
         * My constructor
         *
         */  
      public function __construct()
      {
            parent::__construct();
            $this->field = config("profile.YOUR_FIELD_IN_PROFILE_TABLE") ?? 'YOUR_FIELD_IN_PROFILE_TABLE';
            $this->defaultSize = config("profile.YOUR_DEFAULT_IMAGE_SIZE") ?? '400x400';  
      }
      
      // Add your methods here
    
    }
    

       /**
         * @return Manager
         * @throws NotAUserProfileModel
         */
        public function myCustomManager() : Manager
        {
            $instance = new myCustomManager();
            return $instance->setProfile($this);
        }