Skip to content

Commit 88e26f2

Browse files
eduard13nmalevanec
authored andcommitted
Making configurable settings for MAX_IMAGE_WIDTH and MAX_IMAGE_HEIGHT
1 parent 232eec2 commit 88e26f2

File tree

9 files changed

+108
-8
lines changed

9 files changed

+108
-8
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Backend\Block\Media;
79

810
use Magento\Framework\App\ObjectManager;
911
use Magento\Framework\Serialize\Serializer\Json;
12+
use Magento\Framework\Image\Adapter\UploadConfigInterface;
1013

1114
/**
1215
* Adminhtml media library uploader
@@ -35,20 +38,29 @@ class Uploader extends \Magento\Backend\Block\Widget
3538
*/
3639
private $jsonEncoder;
3740

41+
/**
42+
* @var UploadConfigInterface
43+
*/
44+
private $imageConfig;
45+
3846
/**
3947
* @param \Magento\Backend\Block\Template\Context $context
4048
* @param \Magento\Framework\File\Size $fileSize
4149
* @param array $data
4250
* @param Json $jsonEncoder
51+
* @param UploadConfigInterface $imageConfig
4352
*/
4453
public function __construct(
4554
\Magento\Backend\Block\Template\Context $context,
4655
\Magento\Framework\File\Size $fileSize,
4756
array $data = [],
48-
Json $jsonEncoder = null
57+
Json $jsonEncoder = null,
58+
UploadConfigInterface $imageConfig = null
4959
) {
5060
$this->_fileSizeService = $fileSize;
5161
$this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
62+
$this->imageConfig = $imageConfig ?: ObjectManager::getInstance()->get(UploadConfigInterface::class);
63+
5264
parent::__construct($context, $data);
5365
}
5466

@@ -90,6 +102,26 @@ public function getFileSizeService()
90102
return $this->_fileSizeService;
91103
}
92104

105+
/**
106+
* Get Image Upload Maximum Width Config.
107+
*
108+
* @return int
109+
*/
110+
public function getImageUploadMaxWidth()
111+
{
112+
return $this->imageConfig->getMaxWidth();
113+
}
114+
115+
/**
116+
* Get Image Upload Maximum Height Config.
117+
*
118+
* @return int
119+
*/
120+
public function getImageUploadMaxHeight()
121+
{
122+
return $this->imageConfig->getMaxHeight();
123+
}
124+
93125
/**
94126
* Prepares layout and set element renderer
95127
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,19 @@
335335
</depends>
336336
</field>
337337
</group>
338+
<group id="upload_configuration" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
339+
<label>Images Upload Configuration</label>
340+
<field id="max_width" translate="label comment" type="text" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
341+
<label>Maximum Width</label>
342+
<validate>validate-greater-than-zero validate-number required-entry</validate>
343+
<comment>Maximum allowed width for uploaded image.</comment>
344+
</field>
345+
<field id="max_height" translate="label comment" type="text" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
346+
<label>Maximum Height</label>
347+
<validate>validate-greater-than-zero validate-number required-entry</validate>
348+
<comment>Maximum allowed height for uploaded image.</comment>
349+
</field>
350+
</group>
338351
</section>
339352
<section id="admin" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
340353
<label>Admin</label>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
<dashboard>
2929
<enable_charts>1</enable_charts>
3030
</dashboard>
31+
<upload_configuration>
32+
<max_width>1920</max_width>
33+
<max_height>1200</max_height>
34+
</upload_configuration>
3135
</system>
3236
<general>
3337
<validator_data>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
data-mage-init='{
1414
"Magento_Backend/js/media-uploader" : {
1515
"maxFileSize": <?= /* @escapeNotVerified */ $block->getFileSizeService()->getMaxFileSize() ?>,
16-
"maxWidth":<?= /* @escapeNotVerified */ \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH ?> ,
17-
"maxHeight": <?= /* @escapeNotVerified */ \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT ?>
16+
"maxWidth":<?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?> ,
17+
"maxHeight": <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>
1818
}
1919
}'
2020
>

app/code/Magento/Cms/view/adminhtml/templates/browser/content/uploader.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ require([
147147
maxFileSize: <?= (int) $block->getFileSizeService()->getMaxFileSize() ?> * 10
148148
}, {
149149
action: 'resize',
150-
maxWidth: <?= (float) \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH ?> ,
151-
maxHeight: <?= (float) \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT ?>
150+
maxWidth: <?= $block->getImageUploadMaxWidth() ?> ,
151+
maxHeight: <?= $block->getImageUploadMaxHeight() ?>
152152
}, {
153153
action: 'save'
154154
}]

app/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<preference for="Magento\Framework\View\Design\Theme\CustomizationInterface" type="Magento\Framework\View\Design\Theme\Customization" />
8888
<preference for="Magento\Framework\View\Asset\ConfigInterface" type="Magento\Framework\View\Asset\Config" />
8989
<preference for="Magento\Framework\Image\Adapter\ConfigInterface" type="Magento\Framework\Image\Adapter\Config" />
90+
<preference for="Magento\Framework\Image\Adapter\UploadConfigInterface" type="Magento\Framework\Image\Adapter\Config" />
9091
<preference for="Magento\Framework\View\Design\Theme\Image\PathInterface" type="Magento\Theme\Model\Theme\Image\Path" />
9192
<preference for="Magento\Framework\Session\Config\ConfigInterface" type="Magento\Framework\Session\Config" />
9293
<preference for="Magento\Framework\Session\SidResolverInterface" type="Magento\Framework\Session\SidResolver\Proxy" />

lib/internal/Magento/Framework/File/Uploader.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ class Uploader
136136
const TMP_NAME_EMPTY = 666;
137137

138138
/**
139-
* Max Image Width resolution in pixels. For image resizing on client side
139+
* Maximum Image Width resolution in pixels. For image resizing on client side
140+
* @deprecated
141+
* @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
140142
*/
141143
const MAX_IMAGE_WIDTH = 1920;
142144

143145
/**
144-
* Max Image Height resolution in pixels. For image resizing on client side
146+
* Maximum Image Height resolution in pixels. For image resizing on client side
147+
* @deprecated
148+
* @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
145149
*/
146150
const MAX_IMAGE_HEIGHT = 1200;
147151

lib/internal/Magento/Framework/Image/Adapter/Config.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
*/
66
namespace Magento\Framework\Image\Adapter;
77

8-
class Config implements \Magento\Framework\Image\Adapter\ConfigInterface
8+
class Config implements ConfigInterface, UploadConfigInterface
99
{
1010
const XML_PATH_IMAGE_ADAPTER = 'dev/image/default_adapter';
1111

1212
const XML_PATH_IMAGE_ADAPTERS = 'dev/image/adapters';
1313

14+
const XML_PATH_MAX_WIDTH_IMAGE = 'system/upload_configuration/max_width';
15+
16+
const XML_PATH_MAX_HEIGHT_IMAGE = 'system/upload_configuration/max_height';
17+
1418
/**
1519
* @var \Magento\Framework\App\Config\ScopeConfigInterface
1620
*/
@@ -43,4 +47,24 @@ public function getAdapters()
4347
{
4448
return $this->config->getValue(self::XML_PATH_IMAGE_ADAPTERS);
4549
}
50+
51+
/**
52+
* Get Maximum Image Width resolution in pixels. For image resizing on client side.
53+
*
54+
* @return int
55+
*/
56+
public function getMaxWidth()
57+
{
58+
return (int)$this->config->getValue(self::XML_PATH_MAX_WIDTH_IMAGE);
59+
}
60+
61+
/**
62+
* Get Maximum Image Height resolution in pixels. For image resizing on client side.
63+
*
64+
* @return int
65+
*/
66+
public function getMaxHeight()
67+
{
68+
return (int)$this->config->getValue(self::XML_PATH_MAX_HEIGHT_IMAGE);
69+
}
4670
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Image\Adapter;
7+
8+
/**
9+
* Interface UploadConfigInterface
10+
*/
11+
interface UploadConfigInterface
12+
{
13+
/**
14+
* @return int
15+
*/
16+
public function getMaxWidth();
17+
18+
/**
19+
* @return int
20+
*/
21+
public function getMaxHeight();
22+
}

0 commit comments

Comments
 (0)