dev-master
9999999-dev
MIT
The Requires
- php >=5.3.0
- illuminate/support 4.2.*
- illuminate/database 4.2.*
- illuminate/config 4.2.*
- morrelinko/simple-photo ~0.7
Wallogit.com
2017 © Pedro Peláez
Simple Photo Wrapper for Laravel 4. Simple photo is a library that simplifies handling of photos in your application., (*1)
Add morrelinko/laravel-simple-photo to your composer.json dependencies:, (*2)
{
"require": {
"morrelinko/laravel-simple-photo": "dev-master"
}
}
Afterwards, run composer update to update your dependencies or composer install to install them., (*3)
Add an entry for the service provider by editing app/config/app.php config file., (*4)
'providers' => array(
// ...
'Morrelinko\LaravelSimplePhoto\SimplePhotoServiceProvider',
);
Add an entry in your 'aliases' for the SimplePhoto facade, (*5)
'aliases' => array(
// ...
'SimplePhoto' => 'Morrelinko\LaravelSimplePhoto\SimplePhotoFacade'
);
Run php artisan config:publish morrelinko/laravel-simple-photo to copy the default config file., (*6)
Copy the migration from `vendor/morrelinko/laravel-simple-photo/src/migrations to your migrations directory., (*7)
And run php artisan migrate to run the migration that will create the 'photos' table., (*8)
// ...
$source = new LaravelUploadSource($request->file('photo'));
$photoId = SimplePhoto::upload($source);
$user->photo_id = $photoId;
$user->save();
And to retrieve the photo later, (*9)
$user = User::find(1); $photo = SimplePhoto::get($user->photo_id); echo $photo->url(); echo $photo->fileSize(); echo $photo->fileExtension();
If you are using php 5.4+, you should use the SimplePhotoTrait. 'Makes things easy!!' (o_o )., (*10)
Example:::, (*11)
use Morrelinko\LaravelSimplePhoto\SimplePhotoTrait; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $photos = array( 'photo' => array('column' => 'photo_id') ); use SimplePhotoTrait; public function photo() { return $this->belongsTo('Morrelinko\LaravelSimplePhoto\Photo', 'photo_id', 'id'); } }
Of course, what this will do is that it will add a 'photo' property to this model. And whenever you try to get, (*12)
this property, it will retrieve the photo just the same way as the above example shows., (*13)
The value is a set of options which the trait uses to build the photo of which the 'column' is the most important, (*14)
as it is the field which contains the ID of the photo you wish to retrieve., (*15)
$user = User::find(1); echo $user->photo->url(); echo $user->photo->fileSize(); echo $user->photo->fileExtension(); // .....
If your entity has more than one photo, you can specify all of em like so:::, (*16)
use Morrelinko\LaravelSimplePhoto\SimplePhotoTrait; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $photos = array( 'photo' => array('column' => 'photo_id'), 'coverPhoto' => array('column' => 'cover_photo_id'), 'profileBg' => array('column' => 'profile_bg_id') ); use SimplePhotoTrait; public function photo() { return $this->belongsTo('Morrelinko\LaravelSimplePhoto\Photo', 'photo_id', 'id'); } public function coverPhoto() { return $this->belongsTo('Morrelinko\LaravelSimplePhoto\Photo', 'cover_photo_id', 'id'); } public function profileBg() { return $this->belongsTo('Morrelinko\LaravelSimplePhoto\Photo', 'profile_bg_id', 'id'); } } $user = User::find(1); echo $user->photo->url(); echo $user->coverPhoto->url(); echo $user->profileBg->url();
Options such as transformations and fallback can be added., (*17)
use Morrelinko\LaravelSimplePhoto\SimplePhotoTrait; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $photos = array( 'photo' => array( 'column' => 'photo_id', 'fallback' => 'user_not_found.png', 'transform' => array( 'resize' => array(100, 100) 'rotate' => array(180) )), 'coverPhoto' => array( 'column' => 'cover_photo_id', 'fallback' => 'cover_default.png' ), ); use SimplePhotoTrait; } $user = User::find(1); echo $user->photo->url(); echo $user->coverPhoto->url();
MIT