Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Laravel package that help you manage your users images such as avatars and covers

NotificationsYou must be signed in to change notification settings

oza75/UserImagesManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  • Requirements

    • PHP 7.0 or higher
    • Laravel 5.6.x or higher
  • Installation

    Just run a :

    composer require oza/user-images-manager

  • With Laravel 5.6.x, thanks to Laravel discovering systemthe ServiceProvider is automatically added to your Providers

  • Architecture

The data is represented in an array containing two fields.A "current" field that contains the currently used imageand another "other" field that contains all the imagesthat have been used.

Eg:

  array:2 [    "current" => {#601      +"id": "66a5ebbd-645b-4323-9792-281d6942d09c"      +"src": "http://lorempicsum.com/futurama/255/200/210"      +"set_at": "2018-04-29"    }    "others" => array:3 [      0 => {#606        +"id": "9cdfd187-470c-45f6-a558-387d3a8fe0e7"        +"src": "http://lorempicsum.com/futurama/255/200/2"        +"set_at": "2018-04-28"      }      1 => {#603        +"id": "eba74758-9721-44e8-8278-e09c237016f7"        +"src": "http://lorempicsum.com/futurama/255/200/5"        +"set_at": "2018-04-28"      }        ]  ]
  • How to Use

    • Just Add theUsersImagesManager trait to your Profile Model.
    <?phpnamespaceApp;useIlluminate\Database\Eloquent\Model;useOza\UserImagesManager\Traits\UserImagesManager;/** * App\Profile * * @mixin \Eloquent */class Profileextends Model{use UserImagesManager;protected$guarded = [];protected$table ="profiles";}
    • In your config folder, OpenProfile.php and config your Profile table and options:
    <?phpreturn [/*     |--------------------------------------------------------------------------     | 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",     ];
    • After theses steps, you can callAvatarManager or CoverManager etc....

      • ‼️⚠️ 💥 The Profile Model must be a user's profile not only The Model Class,that means it must have a user_id fields etc...

    Eg:

    <?phpclass ProfileControllerextends \App\Http\Controllers\Controller  {publicfunctionindex(\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(); }}
    • Available Manager

      • Avatar Manager

      Return an Instance of Avatar Class

      $profile()->avatarManager();
      • Cover Manager

      Return an Instance of Cover Class

      $profile()->coverManager();
    • Available Methods for each Manager

      • Current

      Return a current image for a Manager

      $profile->avatarManager()->current();// return a string
      • Current In Array format

      Return an array for current image

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

      Output:

      array:3 [  "id" => "9da1cdb0-0ef6-4558-aa9c-116950e445d4"  "src" => "https://source.unsplash.com/random/400x400"  "set_at" => "2018-04-29"]
      • All

      Return All images for a Manager

      $profile->avatarManager()->all();// return an array
      • Get By Id

      Return an image with a specific id

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

      Return a collection of all images that have a specific source

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

      Return all images that have been used before for a Manager

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

      Put an image to database: it take the image link (string)

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

      Put an image that has already been used as an avatar, cover, etc.This can be useful if for example when the user wants to changehis avatar he is shown all the images that had chosenit before and he has the choice to put an old image or a new image

      /*-------------------------------------------------------  |  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
      • Set A Random Image

      You can use it to set a default Image when user is registered.

      $profile->avatarManager()->setRandom();// return the current avatar
      • Change or Update

      Changes the value of the collection whose id is passed in the parameters by the remplacement value passed.It takes a third parameter which is the field of ​​interest.if no value is specified then if the replacement value is an array,the entire collection will be replaced, else if the replacement value is a string,only the src attribute will be changedotherwise if it is not a string , nor an array, an exception will be thrown

      $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
      • All Methods are in MethodsInterface

      <?php/*** @author Aboubacar Ouattara <abouba181@gmail.com>* @license MIT*/namespaceOza\UserImagesManager\Interfaces;interface MethodsInterface{/**  * Get the current  object  *  * @return mixed  */publicfunctioncurrent():string;/**  * @return array  */publicfunctioncurrentInArray():array;/**  * Get all  *  * @return array  */publicfunctionall():array;/**  * Get Others  *  * @return array  *  */publicfunctionothers():array;/**  *  Fill All Array and return an instance  *  * @return MethodsInterface  */publicfunctiongetAll():MethodsInterface;/**  * @param string $id  * @return array|null  */publicfunctiongetById(string$id): ?array;/**  * @param string $id  * @return array|null  */publicfunctiongetBySrc(string$id): ?array;/**  * set to profile  *  * @param string $src  * @return string|null  */publicfunctionset(string$src):string;/**  * Set Random image  *  * @return string  */publicfunctionsetRandom():string;/**  * Set by an id  *  * @param string $id  * @return mixed  */publicfunctionsetById(string$id): ?string;/**  * @param string $id  * @return bool  */publicfunctionremove(string$id):bool;/**  * @return bool  */publicfunctionremoveAll():bool;/**  * @param string $id  * @param null|string $field  * @param $value  * @return bool  */publicfunctionchange(string$id, ?string$field=null ,$value) :bool;}
    • Add a Custom Manager

    Just create a class that extends to Manager Class and implements MethodsInterface

    <?phpclass MyCustomManagerextends \Oza\UserImagesManager\Managerimplements \Oza\UserImagesManager\Interfaces\MethodsInterface{use \Oza\UserImagesManager\Traits\Methods;private$field;private$defaultSize;/**     * My constructor     *     */publicfunction__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}

    Then add it to UserImagesManager trait or create a new Trait and use it inside your profile model

    /**     * @return Manager     * @throws NotAUserProfileModel     */publicfunctionmyCustomManager() :Manager    {$instance =newmyCustomManager();return$instance->setProfile($this);    }

About

Laravel package that help you manage your users images such as avatars and covers

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp