Within a Laravel application it is possible to use the URL to manipulate images dynamically. The manipulated version of the an image will be stored in the cache and will be loaded directly without resource-intensive GD operation.
An image has to be uploaded only once. All manipulations like resizing or cropping will be handled later, when the file is accessed via a HTTP request like this:
$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
}
class AppServiceProvider extends ServiceProvider
{
{
$this->app->bind(ImageCacheController::class, MyImageCacheController::class);
}
}
class MyImageCacheController extends ImageCacheController
{
public function __call($name, $arguments)
{
if ($name == "getImagePath") {
$filename = $arguments[0];
//код приватного метода
}
}
}
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
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),
....
];