1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace HustleWorks\Chute; |
5
|
|
|
|
6
|
|
|
use HustleWorks\Chute\DTO\ImageConfiguration; |
7
|
|
|
use HustleWorks\Chute\DTO\ImageRecord; |
8
|
|
|
use HustleWorks\Chute\Contracts\ImageRepositoryInterface; |
9
|
|
|
use HustleWorks\Chute\Contracts\ImageTransformationRepositoryInterface; |
10
|
|
|
use HustleWorks\Chute\Contracts\StorageInterface; |
11
|
|
|
use HustleWorks\Chute\ImageEditor; |
12
|
|
|
|
13
|
|
|
abstract class ImageProcessor |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var StorageInterface |
17
|
|
|
*/ |
18
|
|
|
private $storage; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ImageRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $image_repo; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ImageTransformationRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $image_transformation_repo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ImageProcessor constructor. |
32
|
|
|
* |
33
|
|
|
* @param StorageInterface $storage |
34
|
|
|
* @param $image_repo |
35
|
|
|
* @param $image_transformation_repo |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
StorageInterface $storage, |
39
|
|
|
ImageRepositoryInterface $image_repo, |
40
|
|
|
ImageTransformationRepositoryInterface $image_transformation_repo |
41
|
|
|
) |
42
|
|
|
{ |
43
|
|
|
$this->storage = $storage; |
44
|
|
|
$this->image_repo = $image_repo; |
45
|
|
|
$this->image_transformation_repo = $image_transformation_repo; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Handle Processing |
50
|
|
|
* |
51
|
|
|
* @param ImageRecord $record |
52
|
|
|
* @param ImageConfiguration $config |
53
|
|
|
* @return StandardServiceResponse |
54
|
|
|
*/ |
55
|
|
|
public function handle(ImageRecord $record, ImageConfiguration $config) |
56
|
|
|
{ |
57
|
|
|
/* build path */ |
58
|
|
|
$path_to_record = "$record->path/$record->uuid/$record->filename"; |
59
|
|
|
|
60
|
|
|
/* get file from storage */ |
61
|
|
|
$image = $this->storage->get($record->disk, $path_to_record); |
|
|
|
|
62
|
|
|
|
63
|
|
|
/* move file to primary storage if not already there */ |
64
|
|
|
if ($record->disk !== $config->primary_disk) { |
|
|
|
|
65
|
|
|
$this->storage->move($path_to_record, $path_to_record, $record->disk, $config->primary_disk); |
|
|
|
|
66
|
|
|
$this->image_repo->update($record, ['disk' => $config->primary_disk]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* create transformation for each size */ |
70
|
|
|
$transformation_records = $this->_createTransformations($image, $config, $record); |
71
|
|
|
|
72
|
|
|
$this->image_repo->update($record, ['status' => 'complete']); |
73
|
|
|
|
74
|
|
|
return new StandardServiceResponse( |
75
|
|
|
true, |
76
|
|
|
['image' => $record, 'image_transformations' => $transformation_records], |
77
|
|
|
'Image processing completed successfully' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create Transformations |
83
|
|
|
* |
84
|
|
|
* @param $image |
85
|
|
|
* @param ImageConfiguration $config |
86
|
|
|
* @param ImageRecord $image_record |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
private function _createTransformations($image, ImageConfiguration $config, ImageRecord $image_record) |
90
|
|
|
{ |
91
|
|
|
foreach ($config->sizes as $size) { |
92
|
|
|
/* create filename */ |
93
|
|
|
$fn = $size->prefix . |
94
|
|
|
str_replace(".$image_record->extension", '', $image_record->filename) . |
95
|
|
|
"$size->suffix.$image_record->extension"; |
96
|
|
|
|
97
|
|
|
/* initialize fluent image editor */ |
98
|
|
|
$editor = new ImageEditor($image); |
99
|
|
|
|
100
|
|
|
/* create transformation */ |
101
|
|
|
$transformed_image = $editor->resize($size->width, $size->height)->setQuality($size->quality)->getStream(); |
102
|
|
|
|
103
|
|
|
/* store on disk */ |
104
|
|
|
$size_on_disk = $this->storage->put($transformed_image, $config->primary_disk, "$image_record->path/$image_record->uuid", $fn); |
105
|
|
|
|
106
|
|
|
/* create record */ |
107
|
|
|
$transformation_records[] = $this->image_transformation_repo->create([ |
|
|
|
|
108
|
|
|
'name' => $size->name, |
109
|
|
|
'filename' => $fn, |
110
|
|
|
'disk' => $config->primary_disk, |
111
|
|
|
'width' => $size->width ?? round($size->height / $image_record->height * $image_record->width), |
112
|
|
|
'height' => $size->height ?? round($size->width / $image_record->width * $image_record->height), |
113
|
|
|
'quality' => $size->quality, |
114
|
|
|
'size' => $size_on_disk, |
115
|
|
|
], $image_record); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $transformation_records ?? []; |
119
|
|
|
} |
120
|
|
|
} |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.