oza/user-images-manager
Composer 安装命令:
composer require oza/user-images-manager
包简介
A package to help you manage your users images such as avatars or covers
README 文档
README
-
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 system the 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 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": "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 the
UsersImagesManagertrait 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.phpand 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(); } }
-
Available Manager
Return an Instance of Avatar Class
$profile()->avatarManager();
Return an Instance of Cover Class
$profile()->coverManager();
-
Available Methods for each Manager
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("http://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("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
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 <abouba181@gmail.com> * @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; }
-
Add a Custom Manager
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
oza/user-images-manager 适用场景与选型建议
oza/user-images-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「images」 「Users」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oza/user-images-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oza/user-images-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oza/user-images-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova field for distribute your images as array of object.
Pexels API Client for www.pexels.com
Debugging a problem and need to login as one of your customers? This allows you to authenticate as any of your customers.
A trait for Laravel models which have many users with roles.
The library provides a flexible way to add role-based access control management to CakePHP 3.x
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-28