Skip to content

Commit 6414999

Browse files
authored
Merge pull request #1266 from sonole/sonole
Fix old implementation of intervention/image
2 parents e82f8f6 + 6087ea9 commit 6414999

File tree

7 files changed

+104
-56
lines changed

7 files changed

+104
-56
lines changed

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
}
2424
],
2525
"require": {
26-
"php": ">=7.2.0",
26+
"php": ">=8.1",
2727
"ext-exif": "*",
2828
"ext-fileinfo": "*",
29-
"intervention/image": ">=2.0.0",
30-
"illuminate/config": "5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
31-
"illuminate/filesystem": "5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
32-
"illuminate/support": "5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
33-
"illuminate/http": "5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
34-
"illuminate/container": "5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
29+
"intervention/image": ">=3.11.3",
30+
"illuminate/config": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
31+
"illuminate/filesystem": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
32+
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
33+
"illuminate/http": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
34+
"illuminate/container": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
3535
"league/flysystem": ">=2.0.0"
3636
},
3737
"require-dev": {

src/Controllers/CropController.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace UniSharp\LaravelFilemanager\Controllers;
44

5-
use Intervention\Image\Facades\Image as InterventionImageV2;
6-
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
5+
use UniSharp\LaravelFilemanager\Services\ImageService;
76
use UniSharp\LaravelFilemanager\Events\ImageIsCropping;
87
use UniSharp\LaravelFilemanager\Events\ImageWasCropped;
98

109
class CropController extends LfmController
1110
{
11+
private ImageService $imageService;
12+
13+
public function __construct(ImageService $imageService)
14+
{
15+
$this->imageService = $imageService;
16+
parent::__construct();
17+
}
18+
1219
/**
1320
* Show crop page.
1421
*
@@ -42,16 +49,9 @@ public function getCropImage($overWrite = true)
4249

4350
$crop_info = request()->only('dataWidth', 'dataHeight', 'dataX', 'dataY');
4451

45-
// crop image
46-
if (class_exists(InterventionImageV2::class)) {
47-
InterventionImageV2::make($image_path)
48-
->crop(...array_values($crop_info))
49-
->save($crop_path);
50-
} else {
51-
InterventionImageV3::read($image_path)
52-
->crop(...array_values($crop_info))
53-
->save($crop_path);
54-
}
52+
$this->imageService->read($image_path)
53+
->crop(...array_values($crop_info))
54+
->save($crop_path);
5555

5656
// make new thumbnail
5757
$this->lfm->generateThumbnail($image_name);

src/Controllers/ResizeController.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace UniSharp\LaravelFilemanager\Controllers;
44

5-
use Intervention\Image\Facades\Image as InterventionImageV2;
6-
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
5+
use UniSharp\LaravelFilemanager\Services\ImageService;
76
use UniSharp\LaravelFilemanager\Events\ImageIsResizing;
87
use UniSharp\LaravelFilemanager\Events\ImageWasResized;
98

109
class ResizeController extends LfmController
1110
{
11+
private ImageService $imageService;
12+
13+
public function __construct(ImageService $imageService)
14+
{
15+
$this->imageService = $imageService;
16+
parent::__construct();
17+
}
18+
1219
/**
1320
* Dipsplay image for resizing.
1421
*
@@ -19,11 +26,7 @@ public function getResize()
1926
$ratio = 1.0;
2027
$image = request('img');
2128

22-
if (class_exists(InterventionImageV2::class)) {
23-
$original_image = InterventionImageV2::make($this->lfm->setName($image)->path('absolute'));
24-
} else {
25-
$original_image = InterventionImageV3::read($this->lfm->setName($image)->path('absolute'));
26-
}
29+
$original_image = $this->imageService->read($this->lfm->setName($image)->path('absolute'));
2730
$original_width = $original_image->width();
2831
$original_height = $original_image->height();
2932

@@ -71,15 +74,10 @@ public function performResize($overWrite = true)
7174

7275
event(new ImageIsResizing($image_path));
7376

74-
if (class_exists(InterventionImageV2::class)) {
75-
InterventionImageV2::make($image_path)
76-
->resize(request('dataWidth'), request('dataHeight'))
77-
->save($resize_path);
78-
} else {
79-
InterventionImageV3::read($image_path)
80-
->resize(request('dataWidth'), request('dataHeight'))
81-
->save($resize_path);
82-
}
77+
$this->imageService->read($image_path)
78+
->resize(request('dataWidth'), request('dataHeight'))
79+
->save($resize_path);
80+
8381
event(new ImageWasResized($image_path));
8482

8583
return parent::$success_response;

src/LaravelFilemanagerServiceProvider.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
use Illuminate\Support\Facades\Route;
66
use Illuminate\Support\ServiceProvider;
7+
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
8+
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
9+
use Intervention\Image\ImageManager;
10+
use Intervention\Image\Interfaces\ImageManagerInterface;
11+
use UniSharp\LaravelFilemanager\Services\ImageService;
712

813
/**
914
* Class LaravelFilemanagerServiceProvider.
@@ -17,24 +22,24 @@ class LaravelFilemanagerServiceProvider extends ServiceProvider
1722
*/
1823
public function boot()
1924
{
20-
$this->loadTranslationsFrom(__DIR__.'/lang', 'laravel-filemanager');
25+
$this->loadTranslationsFrom(__DIR__ . '/lang', 'laravel-filemanager');
2126

22-
$this->loadViewsFrom(__DIR__.'/views', 'laravel-filemanager');
27+
$this->loadViewsFrom(__DIR__ . '/views', 'laravel-filemanager');
2328

2429
$this->publishes([
2530
__DIR__ . '/config/lfm.php' => base_path('config/lfm.php'),
2631
], 'lfm_config');
2732

2833
$this->publishes([
29-
__DIR__.'/../public' => public_path('vendor/laravel-filemanager'),
34+
__DIR__ . '/../public' => public_path('vendor/laravel-filemanager'),
3035
], 'lfm_public');
3136

3237
$this->publishes([
33-
__DIR__.'/views' => base_path('resources/views/vendor/laravel-filemanager'),
38+
__DIR__ . '/views' => base_path('resources/views/vendor/laravel-filemanager'),
3439
], 'lfm_view');
3540

3641
$this->publishes([
37-
__DIR__.'/Handlers/LfmConfigHandler.php' => base_path('app/Handlers/LfmConfigHandler.php'),
42+
__DIR__ . '/Handlers/LfmConfigHandler.php' => base_path('app/Handlers/LfmConfigHandler.php'),
3843
], 'lfm_handler');
3944

4045
if (config('lfm.use_package_routes')) {
@@ -51,10 +56,31 @@ public function boot()
5156
*/
5257
public function register()
5358
{
54-
$this->mergeConfigFrom(__DIR__ . '/config/lfm.php', 'lfm-config');
59+
$this->mergeConfigFrom(__DIR__ . '/config/lfm.php', 'lfm');
5560

5661
$this->app->singleton('laravel-filemanager', function () {
5762
return true;
5863
});
64+
65+
$this->app->singleton(ImageManagerInterface::class, function ($app) {
66+
$driver = config('lfm.intervention_driver');
67+
68+
$driverInstance = match ($driver) {
69+
'gd' => new GdDriver(),
70+
'imagick' => new ImagickDriver(),
71+
default => null,
72+
};
73+
74+
if (is_null($driverInstance)) {
75+
\Log::error("Unsupported image driver [$driver]. GdDriver will be used.");
76+
$driverInstance = new GdDriver();
77+
}
78+
79+
return new ImageManager($driverInstance);
80+
});
81+
82+
$this->app->singleton(ImageService::class, function ($app) {
83+
return new ImageService($app->make(ImageManagerInterface::class));
84+
});
5985
}
6086
}

src/LfmPath.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace UniSharp\LaravelFilemanager;
44

55
use Illuminate\Container\Container;
6-
use Intervention\Image\Facades\Image as InterventionImageV2;
7-
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
86
use Symfony\Component\HttpFoundation\File\UploadedFile;
7+
use UniSharp\LaravelFilemanager\Services\ImageService;
98
use UniSharp\LaravelFilemanager\Events\FileIsUploading;
109
use UniSharp\LaravelFilemanager\Events\FileWasUploaded;
1110
use UniSharp\LaravelFilemanager\Events\ImageIsUploading;
@@ -20,9 +19,12 @@ class LfmPath
2019

2120
private $helper;
2221

23-
public function __construct(Lfm $lfm)
22+
private ImageService $imageService;
23+
24+
public function __construct(Lfm $lfm, ImageService $imageService)
2425
{
2526
$this->helper = $lfm;
27+
$this->imageService = $imageService;
2628
}
2729

2830
public function __get($var_name)
@@ -325,18 +327,10 @@ public function generateThumbnail($file_name)
325327
$thumbWidth = $this->helper->shouldCreateCategoryThumb() && $this->helper->categoryThumbWidth() ? $this->helper->categoryThumbWidth() : config('lfm.thumb_img_width', 200);
326328
$thumbHeight = $this->helper->shouldCreateCategoryThumb() && $this->helper->categoryThumbHeight() ? $this->helper->categoryThumbHeight() : config('lfm.thumb_img_height', 200);
327329

328-
if (class_exists(InterventionImageV2::class)) {
329-
$encoded_image = InterventionImageV2::make($original_image->get())
330-
->fit($thumbWidth, $thumbHeight)
331-
->stream()
332-
->detach();
333-
} else {
334-
$encoded_image = InterventionImageV3::read($original_image->get())
335-
->cover($thumbWidth, $thumbHeight)
336-
->encodeByMediaType();
337-
}
338-
339-
330+
$encoded_image = $this->imageService->read($original_image->get())
331+
->cover($thumbWidth, $thumbHeight)
332+
->encodeByMediaType();
333+
340334
$this->storage->put($encoded_image, 'public');
341335
}
342336
}

src/Services/ImageService.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace UniSharp\LaravelFilemanager\Services;
4+
5+
use Intervention\Image\Interfaces\ImageManagerInterface;
6+
use Intervention\Image\Image;
7+
8+
class ImageService
9+
{
10+
protected ImageManagerInterface $imageManager;
11+
12+
public function __construct(ImageManagerInterface $imageManager)
13+
{
14+
$this->imageManager = $imageManager;
15+
}
16+
17+
/**
18+
* Dynamically forward method calls to the underlying ImageManagerInterface.
19+
*/
20+
public function __call(string $method, array $arguments)
21+
{
22+
if (method_exists($this->imageManager, $method)) {
23+
return $this->imageManager->$method(...$arguments);
24+
}
25+
26+
throw new \BadMethodCallException("Method {$method} does not exist on ImageService or ImageManagerInterface.");
27+
}
28+
}

src/config/lfm.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,6 @@
179179
'php_ini_overrides' => [
180180
'memory_limit' => '256M',
181181
],
182+
183+
'intervention_driver' => 'gd', // options: gd, imagick
182184
];

0 commit comments

Comments
 (0)