Someline Image is a helper service to handle uploaded images and store images without duplicates.
Build for Laravel and Someline Starter.
Install composer package to your laravel project
composer require someline/someline-image
Add Service Provider to config/app.php
'providers' => [
...
Someline\Image\SomelineImageServiceProvider::class,
...
],
Publishing config file.
php artisan vendor:publish
After published, config file for Rest Client is config/someline-image.php
, you will need to config it to use Rest Client.
Route::get('/image/{name}', 'ImageController@showOriginalImage');
Route::post('/image', 'ImageController@postImage');
Use on the Model:
use SomelineHasImageablesTrait;
Usage:
/** @var SomelineImage $somelineImage */
$somelineImage = SomelineImage::find(1);
/** @var User $user */
$user = User::find(1);
// save image relations smartly with sequence support (recommended)
$user->syncImages([1, 2], ['type' => 'cover', 'data' => json_encode('a')]);
// save image relations via save
$user->images()->save($somelineImage, ['type' => 'cover', 'data' => json_encode('a')]);
// save image relations via attach
$user->images()->attach(1, ['type' => 'cover', 'data' => json_encode('a')]);
// update image relations via sync
$user->images()->sync([1]);
// set as main image
$user->setAsMainImage($somelineImage);
// set as type main image
$user->setAsTypeMainImage('cover', $somelineImage);
// get all images
print_r($user->getImages()->toArray());
// get first main image
print_r($user->getMainImage()->toArray());
// get all main images
print_r($user->getMainImages()->toArray());
// get all type images
print_r($user->getTypeImages('cover')->toArray());
// get all type images and are main images
print_r($user->getTypeMainImages('cover')->toArray());
app/Http/Controllers/ImageController.php
<?php namespace App\Http\Controllers;
use Exception;
use Illuminate\Http\Request;
use Someline\Image\Controllers\SomelineImageController;
use Someline\Models\Image\SomelineImage;
use Someline\Image\SomelineImageService;
class ImageController extends Controller
{
public function postImage(Request $request)
{
$somelineImageService = new SomelineImageService();
$file = $request->file('image');
$somelineImage = null;
try {
/** @var SomelineImage $somelineImage */
$somelineImage = $somelineImageService->handleUploadedFile($file);
} catch (Exception $e) {
return 'Failed to save: ' . $e->getMessage();
}
if (!$somelineImage) {
return 'Failed to save uploaded image.';
}
$somelineImageId = $somelineImage->getSomelineImageId();
return 'Saved: ' . $somelineImage->getImageUrl();
}
public function showOriginalImage($image_name)
{
return SomelineImageController::showImage('original', $image_name);
}
}
phpunit
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.