2017 © Pedro Peláez
 

library laravel-simple-photo

image

morrelinko/laravel-simple-photo

  • Sunday, June 22, 2014
  • by morrelinko
  • Repository
  • 1 Watchers
  • 6 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Simple Photo

Simple Photo Wrapper for Laravel 4. Simple photo is a library that simplifies handling of photos in your application., (*1)

Installation

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)

SimplePhoto

// ...

$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();

SimplePhoto & Eloquent Models

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)

Using the Class


$user = User::find(1); echo $user->photo->url(); echo $user->photo->fileSize(); echo $user->photo->fileExtension(); // .....

Extra

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();

The Versions