Skip to content

Fix old implementation of intervention/image #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
}
],
"require": {
"php": ">=7.2.0",
"php": ">=8.1",
"ext-exif": "*",
"ext-fileinfo": "*",
"intervention/image": ">=2.0.0",
"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",
"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",
"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",
"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",
"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",
"intervention/image": ">=3.11.3",
"illuminate/config": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"illuminate/filesystem": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"illuminate/http": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"illuminate/container": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0 || ^12.0",
"league/flysystem": ">=2.0.0"
},
"require-dev": {
Expand Down
24 changes: 12 additions & 12 deletions src/Controllers/CropController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace UniSharp\LaravelFilemanager\Controllers;

use Intervention\Image\Facades\Image as InterventionImageV2;
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
use UniSharp\LaravelFilemanager\Services\ImageService;
use UniSharp\LaravelFilemanager\Events\ImageIsCropping;
use UniSharp\LaravelFilemanager\Events\ImageWasCropped;

class CropController extends LfmController
{
private ImageService $imageService;

public function __construct(ImageService $imageService)
{
$this->imageService = $imageService;
parent::__construct();
}

/**
* Show crop page.
*
Expand Down Expand Up @@ -42,16 +49,9 @@ public function getCropImage($overWrite = true)

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

// crop image
if (class_exists(InterventionImageV2::class)) {
InterventionImageV2::make($image_path)
->crop(...array_values($crop_info))
->save($crop_path);
} else {
InterventionImageV3::read($image_path)
->crop(...array_values($crop_info))
->save($crop_path);
}
$this->imageService->read($image_path)
->crop(...array_values($crop_info))
->save($crop_path);

// make new thumbnail
$this->lfm->generateThumbnail($image_name);
Expand Down
30 changes: 14 additions & 16 deletions src/Controllers/ResizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace UniSharp\LaravelFilemanager\Controllers;

use Intervention\Image\Facades\Image as InterventionImageV2;
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
use UniSharp\LaravelFilemanager\Services\ImageService;
use UniSharp\LaravelFilemanager\Events\ImageIsResizing;
use UniSharp\LaravelFilemanager\Events\ImageWasResized;

class ResizeController extends LfmController
{
private ImageService $imageService;

public function __construct(ImageService $imageService)
{
$this->imageService = $imageService;
parent::__construct();
}

/**
* Dipsplay image for resizing.
*
Expand All @@ -19,11 +26,7 @@ public function getResize()
$ratio = 1.0;
$image = request('img');

if (class_exists(InterventionImageV2::class)) {
$original_image = InterventionImageV2::make($this->lfm->setName($image)->path('absolute'));
} else {
$original_image = InterventionImageV3::read($this->lfm->setName($image)->path('absolute'));
}
$original_image = $this->imageService->read($this->lfm->setName($image)->path('absolute'));
$original_width = $original_image->width();
$original_height = $original_image->height();

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

event(new ImageIsResizing($image_path));

if (class_exists(InterventionImageV2::class)) {
InterventionImageV2::make($image_path)
->resize(request('dataWidth'), request('dataHeight'))
->save($resize_path);
} else {
InterventionImageV3::read($image_path)
->resize(request('dataWidth'), request('dataHeight'))
->save($resize_path);
}
$this->imageService->read($image_path)
->resize(request('dataWidth'), request('dataHeight'))
->save($resize_path);

event(new ImageWasResized($image_path));

return parent::$success_response;
Expand Down
38 changes: 32 additions & 6 deletions src/LaravelFilemanagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageManagerInterface;
use UniSharp\LaravelFilemanager\Services\ImageService;

/**
* Class LaravelFilemanagerServiceProvider.
Expand All @@ -17,24 +22,24 @@ class LaravelFilemanagerServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/lang', 'laravel-filemanager');
$this->loadTranslationsFrom(__DIR__ . '/lang', 'laravel-filemanager');

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

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

$this->publishes([
__DIR__.'/../public' => public_path('vendor/laravel-filemanager'),
__DIR__ . '/../public' => public_path('vendor/laravel-filemanager'),
], 'lfm_public');

$this->publishes([
__DIR__.'/views' => base_path('resources/views/vendor/laravel-filemanager'),
__DIR__ . '/views' => base_path('resources/views/vendor/laravel-filemanager'),
], 'lfm_view');

$this->publishes([
__DIR__.'/Handlers/LfmConfigHandler.php' => base_path('app/Handlers/LfmConfigHandler.php'),
__DIR__ . '/Handlers/LfmConfigHandler.php' => base_path('app/Handlers/LfmConfigHandler.php'),
], 'lfm_handler');

if (config('lfm.use_package_routes')) {
Expand All @@ -51,10 +56,31 @@ public function boot()
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/config/lfm.php', 'lfm-config');
$this->mergeConfigFrom(__DIR__ . '/config/lfm.php', 'lfm');

$this->app->singleton('laravel-filemanager', function () {
return true;
});

$this->app->singleton(ImageManagerInterface::class, function ($app) {
$driver = config('lfm.intervention_driver');

$driverInstance = match ($driver) {
'gd' => new GdDriver(),
'imagick' => new ImagickDriver(),
default => null,
};

if (is_null($driverInstance)) {
\Log::error("Unsupported image driver [$driver]. GdDriver will be used.");
$driverInstance = new GdDriver();
}

return new ImageManager($driverInstance);
});

$this->app->singleton(ImageService::class, function ($app) {
return new ImageService($app->make(ImageManagerInterface::class));
});
}
}
24 changes: 9 additions & 15 deletions src/LfmPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace UniSharp\LaravelFilemanager;

use Illuminate\Container\Container;
use Intervention\Image\Facades\Image as InterventionImageV2;
use Intervention\Image\Laravel\Facades\Image as InterventionImageV3;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use UniSharp\LaravelFilemanager\Services\ImageService;
use UniSharp\LaravelFilemanager\Events\FileIsUploading;
use UniSharp\LaravelFilemanager\Events\FileWasUploaded;
use UniSharp\LaravelFilemanager\Events\ImageIsUploading;
Expand All @@ -20,9 +19,12 @@ class LfmPath

private $helper;

public function __construct(Lfm $lfm)
private ImageService $imageService;

public function __construct(Lfm $lfm, ImageService $imageService)
{
$this->helper = $lfm;
$this->imageService = $imageService;
}

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

if (class_exists(InterventionImageV2::class)) {
$encoded_image = InterventionImageV2::make($original_image->get())
->fit($thumbWidth, $thumbHeight)
->stream()
->detach();
} else {
$encoded_image = InterventionImageV3::read($original_image->get())
->cover($thumbWidth, $thumbHeight)
->encodeByMediaType();
}


$encoded_image = $this->imageService->read($original_image->get())
->cover($thumbWidth, $thumbHeight)
->encodeByMediaType();

$this->storage->put($encoded_image, 'public');
}
}
28 changes: 28 additions & 0 deletions src/Services/ImageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace UniSharp\LaravelFilemanager\Services;

use Intervention\Image\Interfaces\ImageManagerInterface;
use Intervention\Image\Image;

class ImageService
{
protected ImageManagerInterface $imageManager;

public function __construct(ImageManagerInterface $imageManager)
{
$this->imageManager = $imageManager;
}

/**
* Dynamically forward method calls to the underlying ImageManagerInterface.
*/
public function __call(string $method, array $arguments)
{
if (method_exists($this->imageManager, $method)) {
return $this->imageManager->$method(...$arguments);
}

throw new \BadMethodCallException("Method {$method} does not exist on ImageService or ImageManagerInterface.");
}
}
2 changes: 2 additions & 0 deletions src/config/lfm.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@
'php_ini_overrides' => [
'memory_limit' => '256M',
],

'intervention_driver' => 'gd', // options: gd, imagick
];