-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathImageHelper.php
More file actions
179 lines (153 loc) · 4.72 KB
/
ImageHelper.php
File metadata and controls
179 lines (153 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Cloudinary\Cloudinary\Plugin;
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\Image\ImageFactory;
use Cloudinary\Cloudinary\Core\Image\Transformation;
use Cloudinary\Cloudinary\Core\Image\Transformation\Crop;
use Cloudinary\Cloudinary\Core\Image\Transformation\Dimensions;
use Cloudinary\Cloudinary\Core\UrlGenerator;
use Cloudinary\Cloudinary\Model\Transformation as TransformationModel;
use Cloudinary\Cloudinary\Model\TransformationFactory;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Helper\Image as CatalogImageHelper;
class ImageHelper
{
/**
* @var ImageFactory
*/
private $imageFactory;
/**
* @var UrlGenerator
*/
private $urlGenerator;
/**
* @var ProductInterface
*/
private $product;
/**
* @var Dimensions
*/
private $dimensions;
/**
* @var ConfigurationInterface
*/
private $configuration;
/**
* @var string
*/
private $imageFile;
/**
* @var bool
*/
private $keepFrame;
/**
* @var TransformationModel
*/
private $transformationModel;
/**
* @param ImageFactory $imageFactory
* @param UrlGenerator $urlGenerator
* @param ConfigurationInterface $configuration
*/
public function __construct(
ImageFactory $imageFactory,
UrlGenerator $urlGenerator,
ConfigurationInterface $configuration,
TransformationFactory $transformationFactory
) {
$this->imageFactory = $imageFactory;
$this->urlGenerator = $urlGenerator;
$this->configuration = $configuration;
$this->transformationModel = $transformationFactory->create();
$this->dimensions = null;
$this->imageFile = null;
}
/**
* @param CatalogImageHelper $helper
* @param ProductInterface $product
* @param string $imageId
* @param array $attributes
*
* @return array
*/
public function beforeInit(CatalogImageHelper $helper, $product, $imageId, $attributes = [])
{
$this->product = $product;
$this->dimensions = null;
$this->imageFile = null;
$this->keepFrame = true;
return [$product, $imageId, $attributes];
}
/**
* @param CatalogImageHelper $helper
* @param string $file
*
* @return string[]
*/
public function beforeSetImageFile(CatalogImageHelper $helper, $file)
{
$this->imageFile = $file;
return [$file];
}
/**
* @param CatalogImageHelper $helper
* @param int $width
* @param int $height
*
* @return array
*/
public function beforeResize(CatalogImageHelper $helper, $width, $height = null)
{
$this->dimensions = Dimensions::fromWidthAndHeight($width, $height);
return [$width, $height];
}
/**
* @param CatalogImageHelper $helper
* @param bool $flag
*/
public function beforeKeepFrame(CatalogImageHelper $helper, $flag)
{
$this->keepFrame = (bool)$flag;
}
/**
* @param CatalogImageHelper $helper
* @param \Closure $originalMethod
*
* @return string
*/
public function aroundGetUrl(CatalogImageHelper $helper, \Closure $originalMethod)
{
if (!$this->configuration->isEnabled()) {
return $originalMethod();
}
$imagePath = $this->imageFile ?: $this->product->getData($helper->getType());
$image = $this->imageFactory->build(sprintf('catalog/product%s', $imagePath), $originalMethod);
$transformations = $this->createTransformation($helper);
if ($this->configuration->isEnabledProductFreeTransformations()) {
$transformations = $this->transformationModel->addFreeformTransformationForImage(
$transformations,
$imagePath
);
}
return $this->urlGenerator->generateFor(
$image,
$transformations
);
}
/**
* @param CatalogImageHelper $helper
* @return Transformation
*/
private function createTransformation(CatalogImageHelper $helper)
{
$dimensions = $this->dimensions ?: Dimensions::fromWidthAndHeight($helper->getWidth(), $helper->getHeight());
$transform = $this->configuration->getDefaultTransformation()->withDimensions($dimensions);
if ($this->keepFrame) {
$transform->withCrop(Crop::lpad())
->withDimensions(Dimensions::squareMissingDimension($dimensions));
} else {
$transform->withCrop(Crop::limit());
}
return $transform;
}
}