├── .travis.yml
├── LICENSE
├── composer.json
├── phpunit.xml
├── readme.md
├── src
├── MediaUploadController.php
├── MediaUploadServiceProvider.php
└── config
│ └── config.php
└── tests
└── .gitkeep
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 5.4
5 | - 5.5
6 | - 5.6
7 | - hhvm
8 |
9 | before_script:
10 | - travis_retry composer self-update
11 | - travis_retry composer install --prefer-source --no-interaction --dev
12 |
13 | script: phpunit
14 |
15 | branches:
16 | only:
17 | - master
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (C) 2015 Trias Nur Rahman
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of
4 | this software and associated documentation files (the "Software"), to deal in
5 | the Software without restriction, including without limitation the rights to
6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 | of the Software, and to permit persons to whom the Software is furnished to do
8 | so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "triasrahman/media-upload",
3 | "description": "Simple and easy AJAX media upload for Laravel",
4 | "keywords": ["ajax", "media", "upload", "file", "image", "laravel"],
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "Trias Nur Rahman",
9 | "email": "contact@triasrahman.com"
10 | }
11 | ],
12 | "require": {
13 | "php": ">=5.4.0",
14 | "illuminate/support": "5.*",
15 | "intervention/image": "~2.0"
16 | },
17 | "autoload": {
18 | "psr-4": {
19 | "Triasrahman\\MediaUpload\\": "src/"
20 | }
21 | },
22 | "minimum-stability": "dev"
23 | }
24 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 | ./tests/
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Simple and Easy AJAX Media Upload for Laravel
2 | [](https://travis-ci.org/triasrahman/laravel-media-upload)
3 | [](https://packagist.org/packages/triasrahman/media-upload)
4 | [](https://packagist.org/packages/triasrahman/media-upload)
5 | [](https://packagist.org/packages/triasrahman/media-upload)
6 |
7 | > **This is a package to make AJAX upload process easy and simple! Enhance your customer experience and improve your system performance.**
8 |
9 | ###[SEE DEMO](http://laravel-media-upload.triasrahman.com)
10 |
11 | ## Requirements
12 |
13 | - PHP >=5.3
14 | - [Intervention Image](https://github.com/Intervention/image) (for image processing)
15 |
16 | ## Installation
17 |
18 | Require this package with composer:
19 |
20 | ```
21 | composer require triasrahman/media-upload
22 | ```
23 |
24 | After updating composer, add the ServiceProvider to the providers array in config/app.php
25 |
26 | ```
27 | 'providers' => [
28 | // ...
29 | 'Triasrahman\MediaUpload\MediaUploadServiceProvider',
30 | ]
31 | ```
32 |
33 | Copy the package config to your local config with the publish command:
34 |
35 | ```
36 | php artisan vendor:publish
37 | ```
38 |
39 | ## Usage
40 |
41 | Your HTML form
42 | ```html
43 |
44 |
45 |
46 |
47 | ```
48 |
49 | Using jQuery AJAX
50 |
51 | ```javascript
52 |
53 | $('input[name=file]').change(function()
54 | {
55 | // AJAX Request
56 | $.post( '/media-upload', {file: $(this).val()} )
57 | .done(function( data )
58 | {
59 | if(data.error)
60 | {
61 | // Log the error
62 | console.log(error);
63 | }
64 | else
65 | {
66 | // Change the image attribute
67 | $( 'img.preview' ).attr( 'src', data.path );
68 | }
69 | });
70 | });
71 |
72 | ```
73 |
74 | ## Configurations
75 |
76 | Edit ``media-upload.php`` for more configurations.
77 |
78 | ```php
79 | /*
80 | |--------------------------------------------------------------------------
81 | | Upload Types
82 | |--------------------------------------------------------------------------
83 | |
84 | | It's the flexibility of this package. You can define the type of upload
85 | | file methods. For example, you want to upload for profile picture,
86 | | article post, background, etc. Here is
87 | |
88 | */
89 |
90 | 'types' => [
91 | // ... put your custom type ...
92 |
93 | 'profile' => [
94 | 'middleware' => 'auth',
95 | 'format' => 'image',
96 | 'image' => [
97 | 'fit' => [400, 400],
98 | 'thumbs' => [
99 | 'small' => [50, 50],
100 | 'medium' => [100, 100],
101 | ]
102 | ],
103 | 'save_original' => true,
104 | ],
105 |
106 | 'profile-cover' => [
107 | 'middleware' => 'auth',
108 | 'format' => 'image',
109 | 'image' => [
110 | 'fit' => [1200, 400],
111 | ],
112 | 'multiple' => false,
113 | ],
114 |
115 | 'media' => [
116 | 'middleware' => 'auth',
117 | 'format' => 'image|video|audio',
118 | 'image' => [
119 | 'thumbs' => [
120 | 'small' => [50, 50],
121 | 'medium' => [100, 100],
122 | ]
123 | ],
124 | 'multiple' => true,
125 | ],
126 |
127 | ],
128 |
129 | ```
130 |
131 | ## Front-end Integration
132 |
133 | Coming Soon
134 |
135 | ## License
136 |
137 | Laravel Media Upload is licensed under the [MIT License](http://opensource.org/licenses/MIT).
138 |
139 | Copyright 2015 [Trias Nur Rahman](http://triasrahman.com/)
140 |
--------------------------------------------------------------------------------
/src/MediaUploadController.php:
--------------------------------------------------------------------------------
1 | 'type-not-found' ];
26 | }
27 |
28 | // Check if file is uploaded
29 | if ( ! \Input::hasFile('file') )
30 | {
31 | return [ 'error' => 'file-not-found' ];
32 | }
33 |
34 | $file = \Input::file('file');
35 |
36 | // get file size in Bytes
37 | $file_size = $file->getSize();
38 |
39 | // Check the file size
40 | if ( $file_size > $config['max_size'] * 1024 || ( isset($configType['max_size']) && $file_size > $configType['max_size'] * 1024 ) )
41 | {
42 | return [ 'error' => 'limit-size' ];
43 | }
44 |
45 | // get the extension
46 | $ext = strtolower( $file->getClientOriginalExtension() );
47 |
48 | // checking file format
49 | $format = $this->getFileFormat($ext);
50 |
51 | // TODO: check file format
52 | if( isset($configType['format']) && ! in_array($format, explode('|', $configType['format'])) )
53 | {
54 | return [ 'error' => 'invalid-format' ];
55 | }
56 |
57 | // saving file
58 | $filename = date('U').str_random(10);
59 | $file->move(public_path().'/'.$config['dir'].'/'.$type, $filename.'.'.$ext);
60 |
61 | $file_path = $config['dir'].'/'.$type.'/'.$filename.'.'.$ext;
62 |
63 | if ( $format == 'image' && isset($config['types'][$type]['image']) && count($config['types'][$type]['image']) )
64 | {
65 |
66 | $img = Image::make(public_path().'/'.$file_path);
67 |
68 | foreach($config['types'][$type]['image'] as $task => $params)
69 | {
70 | switch($task) {
71 | case 'resize':
72 | $img->resize($params[0], $params[1]);
73 | break;
74 | case 'fit':
75 | $img->fit($params[0], $params[1]);
76 | break;
77 | case 'crop':
78 | $img->crop($params[0], $params[1]);
79 | break;
80 | case 'thumbs':
81 | $img->save();
82 |
83 | foreach($params as $name => $sizes) {
84 |
85 | $img->backup();
86 |
87 | $thumb_path = $config['dir'].'/'.$filename.'-'.$name.'.'.$ext;
88 |
89 | $img->fit($sizes[0], $sizes[1])->save($thumb_path);
90 |
91 | $img->reset();
92 | }
93 | break;
94 | }
95 | }
96 |
97 | $img->save();
98 | }
99 |
100 | return [
101 | 'original' => [
102 | 'name' => $file->getClientOriginalName(),
103 | 'size' => $file_size,
104 | ],
105 | 'ext' => $ext,
106 | 'format' => $format,
107 | // 'image' => [
108 | // 'size' =>$img->getSize(),
109 | // ],
110 | 'name' => $filename,
111 | 'path' => $file_path,
112 | ];
113 |
114 | }
115 |
116 | protected function getFileFormat($ext) {
117 | if ( preg_match('/(jpg|jpeg|gif|png)/', $ext) )
118 | {
119 | return 'image';
120 | }
121 | elseif( preg_match( '/(mp3|wav|ogg)/', $ext) )
122 | {
123 | return 'audio';
124 | }
125 | elseif( preg_match( '/(mp4|wmv|flv)/', $ext) )
126 | {
127 | return 'video';
128 | }
129 | elseif( preg_match('/txt/', $ext) )
130 | {
131 | return 'text';
132 | }
133 |
134 | return 'other';
135 | }
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/src/MediaUploadServiceProvider.php:
--------------------------------------------------------------------------------
1 | publishes([
23 | __DIR__.'/config/config.php' => config_path('media-upload.php'),
24 | ], 'config');
25 |
26 | // Define the route
27 | $routeConfig = [
28 | 'namespace' => 'Triasrahman\MediaUpload',
29 | ];
30 |
31 | if($this->app['config']->get('media-upload.middleware'))
32 | {
33 | $routeConfig['middleware'] = $this->app['config']->get('media-upload.middleware');
34 | }
35 |
36 | $this->app['router']->group($routeConfig, function($router) {
37 | $router->any($this->app['config']->get('media-upload.route', 'media-upload').'/{type?}', [
38 | 'uses' => 'MediaUploadController@index',
39 | 'as' => 'media-upload',
40 | ]);
41 | });
42 | }
43 |
44 | /**
45 | * Register the service provider.
46 | *
47 | * @return void
48 | */
49 | public function register()
50 | {
51 | //sad
52 | }
53 |
54 | /**
55 | * Get the services provided by the provider.
56 | *
57 | * @return array
58 | */
59 | public function provides()
60 | {
61 | return [];
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/config/config.php:
--------------------------------------------------------------------------------
1 | 'uploads',
15 |
16 | /*
17 | |--------------------------------------------------------------------------
18 | | DebugBar Route
19 | |--------------------------------------------------------------------------
20 | |
21 | | Every AJAX call needs an URL for file upload process. Set the route and
22 | | make sure no another route defined.
23 | |
24 | */
25 |
26 | 'route' => 'media-upload',
27 |
28 | /*
29 | |--------------------------------------------------------------------------
30 | | Middleware
31 | |--------------------------------------------------------------------------
32 | |
33 | | If you want to restrict the upload call, set the middleware.
34 | |
35 | */
36 |
37 | 'middleware' => 'auth',
38 |
39 | /*
40 | |--------------------------------------------------------------------------
41 | | Max File Size
42 | |--------------------------------------------------------------------------
43 | |
44 | | Make sure you give a limitation of file size uploaded (in KiloBytes).
45 | |
46 | */
47 |
48 | 'max_size' => 10240,
49 |
50 | /*
51 | |--------------------------------------------------------------------------
52 | | Upload Types
53 | |--------------------------------------------------------------------------
54 | |
55 | | It's the flexibility of this package. You can define the type of upload
56 | | file methods. For example, you want to upload for profile picture,
57 | | article post, background, etc. Here is
58 | |
59 | */
60 |
61 | 'types' => [
62 | 'default' => [
63 | 'middleware' => 'auth',
64 | 'format' => 'image|video',
65 | // 'image' => [
66 | // 'resize' => [1024, 768],
67 | // 'crop' => [800, 800],
68 | // 'fit' => [400, 400],
69 | // 'thumbs' => [
70 | // 'small' => [50, 50],
71 | // 'medium' => [100, 100],
72 | // ]
73 | // ],
74 | 'multiple' => false,
75 | 'save_original' => false,
76 | ],
77 |
78 | // ... put your custom type ...
79 | ],
80 |
81 | ];
--------------------------------------------------------------------------------
/tests/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/triasrahman/laravel-media-upload/52883747f34c876e5ceeca1917834fb1f7e45fa0/tests/.gitkeep
--------------------------------------------------------------------------------