1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2020 |
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
|
|
|
/** |
25
|
|
|
* Returns the localized name of the job. |
26
|
|
|
* |
27
|
|
|
* @return string Name of the job |
28
|
|
|
*/ |
29
|
|
|
public function getName() : string |
30
|
|
|
{ |
31
|
|
|
return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Rescale product images' ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the localized description of the job. |
37
|
|
|
* |
38
|
|
|
* @return string Description of the job |
39
|
|
|
*/ |
40
|
|
|
public function getDescription() : string |
41
|
|
|
{ |
42
|
|
|
return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Rescales product images to the new sizes' ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Executes the job. |
48
|
|
|
* |
49
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
50
|
|
|
*/ |
51
|
|
|
public function run() |
52
|
|
|
{ |
53
|
|
|
$context = $this->getContext(); |
54
|
|
|
$process = $context->getProcess(); |
55
|
|
|
$manager = \Aimeos\MShop::create( $context, 'media' ); |
56
|
|
|
|
57
|
|
|
$search = $manager->filter(); |
58
|
|
|
$expr = array( |
59
|
|
|
$search->compare( '==', 'media.domain', ['attribute', 'catalog', 'product', 'service', 'supplier'] ), |
|
|
|
|
60
|
|
|
$search->compare( '=~', 'media.mimetype', 'image/' ), |
61
|
|
|
); |
62
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
|
|
|
|
63
|
|
|
$search->setSortations( array( $search->sort( '+', 'media.id' ) ) ); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$start = 0; |
66
|
|
|
|
67
|
|
|
$fcn = function( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Map $items ) { |
68
|
|
|
$this->rescale( $context, $items ); |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
do |
72
|
|
|
{ |
73
|
|
|
$search->slice( $start ); |
|
|
|
|
74
|
|
|
$items = $manager->search( $search ); |
75
|
|
|
|
76
|
|
|
$process->start( $fcn, [$context, $items] ); |
77
|
|
|
|
78
|
|
|
$count = count( $items ); |
79
|
|
|
$start += $count; |
80
|
|
|
} |
81
|
|
|
while( $count === $search->getLimit() ); |
82
|
|
|
|
83
|
|
|
$process->wait(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Recreates the preview images for the given media items |
89
|
|
|
* |
90
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
91
|
|
|
* @param \Aimeos\Map $items List of media items implementing \Aimeos\MShop\Media\Item\Iface |
92
|
|
|
*/ |
93
|
|
|
protected function rescale( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Map $items ) |
94
|
|
|
{ |
95
|
|
|
$logger = $context->getLogger(); |
96
|
|
|
$manager = \Aimeos\MShop::create( $context, 'media' ); |
97
|
|
|
$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context ); |
98
|
|
|
|
99
|
|
|
/** controller/jobs/media/scale/force |
100
|
|
|
* Enforce rescaling all images |
101
|
|
|
* |
102
|
|
|
* By default, all images are rescaled when executing the job controller. |
103
|
|
|
* You can limit scaling to new images only (if mtime of the file is newer |
104
|
|
|
* than the mtime of the media record) by setting this configuration option |
105
|
|
|
* to false or 0 |
106
|
|
|
* |
107
|
|
|
* @param bool True to rescale all images, false for new ones only |
108
|
|
|
* @category Developer |
109
|
|
|
* @category User |
110
|
|
|
* @since 2019.10 |
111
|
|
|
*/ |
112
|
|
|
$force = $context->getConfig()->get( 'controller/jobs/media/scale/force', true ); |
113
|
|
|
|
114
|
|
|
foreach( $items as $item ) |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$manager->save( $cntl->scale( $item, 'fs-media', $force ) ); |
|
|
|
|
118
|
|
|
} catch( \Exception $e ) { |
119
|
|
|
$logger->log( sprintf( 'Scaling media item "%1$s" failed: %2$s', $item->getId(), $e->getMessage() ) ); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|