Commit 90bad245 authored by Nicolas Widart's avatar Nicolas Widart

Prepare a croppy class

parent c58bdbdc
<?php
return [
'smallThumb' => [
'crop' => [
'width' => '200',
'height' => '250'
]
]
];
<?php namespace Modules\Media\Croppy;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\App;
use Intervention\Image\Image;
class Croppy
{
/**
* @var Image
*/
private $image;
/**
* @var Filesystem
*/
private $finder;
/**
* @var Repository
*/
private $config;
public function __construct(Filesystem $finder, Repository $config)
{
$this->image = App::make('Intervention\Image\ImageManager');
$this->finder = App::make('Illuminate\Filesystem\Filesystem');
$this->config = $config;
}
public function image($path, $thumbnail)
{
$filename = '/assets/media/' . $this->newFilename($path, $thumbnail);
try {
$this->finder->get(public_path(). $filename);
return $filename;
} catch (FileNotFoundException $e) {
}
$image = $this->makeImage($path, $thumbnail);
$this->finder->put(public_path() . $filename, $image);
return $filename;
}
/**
* Prepend the thumbnail name to filename
* @param $path
* @param $thumbnail
* @return mixed|string
*/
private function newFilename($path, $thumbnail)
{
$filename = pathinfo($path, PATHINFO_FILENAME);
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\Croppy;
use Illuminate\Support\ServiceProvider;
class CroppyServiceProvider extends ServiceProvider
{
public function boot()
{
$this->package('nwidart/croppy');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$app = $this->app;
$app['croppy'] = $app->share(function ($app) {
new Croppy($app['filesystem.disk'], $app['config']);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['croppy'];
}
}
...@@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Redirect; ...@@ -4,6 +4,7 @@ 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\Repositories\FileRepository; use Modules\Media\Repositories\FileRepository;
...@@ -14,10 +15,15 @@ class MediaController extends AdminBaseController ...@@ -14,10 +15,15 @@ class MediaController extends AdminBaseController
* @var FileRepository * @var FileRepository
*/ */
private $file; private $file;
/**
* @var Croppy
*/
private $croppy;
public function __construct(FileRepository $file) public function __construct(FileRepository $file, Croppy $croppy)
{ {
$this->file = $file; $this->file = $file;
$this->croppy = $croppy;
} }
/** /**
...@@ -67,6 +73,7 @@ class MediaController extends AdminBaseController ...@@ -67,6 +73,7 @@ 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'));
return View::make('media::admin.edit', compact('file')); return View::make('media::admin.edit', compact('file'));
} }
......
<?php namespace Modules\Media\Tests;
use Modules\Core\Tests\BaseTestCase;
use Modules\Media\Croppy\Croppy;
class CroppyTest extends BaseTestCase
{
/**
* @var Croppy
*/
protected $croppy;
public function setUp()
{
$this->refreshApplication();
$this->croppy = new Croppy($this->app['filesystem.disk'], $this->app['config']);
}
/** @test */
public function it_should_return_an_extension()
{
}
}
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
}, },
"providers": [ "providers": [
"Modules\\Media\\Providers\\MediaServiceProvider", "Modules\\Media\\Providers\\MediaServiceProvider",
"Modules\\Media\\Providers\\RouteServiceProvider" "Modules\\Media\\Providers\\RouteServiceProvider",
"Modules\\Media\\Croppy\\CroppyServiceProvider"
] ]
} }
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