morrelinko / laravel-simple-photo
此包已被弃用且不再维护。未建议替代包。
dev-master
2014-06-22 17:41 UTC
Requires
- php: >=5.3.0
- illuminate/config: 4.2.*
- illuminate/database: 4.2.*
- illuminate/support: 4.2.*
- morrelinko/simple-photo: ~0.7
This package is not auto-updated.
Last update: 2024-08-03 15:27:10 UTC
README
为 Laravel 4 简化的照片包装器。Simple photo 是一个库,可简化应用程序中照片的处理。
##安装
将 morrelinko/laravel-simple-photo 添加到 composer.json 的依赖项中
{
"require": {
"morrelinko/laravel-simple-photo": "dev-master"
}
}
之后,运行 composer update 更新依赖项或运行 composer install 安装它们。
通过编辑 app/config/app.php 配置文件添加服务提供者条目。
'providers' => array( // ... 'Morrelinko\LaravelSimplePhoto\SimplePhotoServiceProvider', );
在你的 'aliases' 中添加 SimplePhoto 门面条目
'aliases' => array( // ... 'SimplePhoto' => 'Morrelinko\LaravelSimplePhoto\SimplePhotoFacade' );
运行 php artisan config:publish morrelinko/laravel-simple-photo 复制默认配置文件。
将迁移从 `vendor/morrelinko/laravel-simple-photo/src/migrations` 复制到你的迁移目录。
然后运行 php artisan migrate 执行创建 'photos' 表的迁移。
SimplePhoto
// ... $source = new LaravelUploadSource($request->file('photo')); $photoId = SimplePhoto::upload($source); $user->photo_id = $photoId; $user->save();
并且为了稍后检索照片
$user = User::find(1); $photo = SimplePhoto::get($user->photo_id); echo $photo->url(); echo $photo->fileSize(); echo $photo->fileExtension();
SimplePhoto & Eloquent 模型
如果你使用的是 php 5.4+,你应该使用 SimplePhotoTrait。'使事情变得简单!!' (o_o )。
示例:
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'); } }
当然,这将执行的操作是为此模型添加一个 'photo' 属性。并且每次你尝试获取
此属性时,它将像上述示例一样检索照片。
该值是一组选项,特质使用这些选项构建照片,其中 'column' 是最重要的
因为它包含你想要检索的照片 ID 的字段。
使用类
$user = User::find(1); echo $user->photo->url(); echo $user->photo->fileSize(); echo $user->photo->fileExtension(); // .....
额外
如果你的实体有多个照片,你可以像这样指定所有照片:
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();
可以添加转换和回退等选项。
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();