1
|
|
|
<?php |
2
|
|
|
namespace CbCaio\ImgAttacher\Processors; |
3
|
|
|
|
4
|
|
|
use CbCaio\ImgAttacher\Contracts\AttacherImageContract; |
5
|
|
|
use CbCaio\ImgAttacher\Contracts\ImageProcessorContract; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Intervention\Image\Image; |
8
|
|
|
use Intervention\Image\ImageManager; |
9
|
|
|
|
10
|
|
|
abstract class AbstractImageProcessor implements ImageProcessorContract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ImageManager |
14
|
|
|
*/ |
15
|
|
|
protected $imageManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param ImageManager $imageManager |
19
|
|
|
*/ |
20
|
|
|
public function __construct(ImageManager $imageManager) |
21
|
|
|
{ |
22
|
|
|
$this->imageManager = $imageManager; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Creates an Intervention\Image from UploadedFile |
27
|
|
|
* |
28
|
|
|
* @return Image |
29
|
|
|
*/ |
30
|
|
|
public function createImageFromAttacherImage(AttacherImageContract $attacherImage) |
31
|
|
|
{ |
32
|
|
|
return $this->imageManager->make($attacherImage->getUploadedFile()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param AttacherImageContract $attacherImage |
37
|
|
|
* @param array $processing_styles |
38
|
|
|
* @return Collection|null |
39
|
|
|
*/ |
40
|
|
|
public function applyStyles(AttacherImageContract $attacherImage, $processing_styles) |
41
|
|
|
{ |
42
|
|
|
$style_routine = $this->getProcessingStyleRoutine($attacherImage, $processing_styles); |
43
|
|
|
$styles_to_be_applied = empty($style_routine) ? head($processing_styles) : $style_routine; |
44
|
|
|
|
45
|
|
|
if (empty($styles_to_be_applied)) |
46
|
|
|
{ |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$collection_of_images = new Collection; |
51
|
|
|
|
52
|
|
|
foreach ($styles_to_be_applied as $style_name => $method) |
53
|
|
|
{ |
54
|
|
|
$image = $this->applyStyle($attacherImage, $method); |
55
|
|
|
$collection_of_images->put($style_name,$image); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $collection_of_images; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param AttacherImageContract $attacherImage |
63
|
|
|
* @param callable $style |
64
|
|
|
* @return Image |
65
|
|
|
*/ |
66
|
|
|
public function applyStyle(AttacherImageContract $attacherImage, Callable $style) |
67
|
|
|
{ |
68
|
|
|
$image = $this->createImageFromAttacherImage($attacherImage); |
69
|
|
|
|
70
|
|
|
$processed = $style($image); |
71
|
|
|
|
72
|
|
|
return (is_null($processed)) ? $image : $processed; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param AttacherImageContract $attacherImage |
77
|
|
|
* @param array $processing_styles |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getProcessingStyleRoutine(AttacherImageContract $attacherImage, array $processing_styles) |
81
|
|
|
{ |
82
|
|
|
return array_get($processing_styles, $attacherImage->getProcessingStyleRoutine(), []); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |