PHP code example of felipeweb11 / laravel-easymutators
1. Go to this page and download the library: Download felipeweb11/laravel-easymutators 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/ */
felipeweb11 / laravel-easymutators example snippets
return [
/*
|--------------------------------------------------------------------------
| Filesystem Storage Disk
|--------------------------------------------------------------------------
|
| The filesystems on which to store added media. Choose one of the
| filesystems you configured in app/config/filesystems.php
|
*/
'storage_disk' => env('EASYMUTATORS_STORAGE_DISK', 'easymutators'),
/*
|--------------------------------------------------------------------------
| Default image path generator
|--------------------------------------------------------------------------
|
| Default path generator class, used to generate the of files based on
| mapping.
|
*/
'default_path_generator' => \Webeleven\EasyMutators\Upload\DefaultPathGenerator::class,
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| This package uses Intervention Image internally that supports "GD Library"
| and "Imagick" to process images. You may choose one of them according to
| your PHP configuration. By default PHP's "GD Library" implementation is
| used.
|
| Supported: "gd", "imagick"
|
*/
'image_driver' => 'gd',
/*
|--------------------------------------------------------------------------
| Delete old media
|--------------------------------------------------------------------------
|
| Determine if old media should be deleted from filesystem.
|
| Options: false, "on-set", "on-save"
|
*/
'delete_old_media' => 'on-save',
/*
|--------------------------------------------------------------------------
| Default objects
|--------------------------------------------------------------------------
|
| Default value objects classes, this is an alias array used to map to value
| object class.
|
*/
'objects' => [
'file' => \Webeleven\EasyMutators\ValueObjects\File::class,
'image' => \Webeleven\EasyMutators\ValueObjects\Image::class,
'settings' => \Webeleven\EasyMutators\ValueObjects\Settings::class
],
];
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
//If you are using MySQL 5.7 you can use JSON column type instead of text
$table->text('profile_photo')->nullable()->default(null);
$table->rememberToken();
$table->timestamps();
});
}
...
}
...
use Webeleven\EasyMutators\EasyMutatorsTrait;
class User extends Model
{
use EasyMutatorsTrait;
// Here you should set the attribute name and type.
// You can use predefined types of easymutators.php config file
// or create your own value objects that should implements
// Webeleven\EloquentValueObject\ValueObjectInterface
protected $mutations = [
'profile_photo' => 'image'
];
// Add field to fillable
protected $fillable = [
...
'profile_photo',
];
}
$user = User::find(1);
$user->profile_photo = $request->file('photo');
//Or directly from an URL
$user->profile_photo = 'http://anyresourceurl/some_image.jpg';
//And save the user in database
$user->save();
...
use Webeleven\EasyMutators\EasyMutatorsTrait;
use Webeleven\EasyMutators\Mapping\MediaMapper;
class User extends Model
{
use EasyMutatorsTrait;
protected $mutations = [
'profile_photo' => 'image'
];
protected function mapMedia(MediaMapper $mapper)
{
$profilePhoto = $mapper->image('profile_photo')
->width(900);
$profilePhoto
->addConversion('medium')
->width(400);
$profilePhoto
->addConversion('small')
->width(50)
->quality(70);
$profilePhoto
->addConversion('other')
->name('otherFileName')
->width(100)
->height(50)
->generatePathWith('App\MyOwnPathGenerator::class') // Implements Webeleven\EasyMutators\Upload\PathGenerator
->dontKeepAspectRatio();
}
}
echo $user->profile_photo->medium->url; //Returns url of medium image
echo $user->profile_photo->medium->width; //Get width of medium image
echo $user->profile_photo->medium->height; //Get height of medium image
echo $user->profile_photo->small->url; //Returns url of small image
echo $user->profile_photo->small->width; //Get width of small image
echo $user->profile_photo->small->height; //Get height of small image
//You can also access other attributes like:
echo $user->profile_photo->small->filename; //Returns filename of image
echo $user->profile_photo->small->name; //Returns name of image
echo $user->profile_photo->small->path; //Returns path of image
echo $user->profile_photo->small->size; //Returns size of image
...
use Webeleven\EasyMutators\EasyMutatorsTrait;
use Webeleven\EasyMutators\Mapping\MediaMapper;
class User extends Model
{
use EasyMutatorsTrait;
protected $mutations = [
'profile_photo' => 'image'
];
protected function mapMedia(MediaMapper $mapper)
{
...
//Custom base upload dir for this model
$mapper->baseUploadDir('nameOfDirectory/' + $this->name);
...
}
}
interface ValueObjectInterface extends Arrayable
{
/**
* @param $value
*/
public function __construct($value); // Implement logic do construct your value object
/**
* @return mixed
*/
public function toScalar(); // Convert value object to scalar format
/**
* @return string
*/
public function __toString(); // Convert the value objects to database format (string or json string)
/**
* @param array $args
* @return ValueObjectInterface
*/
public static function make(); // Used to create a new value object instance
}
...
use Webeleven\EasyMutators\EasyMutatorsTrait;
class User extends Model
{
use EasyMutatorsTrait;
protected $mutations = [
'my_attribute' => MyCustomValueObject::class
];
// Add field to fillable
protected $fillable = [
...
'my_attribute',
];
}