1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2025 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Jobs |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Media\Scale; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Image rescale job controller. |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Jobs |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Jobs\Base |
22
|
|
|
implements \Aimeos\Controller\Jobs\Iface |
23
|
|
|
{ |
24
|
|
|
/** controller/jobs/media/scale/name |
25
|
|
|
* Class name of the used media scale job controller implementation |
26
|
|
|
* |
27
|
|
|
* Each default job controller can be replace by an alternative imlementation. |
28
|
|
|
* To use this implementation, you have to set the last part of the class |
29
|
|
|
* name as configuration value so the controller factory knows which class it |
30
|
|
|
* has to instantiate. |
31
|
|
|
* |
32
|
|
|
* For example, if the name of the default class is |
33
|
|
|
* |
34
|
|
|
* \Aimeos\Controller\Jobs\Media\Scale\Standard |
35
|
|
|
* |
36
|
|
|
* and you want to replace it with your own version named |
37
|
|
|
* |
38
|
|
|
* \Aimeos\Controller\Jobs\Media\Scale\Myscaler |
39
|
|
|
* |
40
|
|
|
* then you have to set the this configuration option: |
41
|
|
|
* |
42
|
|
|
* controller/media/scale/name = Myscaler |
43
|
|
|
* |
44
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
45
|
|
|
* so take care that the configuration value is exactly named like the last |
46
|
|
|
* part of the class name. |
47
|
|
|
* |
48
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
49
|
|
|
* characters are possible! You should always start the last part of the class |
50
|
|
|
* name with an upper case character and continue only with lower case characters |
51
|
|
|
* or numbers. Avoid chamel case names like "Myscaler"! |
52
|
|
|
* |
53
|
|
|
* @param string Last part of the class name |
54
|
|
|
* @since 2017.01 |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
/** controller/jobs/media/scale/decorators/excludes |
58
|
|
|
* Excludes decorators added by the "common" option from the media scale controllers |
59
|
|
|
* |
60
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
61
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
62
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
63
|
|
|
* modify what is returned to the caller. |
64
|
|
|
* |
65
|
|
|
* This option allows you to remove a decorator added via |
66
|
|
|
* "controller/jobs/common/decorators/default" before they are wrapped |
67
|
|
|
* around the job controller. |
68
|
|
|
* |
69
|
|
|
* controller/jobs/media/scale/decorators/excludes = array( 'decorator1' ) |
70
|
|
|
* |
71
|
|
|
* This would remove the decorator named "decorator1" from the list of |
72
|
|
|
* common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via |
73
|
|
|
* "controller/jobs/common/decorators/default" to this job controller. |
74
|
|
|
* |
75
|
|
|
* @param array List of decorator names |
76
|
|
|
* @since 2015.09 |
77
|
|
|
* @see controller/jobs/common/decorators/default |
78
|
|
|
* @see controller/jobs/media/scale/decorators/global |
79
|
|
|
* @see controller/jobs/media/scale/decorators/local |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
/** controller/jobs/media/scale/decorators/global |
83
|
|
|
* Adds a list of globally available decorators only to the media scale controllers |
84
|
|
|
* |
85
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
86
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
87
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
88
|
|
|
* modify what is returned to the caller. |
89
|
|
|
* |
90
|
|
|
* This option allows you to wrap global decorators |
91
|
|
|
* ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller. |
92
|
|
|
* |
93
|
|
|
* controller/jobs/media/scale/decorators/global = array( 'decorator1' ) |
94
|
|
|
* |
95
|
|
|
* This would add the decorator named "decorator1" defined by |
96
|
|
|
* "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to this job controller. |
97
|
|
|
* |
98
|
|
|
* @param array List of decorator names |
99
|
|
|
* @since 2015.09 |
100
|
|
|
* @see controller/jobs/common/decorators/default |
101
|
|
|
* @see controller/jobs/media/scale/decorators/excludes |
102
|
|
|
* @see controller/jobs/media/scale/decorators/local |
103
|
|
|
*/ |
104
|
|
|
|
105
|
|
|
/** controller/jobs/media/scale/decorators/local |
106
|
|
|
* Adds a list of local decorators only to the media scale controllers |
107
|
|
|
* |
108
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
109
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
110
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
111
|
|
|
* modify what is returned to the caller. |
112
|
|
|
* |
113
|
|
|
* This option allows you to wrap local decorators |
114
|
|
|
* ("\Aimeos\Controller\Jobs\Media\Scale\Decorator\*") around this job controller. |
115
|
|
|
* |
116
|
|
|
* controller/jobs/media/scale/decorators/local = array( 'decorator2' ) |
117
|
|
|
* |
118
|
|
|
* This would add the decorator named "decorator2" defined by |
119
|
|
|
* "\Aimeos\Controller\Jobs\Media\Scale\Decorator\Decorator2" only to this job |
120
|
|
|
* controller. |
121
|
|
|
* |
122
|
|
|
* @param array List of decorator names |
123
|
|
|
* @since 2015.09 |
124
|
|
|
* @see controller/jobs/common/decorators/default |
125
|
|
|
* @see controller/jobs/media/scale/decorators/excludes |
126
|
|
|
* @see controller/jobs/media/scale/decorators/global |
127
|
|
|
*/ |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the localized name of the job. |
132
|
|
|
* |
133
|
|
|
* @return string Name of the job |
134
|
|
|
*/ |
135
|
|
|
public function getName() : string |
136
|
|
|
{ |
137
|
|
|
return $this->context()->translate( 'controller/jobs', 'Rescale product images' ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the localized description of the job. |
143
|
|
|
* |
144
|
|
|
* @return string Description of the job |
145
|
|
|
*/ |
146
|
|
|
public function getDescription() : string |
147
|
|
|
{ |
148
|
|
|
return $this->context()->translate( 'controller/jobs', 'Rescales product images to the new sizes' ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Executes the job. |
154
|
|
|
* |
155
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
156
|
|
|
*/ |
157
|
|
|
public function run() |
158
|
|
|
{ |
159
|
|
|
$context = $this->context(); |
160
|
|
|
$process = $context->process(); |
161
|
|
|
$manager = \Aimeos\MShop::create( $context, 'media' ); |
162
|
|
|
|
163
|
|
|
$search = $manager->filter()->order( 'media.id' ); |
164
|
|
|
$search->add( $search->and( [ |
165
|
|
|
$search->compare( '==', 'media.siteid', $context->locale()->getSiteId() ), |
166
|
|
|
$search->compare( '==', 'media.domain', ['attribute', 'catalog', 'product', 'service', 'supplier'] ), |
167
|
|
|
$search->compare( '=~', 'media.mimetype', 'image/' ), |
168
|
|
|
] ) ); |
169
|
|
|
|
170
|
|
|
$fcn = function( \Aimeos\MShop\ContextIface $context, \Aimeos\Map $items ) { |
171
|
|
|
$this->rescale( $context, $items ); |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
while( !( $items = $manager->search( ( clone $search )->add( 'media.id', '>', $lastId ?? 0 ) ) )->isEmpty() ) |
175
|
|
|
{ |
176
|
|
|
$process->start( $fcn, [$context, $items] ); |
177
|
|
|
$lastId = $items->last()->getId(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$process->wait(); |
181
|
|
|
|
182
|
|
|
$context->cache()->clear(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Recreates the preview images for the given media items |
188
|
|
|
* |
189
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
190
|
|
|
* @param \Aimeos\Map $items List of media items implementing \Aimeos\MShop\Media\Item\Iface |
191
|
|
|
*/ |
192
|
|
|
protected function rescale( \Aimeos\MShop\ContextIface $context, \Aimeos\Map $items ) |
193
|
|
|
{ |
194
|
|
|
$logger = $context->logger(); |
195
|
|
|
$manager = \Aimeos\MShop::create( $context, 'media' ); |
196
|
|
|
|
197
|
|
|
/** controller/jobs/media/scale/force |
198
|
|
|
* Enforce rescaling all images |
199
|
|
|
* |
200
|
|
|
* By default, all images are rescaled when executing the job controller. |
201
|
|
|
* You can limit scaling to new images only (if mtime of the file is newer |
202
|
|
|
* than the mtime of the media record) by setting this configuration option |
203
|
|
|
* to false or 0 |
204
|
|
|
* |
205
|
|
|
* @param bool True to rescale all images, false for new ones only |
206
|
|
|
* @since 2019.10 |
207
|
|
|
*/ |
208
|
|
|
$force = $context->config()->get( 'controller/jobs/media/scale/force', true ); |
209
|
|
|
|
210
|
|
|
foreach( $items as $item ) |
211
|
|
|
{ |
212
|
|
|
try { |
213
|
|
|
$manager->save( $manager->scale( $item, $force ) ); |
214
|
|
|
} catch( \Exception $e ) { |
215
|
|
|
$msg = sprintf( 'Scaling media item "%1$s" failed: %2$s', $item->getId(), $e->getMessage() ); |
216
|
|
|
$logger->error( $msg, 'media/scale' ); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|