Commit bea53b77 authored by Nicolas Widart's avatar Nicolas Widart

Adding a factory for image manipulations

parent 90bad245
...@@ -5,6 +5,7 @@ return [ ...@@ -5,6 +5,7 @@ return [
'crop' => [ 'crop' => [
'width' => '200', 'width' => '200',
'height' => '250' 'height' => '250'
] ],
// 'blur' => '15'
] ]
]; ];
...@@ -4,9 +4,9 @@ use Illuminate\Support\Facades\Redirect; ...@@ -4,9 +4,9 @@ use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\View;
use Laracasts\Flash\Flash; use Laracasts\Flash\Flash;
use Modules\Core\Http\Controllers\Admin\AdminBaseController; use Modules\Core\Http\Controllers\Admin\AdminBaseController;
use Modules\Media\Croppy\Croppy;
use Modules\Media\Entities\File; use Modules\Media\Entities\File;
use Modules\Media\Http\Requests\UpdateMediaRequest; use Modules\Media\Http\Requests\UpdateMediaRequest;
use Modules\Media\Image\Imagy;
use Modules\Media\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
class MediaController extends AdminBaseController class MediaController extends AdminBaseController
...@@ -16,14 +16,14 @@ class MediaController extends AdminBaseController ...@@ -16,14 +16,14 @@ class MediaController extends AdminBaseController
*/ */
private $file; private $file;
/** /**
* @var Croppy * @var Imagy
*/ */
private $croppy; private $imagy;
public function __construct(FileRepository $file, Croppy $croppy) public function __construct(FileRepository $file, Imagy $imagy)
{ {
$this->file = $file; $this->file = $file;
$this->croppy = $croppy; $this->imagy = $imagy;
} }
/** /**
...@@ -73,7 +73,8 @@ class MediaController extends AdminBaseController ...@@ -73,7 +73,8 @@ class MediaController extends AdminBaseController
*/ */
public function edit(File $file) public function edit(File $file)
{ {
dd($this->croppy->image('/assets/media/screen-shot-2014-10-25-at-44939-pm.png', 'smallThumb')); dd($this->imagy->get('/assets/media/screen-shot-2014-10-25-at-44939-pm.png', 'smallThumb'));
//dd($this->croppy->image('/assets/media/screen-shot-2014-10-25-at-44939-pm.png', 'smallThumb'));
return View::make('media::admin.edit', compact('file')); return View::make('media::admin.edit', compact('file'));
} }
......
<?php namespace Modules\Media\Image;
interface ImageFactoryInterface
{
/**
* Return a new Manipulation class
* @param array $manipulation
* @return \Modules\Media\Image\ImageHandlerInterface
*/
public function make($manipulation);
}
<?php namespace Modules\Media\Image;
interface ImageHandlerInterface
{
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return \Intervention\Image\Image
*/
public function handle($image, $options);
}
<?php namespace Modules\Media\Croppy; <?php namespace Modules\Media\Image;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Modules\Media\Image\Intervention\InterventionFactory;
class CroppyServiceProvider extends ServiceProvider class ImageServiceProvider extends ServiceProvider
{ {
public function boot() public function boot()
{ {
$this->package('nwidart/croppy'); $this->package('nwidart/imagy');
} }
/** /**
...@@ -16,9 +17,13 @@ class CroppyServiceProvider extends ServiceProvider ...@@ -16,9 +17,13 @@ class CroppyServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
$app = $this->app; $this->app->bind(
$app['croppy'] = $app->share(function ($app) { 'Modules\Media\Image\ImageFactoryInterface',
new Croppy($app['filesystem.disk'], $app['config']); 'Modules\Media\Image\Intervention\InterventionFactory'
);
$app['imagy'] = $this->app->share(function ($app) {
return new Imagy($app['config'], new InterventionFactory);
}); });
} }
...@@ -29,6 +34,6 @@ class CroppyServiceProvider extends ServiceProvider ...@@ -29,6 +34,6 @@ class CroppyServiceProvider extends ServiceProvider
*/ */
public function provides() public function provides()
{ {
return ['croppy']; return ['imagy'];
} }
} }
<?php namespace Modules\Media\Croppy; <?php namespace Modules\Media\Image;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FileNotFoundException; use Illuminate\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Intervention\Image\Image; use Illuminate\Contracts\Config\Repository;
class Croppy class Imagy
{ {
/** /**
* @var Image * @var \Intervention\Image\Image
*/ */
private $image; private $image;
/** /**
* @var Filesystem * @var \Illuminate\Filesystem\Filesystem
*/ */
private $finder; private $finder;
/** /**
* @var Repository * @var \Illuminate\Contracts\Config\Repository
*/ */
private $config; private $config;
/**
* @var ImageFactoryInterface
*/
private $imageFactory;
public function __construct(Filesystem $finder, Repository $config) public function __construct(Repository $config, ImageFactoryInterface $imageFactory)
{ {
$this->image = App::make('Intervention\Image\ImageManager'); $this->image = App::make('Intervention\Image\ImageManager');
$this->finder = App::make('Illuminate\Filesystem\Filesystem'); $this->finder = App::make('Illuminate\Filesystem\Filesystem');
$this->config = $config; $this->config = $config;
$this->imageFactory = $imageFactory;
} }
public function image($path, $thumbnail) public function get($path, $thumbnail)
{ {
$filename = '/assets/media/' . $this->newFilename($path, $thumbnail); $filename = '/assets/media/' . $this->newFilename($path, $thumbnail);
try { try {
$this->finder->get(public_path(). $filename); $this->finder->get(public_path(). $filename);
return $filename; return $filename;
} catch (FileNotFoundException $e) { } catch (FileNotFoundException $e) {
} $image = $this->image->make(public_path() . $path);
$image = $this->makeImage($path, $thumbnail); foreach ($this->config->get("media::thumbnails.{$thumbnail}") as $manipulation => $options) {
$image = $this->imageFactory->make($manipulation)->handle($image, $options);
}
$this->finder->put(public_path() . $filename, $image); $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
return $filename; $this->finder->put(public_path() . $filename, $image);
}
} }
/** /**
...@@ -56,15 +62,4 @@ class Croppy ...@@ -56,15 +62,4 @@ class Croppy
return $filename . '_' . $thumbnail . '.' . pathinfo($path, PATHINFO_EXTENSION); return $filename . '_' . $thumbnail . '.' . pathinfo($path, PATHINFO_EXTENSION);
} }
private function makeImage($path, $thumbnail)
{
$thumbnailActions = $this->config->get("media::thumbnails.{$thumbnail}");
$image = $this->image->make(public_path() . $path);
return $image->crop($thumbnailActions['crop']['width'], $thumbnailActions['crop']['height'])
->encode(pathinfo($path, PATHINFO_EXTENSION));
}
} }
<?php namespace Modules\Media\Image\Intervention;
use Modules\Media\Image\ImageFactoryInterface;
class InterventionFactory implements ImageFactoryInterface
{
/**
* @param $manipulation
* @return mixed
*/
public function make($manipulation)
{
$class = 'Modules\\Media\\Image\\Intervention\\Manipulations\\'. ucfirst($manipulation);
return new $class;
}
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
abstract class BaseManipulation
{
}
<?php namespace Modules\Media\Image\Intervention\Manipulations;
use Modules\Media\Image\ImageHandlerInterface;
class Crop extends BaseManipulation implements ImageHandlerInterface
{
/**
* Handle the image manipulation request
* @param \Intervention\Image\Image $image
* @param array $options
* @return mixed
*/
public function handle($image, $options)
{
return $image->crop($options['width'], $options['height']);
}
}
...@@ -11,6 +11,6 @@ ...@@ -11,6 +11,6 @@
"providers": [ "providers": [
"Modules\\Media\\Providers\\MediaServiceProvider", "Modules\\Media\\Providers\\MediaServiceProvider",
"Modules\\Media\\Providers\\RouteServiceProvider", "Modules\\Media\\Providers\\RouteServiceProvider",
"Modules\\Media\\Croppy\\CroppyServiceProvider" "Modules\\Media\\Image\\ImageServiceProvider"
] ]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment