Skip to content

[Forwardport] Making configurable settings for MAX_IMAGE_WIDTH and MAX_IMAGE_HEIGHT #17826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion app/code/Magento/Backend/Block/Media/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Block\Media;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Image\Adapter\UploadConfigInterface;

/**
* Adminhtml media library uploader
Expand Down Expand Up @@ -35,20 +38,29 @@ class Uploader extends \Magento\Backend\Block\Widget
*/
private $jsonEncoder;

/**
* @var UploadConfigInterface
*/
private $imageConfig;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\File\Size $fileSize
* @param array $data
* @param Json $jsonEncoder
* @param UploadConfigInterface $imageConfig
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\File\Size $fileSize,
array $data = [],
Json $jsonEncoder = null
Json $jsonEncoder = null,
UploadConfigInterface $imageConfig = null
) {
$this->_fileSizeService = $fileSize;
$this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
$this->imageConfig = $imageConfig ?: ObjectManager::getInstance()->get(UploadConfigInterface::class);

parent::__construct($context, $data);
}

Expand Down Expand Up @@ -90,6 +102,26 @@ public function getFileSizeService()
return $this->_fileSizeService;
}

/**
* Get Image Upload Maximum Width Config.
*
* @return int
*/
public function getImageUploadMaxWidth()
{
return $this->imageConfig->getMaxWidth();
}

/**
* Get Image Upload Maximum Height Config.
*
* @return int
*/
public function getImageUploadMaxHeight()
{
return $this->imageConfig->getMaxHeight();
}

/**
* Prepares layout and set element renderer
*
Expand Down
13 changes: 13 additions & 0 deletions app/code/Magento/Backend/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,19 @@
</depends>
</field>
</group>
<group id="upload_configuration" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Images Upload Configuration</label>
<field id="max_width" translate="label comment" type="text" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Maximum Width</label>
<validate>validate-greater-than-zero validate-number required-entry</validate>
<comment>Maximum allowed width for uploaded image.</comment>
</field>
<field id="max_height" translate="label comment" type="text" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Maximum Height</label>
<validate>validate-greater-than-zero validate-number required-entry</validate>
<comment>Maximum allowed height for uploaded image.</comment>
</field>
</group>
</section>
<section id="admin" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Admin</label>
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/Backend/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<dashboard>
<enable_charts>1</enable_charts>
</dashboard>
<upload_configuration>
<max_width>1920</max_width>
<max_height>1200</max_height>
</upload_configuration>
</system>
<general>
<validator_data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
data-mage-init='{
"Magento_Backend/js/media-uploader" : {
"maxFileSize": <?= /* @escapeNotVerified */ $block->getFileSizeService()->getMaxFileSize() ?>,
"maxWidth":<?= /* @escapeNotVerified */ \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH ?> ,
"maxHeight": <?= /* @escapeNotVerified */ \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT ?>
"maxWidth":<?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?> ,
"maxHeight": <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>
}
}'
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ require([
maxFileSize: <?= (int) $block->getFileSizeService()->getMaxFileSize() ?> * 10
}, {
action: 'resize',
maxWidth: <?= (float) \Magento\Framework\File\Uploader::MAX_IMAGE_WIDTH ?> ,
maxHeight: <?= (float) \Magento\Framework\File\Uploader::MAX_IMAGE_HEIGHT ?>
maxWidth: <?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?> ,
maxHeight: <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>
}, {
action: 'save'
}]
Expand Down
1 change: 1 addition & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<preference for="Magento\Framework\View\Design\Theme\CustomizationInterface" type="Magento\Framework\View\Design\Theme\Customization" />
<preference for="Magento\Framework\View\Asset\ConfigInterface" type="Magento\Framework\View\Asset\Config" />
<preference for="Magento\Framework\Image\Adapter\ConfigInterface" type="Magento\Framework\Image\Adapter\Config" />
<preference for="Magento\Framework\Image\Adapter\UploadConfigInterface" type="Magento\Framework\Image\Adapter\Config" />
<preference for="Magento\Framework\View\Design\Theme\Image\PathInterface" type="Magento\Theme\Model\Theme\Image\Path" />
<preference for="Magento\Framework\Session\Config\ConfigInterface" type="Magento\Framework\Session\Config" />
<preference for="Magento\Framework\Session\SidResolverInterface" type="Magento\Framework\Session\SidResolver\Proxy" />
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/Magento/Framework/File/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ class Uploader
const TMP_NAME_EMPTY = 666;

/**
* Max Image Width resolution in pixels. For image resizing on client side
* Maximum Image Width resolution in pixels. For image resizing on client side
* @deprecated
* @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
*/
const MAX_IMAGE_WIDTH = 1920;

/**
* Max Image Height resolution in pixels. For image resizing on client side
* Maximum Image Height resolution in pixels. For image resizing on client side
* @deprecated
* @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
*/
const MAX_IMAGE_HEIGHT = 1200;

Expand Down
28 changes: 27 additions & 1 deletion lib/internal/Magento/Framework/Image/Adapter/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Image\Adapter;

class Config implements \Magento\Framework\Image\Adapter\ConfigInterface
class Config implements ConfigInterface, UploadConfigInterface
{
const XML_PATH_IMAGE_ADAPTER = 'dev/image/default_adapter';

const XML_PATH_IMAGE_ADAPTERS = 'dev/image/adapters';

const XML_PATH_MAX_WIDTH_IMAGE = 'system/upload_configuration/max_width';

const XML_PATH_MAX_HEIGHT_IMAGE = 'system/upload_configuration/max_height';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
Expand Down Expand Up @@ -43,4 +49,24 @@ public function getAdapters()
{
return $this->config->getValue(self::XML_PATH_IMAGE_ADAPTERS);
}

/**
* Get Maximum Image Width resolution in pixels. For image resizing on client side.
*
* @return int
*/
public function getMaxWidth(): int
{
return (int)$this->config->getValue(self::XML_PATH_MAX_WIDTH_IMAGE);
}

/**
* Get Maximum Image Height resolution in pixels. For image resizing on client side.
*
* @return int
*/
public function getMaxHeight(): int
{
return (int)$this->config->getValue(self::XML_PATH_MAX_HEIGHT_IMAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Image\Adapter;

/**
* Interface UploadConfigInterface
*/
interface UploadConfigInterface
{
/**
* @return int
*/
public function getMaxWidth(): int;

/**
* @return int
*/
public function getMaxHeight(): int;
}