oza / user-images-manager
一个帮助您管理用户图像的包,例如头像或封面
1.0.1
2018-05-10 05:45 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^7.1
This package is auto-updated.
Last update: 2024-09-04 09:21:16 UTC
README
-
需求
- PHP 7.0 或更高版本
- Laravel 5.6.x 或更高版本
-
安装
只需运行一个
composer require oza/user-images-manager
-
在 Laravel 5.6.x 中,由于 Laravel 发现系统,ServiceProvider 将自动添加到您的 Providers
-
架构
数据以包含两个字段的数组表示。一个 "current" 字段,其中包含当前使用的图像,另一个 "other" 字段,其中包含所有已使用的图像。
例如:
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"
}
]
]
-
如何使用
- 只需将
UsersImagesManager
特性添加到您的 Profile 模型。
<?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"; }
- 在您的配置文件夹中,打开
Profile.php
并配置您的 Profile 表和选项
<?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", ];
-
完成这些步骤后,您就可以调用
AvatarManager 或 CoverManager 等...
。- ‼️ ⚠️ 💥 Profile 模型必须是一个用户的配置文件,而不仅仅是模型类,这意味着它必须具有 user_id 字段等...
例如
<?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(); } }
-
可用管理器
返回头像类的实例
$profile()->avatarManager();
返回封面类的实例
$profile()->coverManager();
-
每个管理器的可用方法
为管理器返回当前图像
$profile->avatarManager()->current(); // return a string
返回当前图像的数组
$profile->avatarManager()->currentInArray(); // return an array
输出
array:3 [ "id" => "9da1cdb0-0ef6-4558-aa9c-116950e445d4" "src" => "https://source.unsplash.com/random/400x400" "set_at" => "2018-04-29" ]
为管理器返回所有图像
$profile->avatarManager()->all(); // return an array
返回具有特定 id 的图像
$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
更改通过参数传递的具有指定 id 的集合的值。它接受第三个参数,即感兴趣的字段。如果没有指定值,则如果替换值是数组,则整个集合将被替换;否则,如果替换值是字符串,则仅更改 src 属性;如果它既不是字符串也不是数组,则抛出异常
$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; }
-
添加自定义管理器
只需创建一个扩展到 Manager 类并实现 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 }
然后将其添加到 UserImagesManager 特性或创建一个新的特性并在您的配置文件中使用它
/** * @return Manager * @throws NotAUserProfileModel */ public function myCustomManager() : Manager { $instance = new myCustomManager(); return $instance->setProfile($this); }
- 只需将