-
- PHP 7.0 or higher
- Laravel 5.6.x or higher
-
Just run a :
composer require oza/user-images-manager
-
With Laravel 5.6.x, thanks to Laravel discovering system the ServiceProvider is automatically added to your Providers
The data is represented in an array containing two fields. A "current" field that contains the currently used image and another "other" field that contains all the images that have been used.
Eg:
array:2 [
"current" => {#601
+"id": "66a5ebbd-645b-4323-9792-281d6942d09c"
+"src": "https://lorempicsum.com/futurama/255/200/210"
+"set_at": "2018-04-29"
}
"others" => array:3 [
0 => {#606
+"id": "9cdfd187-470c-45f6-a558-387d3a8fe0e7"
+"src": "https://lorempicsum.com/futurama/255/200/2"
+"set_at": "2018-04-28"
}
1 => {#603
+"id": "eba74758-9721-44e8-8278-e09c237016f7"
+"src": "https://lorempicsum.com/futurama/255/200/5"
+"set_at": "2018-04-28"
}
]
]
-
- Just Add the
UsersImagesManager
trait to your Profile Model.
<?php 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"; }
- In your config folder, Open
Profile.php
and config your Profile table and options:
<?php 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", ];
-
After theses steps, you can call
AvatarManager 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:
<?php 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(); } }
-
Return an Instance of Avatar Class
$profile()->avatarManager();
Return an Instance of Cover Class
$profile()->coverManager();
-
Return a current image for a Manager
$profile->avatarManager()->current(); // return a string
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" ]
Return All images for a Manager
$profile->avatarManager()->all(); // return an array
Return an image with a specific id
$profile->avatarManager()->getById("cc5088ab-0a68-4459-890d-f949cfed235e"); // return an array
Return a collection of all images that have a specific source
$profile->avatarManager()->getBySrc("https://source.unsplash.com/random/400x400"); // return an array
Return all images that have been used before for a Manager
$profile->avatarManager()->others(); // return an array
Put an image to database: it take the image link (string)
$profile->avatarManager()->set("https://lorempicsum.com/random/100"); // return the current avatar
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 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
/*------------------------------------------------------- | First image |-------------------------------------------------------- | put the first image to database */ $profile->avatarManager()->set("https://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("https://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
You can use it to set a default Image when user is registered.
$profile->avatarManager()->setRandom(); // return the current avatar
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 changed otherwise 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
<?php /** * @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; }
Just create a class that extends to Manager Class and implements MethodsInterface
<?php 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 }
Then add it to UserImagesManager trait or create a new Trait and use it inside your profile model
/** * @return Manager * @throws NotAUserProfileModel */ public function myCustomManager() : Manager { $instance = new myCustomManager(); return $instance->setProfile($this); }
- Just Add the