Skip to content

Commit 376ad24

Browse files
author
ChenCatherine
committed
Fix crop and resize function not work issue. Add event for cropping and resizing.
1 parent 73f60ed commit 376ad24

File tree

9 files changed

+113
-15
lines changed

9 files changed

+113
-15
lines changed

docs/events.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@
77
* Unisharp\Laravelfilemanager\Events\ImageWasDeleted
88
* Unisharp\Laravelfilemanager\Events\FolderIsRenaming
99
* Unisharp\Laravelfilemanager\Events\FolderWasRenamed
10+
* Unisharp\Laravelfilemanager\Events\ImageIsResizing
11+
* Unisharp\Laravelfilemanager\Events\ImageWasResize
12+
* Unisharp\Laravelfilemanager\Events\ImageIsCropping
13+
* Unisharp\Laravelfilemanager\Events\ImageWasCropped
14+
1015

1116
## How to use
1217
* To use events you can add a listener to listen to the events.
1318

1419
Snippet for `EventServiceProvider`
15-
20+
1621
```php
1722
protected $listen = [
1823
ImageWasUploaded::class => [
1924
UploadListener::class,
2025
],
2126
];
2227
```
23-
28+
2429
The `UploadListener` will look like:
25-
30+
2631
```php
2732
class UploadListener
2833
{
@@ -33,7 +38,7 @@
3338
call_user_func([$this, $method], $event);
3439
}
3540
}
36-
41+
3742
public function onImageWasUploaded(ImageWasUploaded $event)
3843
{
3944
$path = $event->path();
@@ -45,45 +50,45 @@
4550
* Or by using Event Subscribers
4651

4752
Snippet for `EventServiceProvider`
48-
53+
4954
```php
5055
protected $subscribe = [
5156
UploadListener::class
5257
];
5358
```
54-
59+
5560
The `UploadListener` will look like:
56-
61+
5762
```php
5863
public function subscribe($events)
5964
{
6065
$events->listen('*', UploadListener::class);
6166
}
62-
67+
6368
public function handle($event)
6469
{
6570
$method = 'on'.class_basename($event);
6671
if (method_exists($this, $method)) {
6772
call_user_func([$this, $method], $event);
6873
}
6974
}
70-
75+
7176
public function onImageWasUploaded(ImageWasUploaded $event)
7277
{
7378
$path = $event->path();
7479
// your code, for example resizing and cropping
7580
}
76-
81+
7782
public function onImageWasRenamed(ImageWasRenamed $event)
7883
{
7984
// image was renamed
8085
}
81-
86+
8287
public function onImageWasDeleted(ImageWasDeleted $event)
8388
{
8489
// image was deleted
8590
}
86-
91+
8792
public function onFolderWasRenamed(FolderWasRenamed $event)
8893
{
8994
// folder was renamed

src/Events/ImageIsCropping.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Unisharp\Laravelfilemanager\Events;
4+
5+
class ImageIsCropping
6+
{
7+
private $path;
8+
9+
public function __construct($path)
10+
{
11+
$this->path = $path;
12+
}
13+
14+
/**
15+
* @return string
16+
*/
17+
public function path()
18+
{
19+
return $this->path;
20+
}
21+
}

src/Events/ImageIsResizing.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Unisharp\Laravelfilemanager\Events;
4+
5+
class ImageIsResizing
6+
{
7+
private $path;
8+
9+
public function __construct($path)
10+
{
11+
$this->path = $path;
12+
}
13+
14+
/**
15+
* @return string
16+
*/
17+
public function path()
18+
{
19+
return $this->path;
20+
}
21+
}

src/Events/ImageWasCropped.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Unisharp\Laravelfilemanager\Events;
4+
5+
class ImageWasCropped
6+
{
7+
private $path;
8+
9+
public function __construct($path)
10+
{
11+
$this->path = $path;
12+
}
13+
14+
/**
15+
* @return string
16+
*/
17+
public function path()
18+
{
19+
return $this->path;
20+
}
21+
}

src/Events/ImageWasResize.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Unisharp\Laravelfilemanager\Events;
4+
5+
class ImageWasResize
6+
{
7+
private $path;
8+
9+
public function __construct($path)
10+
{
11+
$this->path = $path;
12+
}
13+
14+
/**
15+
* @return string
16+
*/
17+
public function path()
18+
{
19+
return $this->path;
20+
}
21+
}

src/controllers/CropController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use Unisharp\Laravelfilemanager\controllers\Controller;
44
use Intervention\Image\Facades\Image;
5+
use Unisharp\Laravelfilemanager\Events\ImageIsCropping;
6+
use Unisharp\Laravelfilemanager\Events\ImageWasCropped;
57

68
/**
79
* Class CropController
@@ -40,10 +42,12 @@ public function getCropimage()
4042
Image::make($image_path)
4143
->crop($dataWidth, $dataHeight, $dataX, $dataY)
4244
->save($image_path);
45+
event(new ImageIsCropping($image_path));
4346

4447
// make new thumbnail
4548
Image::make($image_path)
4649
->fit(200, 200)
4750
->save(parent::getThumbPath(parent::getName($image_path)));
51+
event(new ImageWasCropped($image_path));
4852
}
4953
}

src/controllers/ResizeController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php namespace Unisharp\Laravelfilemanager\controllers;
22

33
use Intervention\Image\Facades\Image;
4+
use Unisharp\Laravelfilemanager\Events\ImageIsResizing;
5+
use Unisharp\Laravelfilemanager\Events\ImageWasResize;
46

57
/**
68
* Class ResizeController
@@ -58,9 +60,12 @@ public function performResize()
5860
$dataY = request('dataY');
5961
$height = request('dataHeight');
6062
$width = request('dataWidth');
63+
$image_path = public_path() . $img;
6164

6265
try {
63-
Image::make(public_path() . $img)->resize($width, $height)->save();
66+
event(new ImageIsResizing($image_path));
67+
Image::make($image_path)->resize($width, $height)->save();
68+
event(new ImageWasResize($image_path));
6469
return $this->success_response;
6570
} catch (Exception $e) {
6671
return "width : " . $width . " height: " . $height;

src/views/crop.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function performCrop() {
8585
dataType: "text",
8686
url: "{{ route('unisharp.lfm.getCropimage') }}",
8787
data: {
88-
img: '{{ $img }}',
88+
img: '{{ parse_url($img, PHP_URL_PATH) }}',
8989
working_dir: $("#working_dir").val(),
9090
dataX: $("#dataX").val(),
9191
dataY: $("#dataY").val(),

src/views/resize.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function doResize() {
7575
dataType: "text",
7676
url: "{{ route('unisharp.lfm.performResize') }}",
7777
data: {
78-
img: '{{ $img }}',
78+
img: '{{ parse_url($img, PHP_URL_PATH) }}',
7979
working_dir: $("#working_dir").val(),
8080
dataX: $("#dataX").val(),
8181
dataY: $("#dataY").val(),

0 commit comments

Comments
 (0)