app/Helpers/SubfoldersRecursive.php
<?php
namespace App\Helpers;
use Symfony\Component\Finder\Finder;
class SubfoldersRecursive
{
public static function get($paths){
$finder = new Finder;
foreach ($paths as $path) {
$finder->in($path);
}
$finder->directories();
$directories = iterator_to_array($finder->getIterator());
if (count($directories)) {
return array_keys($directories);
}
return $paths;
}
}
config/imagecache.php
<?php
use App\Helpers\SubfoldersRecursive;
$defaultPaths = [
public_path('uploads/products'),
];
return [
....
'paths' => SubfoldersRecursive::get($defaultPaths),
....
];
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Intervention\Image\ImageCacheController;
use App\Http\Controllers\DnkImageCacheController;
use Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
$this->app->bind(ImageCacheController::class, DnkImageCacheController::class);
}
}
app/Http/Controllers/DnkImageCacheController.php
<?php
namespace App\Http\Controllers;
use Intervention\Image\ImageCacheController;
use Symfony\Component\Finder\Finder;
class DnkImageCacheController extends ImageCacheController
{
public function __call($name, $arguments)
{
if ($name == "getImagePath") {
$filename = $arguments[0];
//код приватного метода
// find file
$finder = new Finder;
foreach (config('imagecache.paths') as $path) {
// don't allow '..' in filenames
$image_path = $path.'/'.str_replace('..', '', $filename);
if (file_exists($image_path) && is_file($image_path)) {
// file found
return $image_path;
}
$finder->in($path);
}
$finder->files()->name($filename);
$files = iterator_to_array($finder->getIterator());
if (count($files)) {
return array_keys($files)[0];
}
// file not found
abort(404);
return abort(404);
}
}
}
composer dump-autoload
$images = $model->getImages();
foreach($images as $img) {
//retun url to full image
echo $img->getUrl();
//return url to proportionally resized image by width
echo $img->getUrl('300x');
//return url to proportionally resized image by height
echo $img->getUrl('x300');
//return url to resized and cropped (center) image by width and height
echo $img->getUrl('200x300');
//return alt text to image
$img->alt
//return title to image
$img->title
//return description image
$img->description
}