1
|
|
|
<?php |
2
|
|
|
namespace CbCaio\ImgAttacher; |
3
|
|
|
|
4
|
|
|
use CbCaio\ImgAttacher\Contracts\AttacherImageContract; |
5
|
|
|
use CbCaio\ImgAttacher\Managers\FileManager; |
6
|
|
|
use CbCaio\ImgAttacher\Managers\FilePathManager; |
7
|
|
|
use CbCaio\ImgAttacher\Processors\ImageProcessor; |
8
|
|
|
use Intervention\Image\Image; |
9
|
|
|
|
10
|
|
|
class AttacherManager |
11
|
|
|
{ |
12
|
|
|
protected $processing_styles_routine; |
13
|
|
|
protected $base_url; |
14
|
|
|
protected $path_to_save; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->processing_styles_routine = app('config')->get('img-attacher')['processing_styles_routines']; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function writeImage(AttacherImageContract $attacherImage) |
25
|
|
|
{ |
26
|
|
|
$imageProcessor = $this->getImageProcessor(); |
27
|
|
|
|
28
|
|
|
$images_to_save = $imageProcessor->applyStyles($attacherImage, $this->getProcessingStylesRoutines()); |
29
|
|
|
|
30
|
|
|
if (is_null($images_to_save)) |
31
|
|
|
{ |
32
|
|
|
return FALSE; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$needToUpdate = $attacherImage->hasDifferentFileName(); |
36
|
|
|
|
37
|
|
|
if ($needToUpdate) |
38
|
|
|
{ |
39
|
|
|
$this->getFileManager()->updateMany($images_to_save, $attacherImage); |
40
|
|
|
} else |
41
|
|
|
{ |
42
|
|
|
$this->getFileManager()->saveMany($images_to_save, $attacherImage); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return TRUE; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AttacherImageContract $attacherImage |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function deleteImages(AttacherImageContract $attacherImage, $style = NULL) |
53
|
|
|
{ |
54
|
|
|
$path = $attacherImage->getDeletePath($style); |
55
|
|
|
|
56
|
|
|
return $this->getFileManager()->delete($path); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return FileManager |
61
|
|
|
*/ |
62
|
|
|
public function getFileManager() |
63
|
|
|
{ |
64
|
|
|
return app('img-attacher.FileManager'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return ImageProcessor |
69
|
|
|
*/ |
70
|
|
|
public function getImageProcessor() |
71
|
|
|
{ |
72
|
|
|
return app('img-attacher.ImageProcessor'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return FilePathManager |
77
|
|
|
*/ |
78
|
|
|
public function getFilePathManager() |
79
|
|
|
{ |
80
|
|
|
return app('img-attacher.FilePathManager'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getProcessingStylesRoutines() |
87
|
|
|
{ |
88
|
|
|
return $this->processing_styles_routine; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getBaseUrl() |
95
|
|
|
{ |
96
|
|
|
return $this->getFilePathManager()->getBaseUrl(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getPathToSave() |
103
|
|
|
{ |
104
|
|
|
return $this->getFilePathManager()->getPath(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|