Skip to content

Commit 5e17b04

Browse files
authored
Merge pull request #3467 from magento-tango/PR-1911
[tango] PR
2 parents 9845b2b + e7142cc commit 5e17b04

File tree

28 files changed

+621
-96
lines changed

28 files changed

+621
-96
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backend\Block\DataProviders;
9+
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
use Magento\Backend\Model\Image\UploadResizeConfigInterface;
12+
13+
/**
14+
* Provides additional data for image uploader
15+
*/
16+
class ImageUploadConfig implements ArgumentInterface
17+
{
18+
/**
19+
* @var UploadResizeConfigInterface
20+
*/
21+
private $imageUploadConfig;
22+
23+
/**
24+
* @param UploadResizeConfigInterface $imageUploadConfig
25+
*/
26+
public function __construct(UploadResizeConfigInterface $imageUploadConfig)
27+
{
28+
$this->imageUploadConfig = $imageUploadConfig;
29+
}
30+
31+
/**
32+
* Get image resize configuration
33+
*
34+
* @return int
35+
*/
36+
public function getIsResizeEnabled(): int
37+
{
38+
return (int)$this->imageUploadConfig->isResizeEnabled();
39+
}
40+
}

app/code/Magento/Backend/Block/Media/Uploader.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Serialize\Serializer\Json;
1212
use Magento\Framework\Image\Adapter\UploadConfigInterface;
13+
use Magento\Backend\Model\Image\UploadResizeConfigInterface;
1314

1415
/**
1516
* Adminhtml media library uploader
@@ -38,8 +39,15 @@ class Uploader extends \Magento\Backend\Block\Widget
3839
*/
3940
private $jsonEncoder;
4041

42+
/**
43+
* @var UploadResizeConfigInterface
44+
*/
45+
private $imageUploadConfig;
46+
4147
/**
4248
* @var UploadConfigInterface
49+
* @deprecated
50+
* @see \Magento\Backend\Model\Image\UploadResizeConfigInterface
4351
*/
4452
private $imageConfig;
4553

@@ -49,18 +57,22 @@ class Uploader extends \Magento\Backend\Block\Widget
4957
* @param array $data
5058
* @param Json $jsonEncoder
5159
* @param UploadConfigInterface $imageConfig
60+
* @param UploadResizeConfigInterface $imageUploadConfig
5261
*/
5362
public function __construct(
5463
\Magento\Backend\Block\Template\Context $context,
5564
\Magento\Framework\File\Size $fileSize,
5665
array $data = [],
5766
Json $jsonEncoder = null,
58-
UploadConfigInterface $imageConfig = null
67+
UploadConfigInterface $imageConfig = null,
68+
UploadResizeConfigInterface $imageUploadConfig = null
5969
) {
6070
$this->_fileSizeService = $fileSize;
6171
$this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
62-
$this->imageConfig = $imageConfig ?: ObjectManager::getInstance()->get(UploadConfigInterface::class);
63-
72+
$this->imageConfig = $imageConfig
73+
?: ObjectManager::getInstance()->get(UploadConfigInterface::class);
74+
$this->imageUploadConfig = $imageUploadConfig
75+
?: ObjectManager::getInstance()->get(UploadResizeConfigInterface::class);
6476
parent::__construct($context, $data);
6577
}
6678

@@ -111,7 +123,7 @@ public function getFileSizeService()
111123
*/
112124
public function getImageUploadMaxWidth()
113125
{
114-
return $this->imageConfig->getMaxWidth();
126+
return $this->imageUploadConfig->getMaxWidth();
115127
}
116128

117129
/**
@@ -121,7 +133,7 @@ public function getImageUploadMaxWidth()
121133
*/
122134
public function getImageUploadMaxHeight()
123135
{
124-
return $this->imageConfig->getMaxHeight();
136+
return $this->imageUploadConfig->getMaxHeight();
125137
}
126138

127139
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backend\Model\Image;
9+
10+
/**
11+
* Image uploader config provider.
12+
*/
13+
class UploadResizeConfig implements UploadResizeConfigInterface
14+
{
15+
/**
16+
* Config path for the maximal image width value
17+
*/
18+
const XML_PATH_MAX_WIDTH_IMAGE = 'system/upload_configuration/max_width';
19+
20+
/**
21+
* Config path for the maximal image height value
22+
*/
23+
const XML_PATH_MAX_HEIGHT_IMAGE = 'system/upload_configuration/max_height';
24+
25+
/**
26+
* Config path for the maximal image height value
27+
*/
28+
const XML_PATH_ENABLE_RESIZE = 'system/upload_configuration/enable_resize';
29+
30+
/**
31+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
32+
*/
33+
private $config;
34+
35+
/**
36+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
37+
*/
38+
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $config)
39+
{
40+
$this->config = $config;
41+
}
42+
43+
/**
44+
* Get maximal width value for resized image
45+
*
46+
* @return int
47+
*/
48+
public function getMaxWidth(): int
49+
{
50+
return (int)$this->config->getValue(self::XML_PATH_MAX_WIDTH_IMAGE);
51+
}
52+
53+
/**
54+
* Get maximal height value for resized image
55+
*
56+
* @return int
57+
*/
58+
public function getMaxHeight(): int
59+
{
60+
return (int)$this->config->getValue(self::XML_PATH_MAX_HEIGHT_IMAGE);
61+
}
62+
63+
/**
64+
* Get config value for frontend resize
65+
*
66+
* @return bool
67+
*/
68+
public function isResizeEnabled(): bool
69+
{
70+
return (bool)$this->config->getValue(self::XML_PATH_ENABLE_RESIZE);
71+
}
72+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backend\Model\Image;
9+
10+
/**
11+
* Interface UploadResizeConfigInterface
12+
*
13+
* Used to retrieve configuration for frontend image uploader
14+
*/
15+
interface UploadResizeConfigInterface
16+
{
17+
/**
18+
* Get maximal width value for resized image
19+
*
20+
* @return int
21+
*/
22+
public function getMaxWidth(): int;
23+
24+
/**
25+
* Get maximal height value for resized image
26+
*
27+
* @return int
28+
*/
29+
public function getMaxHeight(): int;
30+
31+
/**
32+
* Get config value for frontend resize
33+
*
34+
* @return bool
35+
*/
36+
public function isResizeEnabled(): bool;
37+
}

app/code/Magento/Backend/etc/adminhtml/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,5 @@
168168
</arguments>
169169
</type>
170170
<preference for="CsrfRequestValidator" type="Magento\Backend\App\Request\BackendValidator" />
171+
<preference for="Magento\Backend\Model\Image\UploadResizeConfigInterface" type="Magento\Backend\Model\Image\UploadResizeConfig" />
171172
</config>

app/code/Magento/Backend/etc/adminhtml/system.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,26 @@
338338
</group>
339339
<group id="upload_configuration" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
340340
<label>Images Upload Configuration</label>
341-
<field id="max_width" translate="label comment" type="text" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
341+
<field id="enable_resize" translate="label" type="select" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
342+
<label>Enable Frontend Resize</label>
343+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
344+
<comment>Resize performed via javascript before file upload.</comment>
345+
</field>
346+
<field id="max_width" translate="label comment" type="text" sortOrder="300" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
342347
<label>Maximum Width</label>
343348
<validate>validate-greater-than-zero validate-number required-entry</validate>
344349
<comment>Maximum allowed width for uploaded image.</comment>
350+
<depends>
351+
<field id="enable_resize">1</field>
352+
</depends>
345353
</field>
346-
<field id="max_height" translate="label comment" type="text" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
354+
<field id="max_height" translate="label comment" type="text" sortOrder="400" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
347355
<label>Maximum Height</label>
348356
<validate>validate-greater-than-zero validate-number required-entry</validate>
349357
<comment>Maximum allowed height for uploaded image.</comment>
358+
<depends>
359+
<field id="enable_resize">1</field>
360+
</depends>
350361
</field>
351362
</group>
352363
</section>

app/code/Magento/Backend/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<enable_charts>1</enable_charts>
3030
</dashboard>
3131
<upload_configuration>
32+
<enable_resize>1</enable_resize>
3233
<max_width>1920</max_width>
3334
<max_height>1200</max_height>
3435
</upload_configuration>

app/code/Magento/Backend/view/adminhtml/templates/media/uploader.phtml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
data-mage-init='{
1414
"Magento_Backend/js/media-uploader" : {
1515
"maxFileSize": <?= /* @escapeNotVerified */ $block->getFileSizeService()->getMaxFileSize() ?>,
16-
"maxWidth":<?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?> ,
17-
"maxHeight": <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>
16+
"maxWidth": <?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?>,
17+
"maxHeight": <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>,
18+
"isResizeEnabled": <?= /* @noEscape */ $block->getImageUploadConfigData()->getIsResizeEnabled() ?>
1819
}
1920
}'
2021
>

app/code/Magento/Backend/view/adminhtml/web/js/media-uploader.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ define([
3333
* @private
3434
*/
3535
_create: function () {
36-
var
37-
self = this,
38-
progressTmpl = mageTemplate('[data-template="uploader"]');
36+
var self = this,
37+
progressTmpl = mageTemplate('[data-template="uploader"]'),
38+
isResizeEnabled = this.options.isResizeEnabled,
39+
resizeConfiguration = {
40+
action: 'resize',
41+
maxWidth: this.options.maxWidth,
42+
maxHeight: this.options.maxHeight
43+
};
44+
45+
if (!isResizeEnabled) {
46+
resizeConfiguration = {
47+
action: 'resize'
48+
};
49+
}
3950

4051
this.element.find('input[type=file]').fileupload({
4152
dataType: 'json',
@@ -52,8 +63,7 @@ define([
5263
* @param {Object} data
5364
*/
5465
add: function (e, data) {
55-
var
56-
fileSize,
66+
var fileSize,
5767
tmpl;
5868

5969
$.each(data.files, function (index, file) {
@@ -123,11 +133,10 @@ define([
123133
this.element.find('input[type=file]').fileupload('option', {
124134
process: [{
125135
action: 'load',
126-
fileTypes: /^image\/(gif|jpeg|png)$/,
127-
maxFileSize: this.options.maxFileSize
128-
}, {
129-
action: 'resize'
130-
}, {
136+
fileTypes: /^image\/(gif|jpeg|png)$/
137+
},
138+
resizeConfiguration,
139+
{
131140
action: 'save'
132141
}]
133142
});

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
*/
1414
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery;
1515

16+
use Magento\Framework\App\ObjectManager;
1617
use Magento\Backend\Block\Media\Uploader;
1718
use Magento\Framework\View\Element\AbstractBlock;
1819
use Magento\Framework\App\Filesystem\DirectoryList;
1920
use Magento\Framework\Exception\FileSystemException;
21+
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
2022

2123
/**
2224
* Block for gallery content.
@@ -43,21 +45,30 @@ class Content extends \Magento\Backend\Block\Widget
4345
*/
4446
private $imageHelper;
4547

48+
/**
49+
* @var ImageUploadConfigDataProvider
50+
*/
51+
private $imageUploadConfigDataProvider;
52+
4653
/**
4754
* @param \Magento\Backend\Block\Template\Context $context
4855
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
4956
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
5057
* @param array $data
58+
* @param ImageUploadConfigDataProvider $imageUploadConfigDataProvider
5159
*/
5260
public function __construct(
5361
\Magento\Backend\Block\Template\Context $context,
5462
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
5563
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
56-
array $data = []
64+
array $data = [],
65+
ImageUploadConfigDataProvider $imageUploadConfigDataProvider = null
5766
) {
5867
$this->_jsonEncoder = $jsonEncoder;
5968
$this->_mediaConfig = $mediaConfig;
6069
parent::__construct($context, $data);
70+
$this->imageUploadConfigDataProvider = $imageUploadConfigDataProvider
71+
?: ObjectManager::getInstance()->get(ImageUploadConfigDataProvider::class);
6172
}
6273

6374
/**
@@ -67,7 +78,11 @@ public function __construct(
6778
*/
6879
protected function _prepareLayout()
6980
{
70-
$this->addChild('uploader', \Magento\Backend\Block\Media\Uploader::class);
81+
$this->addChild(
82+
'uploader',
83+
\Magento\Backend\Block\Media\Uploader::class,
84+
['image_upload_config_data' => $this->imageUploadConfigDataProvider]
85+
);
7186

7287
$this->getUploader()->getConfig()->setUrl(
7388
$this->_urlBuilder->addSessionParam()->getUrl('catalog/product_gallery/upload')

0 commit comments

Comments
 (0)