Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
saqueib committed Sep 21, 2018
0 parents commit a36e952
Show file tree
Hide file tree
Showing 20 changed files with 5,503 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
/node_modules
/vendor
/.idea
/.vscode
.env
.phpunit.result.cache
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: php

php:
- 5.6
- 7.0

before_script:
- travis_retry composer self-update
- travis_retry composer install

script: vendor/bin/phpunit --verbose
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,3 @@
# Changelog

All notable changes to `laravel-imageup` will be documented in this file
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/spatie/eloquent-sortable).


## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


## Running Tests

``` bash
$ vendor/bin/phpunit
```


**Happy coding**!
20 changes: 20 additions & 0 deletions LICENSE.md
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2018 Mohd Saqueib Ansari <saquibeweb@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
266 changes: 266 additions & 0 deletions README.md
@@ -0,0 +1,266 @@
## Laravel ImageUp

[![Latest Version on Packagist](https://img.shields.io/packagist/v/qcod/laravel-imageup.svg)](https://packagist.org/packages/qcod/laravel-imageup)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/qcod/laravel-imageup/master.svg)](https://travis-ci.org/qcod/laravel-imageup)
[![Total Downloads](https://img.shields.io/packagist/dt/qcod/laravel-imageup.svg)](https://packagist.org/packages/qcod/laravel-imageup)

The `qcod/laravel-imageup` is a trait which gives you auto upload, resize and crop for image feature with tons of customization.

### Installation

You can install the package via composer:

```bash
$ composer require qcod/imageup
```

The package will automatically register itself. In case you need to manually register it you can by adding it in `config/app.php` providers array:

```php
QCod\ImageUp\ImageUpServiceProvider::class
```

You can optionally publish the config file with:

```bash
php artisan vendor:publish --provider="QCod\ImageUp\ImageUpServiceProvider" --tag="config"
```

It will create `config/imageup.php` with all the settings.

### Getting Started

To use this trait you just need to add `use HasImageUploads` on the Eloquent model and define all the fields which needs to store the images in database.

**Model**
```php
<?php
namespace App;

use QCod\ImageUp\HasImageUploads;
use Illuminate\Database\Eloquent\Model;

class User extends Model {
use HasImageUploads;

// assuming `users` table has 'cover', 'avatar' columns
// mark all the columns as image fields
protected static $imageFields = [
'cover', 'avatar'
];
}
```

Once you marked all the image fields in model it will auto upload the image whenever you save the model by hooking in `Model::saved()` event.
It will also update the database with new stored file path and if finds old image it will be deleted once new image is uploaded.

> Image field should be as as `VARCHAR` in database table to store the path of uploaded image.
**In Controller**
```php
<?php
namespace App;
use App\Http\Controllers\Controller;

class UserController extends Controller {
public function store(Request $request){
return User::create($request->all());
}
}

```
## Image Field options

ImageUp gives you tons of customization on how the upload and resize will be handled from defined field options, following are the things you can customize:

```php
<?php
namespace App;

use QCod\ImageUp\HasImageUploads;
use Illuminate\Database\Eloquent\Model;

class User extends Model {

use HasImageUploads;

// which disk to use for upload, can be override by field options
protected $imagesUploadDisk = 'local';

// path in disk to use for upload, can be override by field options
protected $imagesUploadPath = 'uploads';

// auto upload allowed
protected $autoUploadImages = true;

// all the images fields for model
protected static $imageFields = [
'avatar' => [
// width to resize image after upload
'width' => 200,

// height to resize image after upload
'height' => 100,

// set true to crop image with the given width/height and you can also pass arr [x,y] coordinate for crop.
'crop' => true,

// what disk you want to upload, default config('imageup.upload_disk')
'disk' => 'public',

// a folder path on the above disk, default config('imageup.upload_directory')
'path' => 'avatars',

// placeholder image if image field is empty
'placeholder' => '/images/avatar-placeholder.svg',

// validation rules when uploading image
'rules' => 'image|max:2000',

// override global auto upload setting coming from config('imageup.auto_upload_images')
'auto_upload' => false,

// if request file is don't have same name, default will be the field name
'file_input' => 'photo'
],
'cover' => [
//...
]
];
}
```

### Available methods

You are not limited to use auto upload image feature only. This trait will give you following methods which you can use to manually upload and resize image.

#### $model->uploadImage($imageFile, $field = null)

Upload image for given $field, if $field is null it will upload to first image option defined in array.

```php
$user = User::findOrFail($id);
$user->uploadImage(request()->file('cover'), 'cover');
```

#### $model->setImagesField($fieldsOptions)

You can also set the image fields dynamically by calling `$model->setImagesField($fieldsOptions)` with field options, it will replace fields defined on model property.

```php
$user = User::findOrFail($id);

$fieldOptions = [
'cover' => [ 'width' => 1000 ],
'avatar' => [ 'width' => 120, 'crop' => true ]
];

// override image fields defined on model
$user->setImagesField($fieldOptions);
```

#### $model->hasImageField($field)

To check if field is defined as image field.

#### $model->deleteImage($filePath)

Delete any image if it exists.

#### $model->resizeImage($imageFile, $fieldOptions)

If you have image already you can call this method to resize it with the same options we have used for image fields.

```php
$user = User::findOrFail($id);

// resize image, it will give you resized image, you need to save it
$imageFile = '/images/some-big-image.jpg';
$image = $user->resizeImage($imageFile, [ 'width' => 120, 'crop' => true ]);

// or you can use uploaded file
$imageFile = request()->file('avatar');
$image = $user->resizeImage($imageFile, [ 'width' => 120, 'crop' => true ]);
```

#### $model->cropTo($x, $y)->resizeImage($imageFile, $field = null)

You can use this `cropTo()` method to set the x and y coordinates of cropping. It will be very useful if you are getting coordinate from some sort of font-end image cropping library.

```php
$user = User::findOrFail($id);

// uploaded file from request
$imageFile = request()->file('avatar');

// coordinates from request
$coords = request()->only(['crop_x', 'crop_y']);

// resizing will give you intervention image back
$image = $user->cropTo($coords)
->resizeImage($imageFile, [ 'width' => 120, 'crop' => true ]);

// or you can do upload and resize like this, it will override field options crop setting
$user->cropTo($coords)
->uploadImage(request()->file('cover'), 'avatar');
```

#### $model->imageUrl($field)

Gives uploaded image url for given image field.

```php
$user = User::findOrFail($id);

// in your view
<img src="{{ $user->imageUrl('cover') }}" alt="" />
// http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg
```

#### $model->imageTag($field, $attribute = '')

It gives you `<img />` tag for a field.

```html
{!! $model->imageTag('avatar') !!}
<!-- <img src="http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg" /> -->

{!! $model->imageTag('avatar', 'class="float-left mr-3"') !!}
<!-- <img src="http://www.example.com/storage/uploads/iGqUEbCPTv7EuqkndE34CNitlJbFhuxEWmgN9JIh.jpeg" class="float-left mr-3 /> -->
```

Config file

```php
<?php
// config file
```

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

### Testing
The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.

```bash
$ composer test
```

### Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

If you discover any security related issues, please email saquibwebk@gmail.com instead of using the issue tracker.

### Credits
- [Mohd Saqueib Ansari](https://github.com/saqueib)

### About QCode.in
QCode.in (https://www.qcode.in) is blog by [Saqueib](https://github.com/saqueib) which covers All about Full Stack Web Development.

### License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
53 changes: 53 additions & 0 deletions composer.json
@@ -0,0 +1,53 @@
{
"name": "qcod/laravel-imageup",
"description": "Auto Image upload, resize and crop for Laravel eloquent model using Intervention image",
"homepage": "https://github.com/qcod/laravel-imageup",
"type": "library",
"license": "MIT",
"keywords": [
"laravel",
"image upload",
"image crop",
"image resize",
"intervention image",
"eloquent",
"model",
"laravel5"
],
"authors": [
{
"name": "Mohd Saqueib Ansari",
"email": "saquibweb@gmail.com"
}
],
"require": {
"php": ">=5.6.0",
"laravel/framework": "~5.4.0|~5.5.0|~5.6.0|~5.7.0",
"intervention/image": "^2.4"
},
"require-dev": {
"orchestra/testbench": "~3.5",
"phpunit/phpunit" : "^6.2|^7.0",
"mockery/mockery": "^1.1"
},
"autoload": {
"psr-4": {
"QCod\\ImageUp\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"QCod\\ImageUp\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"QCod\\ImageUp\\ImageUpServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}

0 comments on commit a36e952

Please sign in to comment.