khaleghi/media
Composer 安装命令:
composer require khaleghi/media
包简介
This will store files in server and save their attributes to database
README 文档
README
khaleghi/media-store is a Laravel package which created to provide an easy way
to store any kind of files on server storage and save their attributes in the database.
The other purpose of this package is to store images in a way that to be used in various responsive dimensions.
This package is supported and tested in Laravel 5.4 and above.
Requirements
PHP >= 5.4
Installation
Run the following command:
composer require khaleghi/media
Then run the following:
php artisan vendor:publish --provider="Khaleghi\Media\MediaServiceProvider"
php artisan migrate
You have to make a link between public folder and storage by running this command:
php artisan storage:link
Configuration
Set the property values in the config/media.php. These values will be used by media-store to make proper image sizes and customized upload folder.
Usage
Saving Images
Let's make a form in a html file:
<form action="{{route('media.store')}}" method="post" enctype="multipart/form-data"> @csrf <div class="input-group"> <span class="input-group-addon">Select your file:</span> <input type="file" id="media" name="photo" class="form-control" style="height:50px;padding:12px"> <span class="input-group-addon"> <button class="btn btn-default" type="submit">Upload</button> </span> </div> </form>
Then make a route like this:
Route::post('media', 'YourController@YourMethod')->name('media.store');
Now, All ever we have to do in our controller is:
use Khaleghi\Media\Medium; public function YourMethod(Request $request){ if($request->has('photo')){ $medium = Medium::create([ 'file' => $request->file('photo'), ]); return back()->with('photo_url', $medium->url()); } }
We can display the image like this:
<div> <img src="{{asset(session('photo_url')) }}" alt=""> </div>
Responsive images
By default, The image we store in a way like above will be saved in 4 paths:
storage\app\public\images\lg (Original Image size)
storage\app\public\images\md (540 x 540 px)
storage\app\public\images\sm (270 x 270 px)
storage\app\public\images\xs (135 x 135 px)
You can access url of each path by sending these parameters:
$medium->url('lg') //default
$medium->url('md') // medium size
$medium->url('sm') // small size
$medium->url('xs') // extra small size
Fill free to change default image sizes at config\media.php
Note:
khaleghi/media-storeuses Intervension Image package to change dimension of images. It will be installed on your project as a dependency.
Saving other file types
You can store all other file types in a way that mentioned before. They will save in these paths related to their mime types:
storage\app\public\applications
storage\app\public\auduis
storage\app\public\videos
storage\app\public\texts
...
Polymorphism
The Medium model is a polymorphic class. So you can make a relation between your other models to this model.
$medium = Medium::create([ 'file' => $request->file('name'), 'mediumable_type' => 'App\User', 'mediumable_id' => 10 ]);
Now you can access to user's file:
$user = App\User::find(10); $file = $user->media->first(); echo $file->url();
And reverse access:
echo $medium->mediumable->email;
Note: Don't forget to use morphMany in medium's related model (here: App\User).
public function media(){ return $this->morphMany(Medium::class, 'mediumable'); }
Determine before saving
You can make an instance of medium model and change it's attribute before save it:
$medium = new Medium(); $medium->attach_file($request->file('name')); $medium->description = "Occupational and educational information"; if($medium->size < 102400 and $medium->extension == "jpg"){ $medium->save(); }else{ $medium->detach(); return "Error"; }
Delete a medium
To delete a medium from Storage and Database you need to use remove method:
$medium->remove();
Database
These fields will be stored in database and you are able to access them through instance of Medium model:
| Field Name | Usage |
|---|---|
| stored_name | The name that file will store in storage folder |
| file_name | Original name of the file |
| caption | |
| mime | |
| size | Size of the file in bytes |
| width | Just for images |
| height | Just for images |
| mediumable_type | Name of model that this file is related to it |
| mediumable_id | model primary key that this file is related to it |
| position | Assigning a priority to each file with same manner of mediumable_type |
| manner | What this file is good for. Example: avatar |
| comments_count | |
| likes_count | |
| description |
License
Media-store is free software distributed under the terms of the MIT license.
khaleghi/media 适用场景与选型建议
khaleghi/media 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 khaleghi/media 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 khaleghi/media 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2019-01-25