Skip to content

Commit

Permalink
[EngCom] Public Pull Requests - 2.3-develop
Browse files Browse the repository at this point in the history
 - merged latest code from mainline branch
  • Loading branch information
magento-engcom-team authored Nov 20, 2018
2 parents c85563e + 5e17b04 commit 1022233
Show file tree
Hide file tree
Showing 28 changed files with 621 additions and 96 deletions.
40 changes: 40 additions & 0 deletions app/code/Magento/Backend/Block/DataProviders/ImageUploadConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Block\DataProviders;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Backend\Model\Image\UploadResizeConfigInterface;

/**
* Provides additional data for image uploader
*/
class ImageUploadConfig implements ArgumentInterface
{
/**
* @var UploadResizeConfigInterface
*/
private $imageUploadConfig;

/**
* @param UploadResizeConfigInterface $imageUploadConfig
*/
public function __construct(UploadResizeConfigInterface $imageUploadConfig)
{
$this->imageUploadConfig = $imageUploadConfig;
}

/**
* Get image resize configuration
*
* @return int
*/
public function getIsResizeEnabled(): int
{
return (int)$this->imageUploadConfig->isResizeEnabled();
}
}
22 changes: 17 additions & 5 deletions app/code/Magento/Backend/Block/Media/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Image\Adapter\UploadConfigInterface;
use Magento\Backend\Model\Image\UploadResizeConfigInterface;

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

/**
* @var UploadResizeConfigInterface
*/
private $imageUploadConfig;

/**
* @var UploadConfigInterface
* @deprecated
* @see \Magento\Backend\Model\Image\UploadResizeConfigInterface
*/
private $imageConfig;

Expand All @@ -49,18 +57,22 @@ class Uploader extends \Magento\Backend\Block\Widget
* @param array $data
* @param Json $jsonEncoder
* @param UploadConfigInterface $imageConfig
* @param UploadResizeConfigInterface $imageUploadConfig
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\File\Size $fileSize,
array $data = [],
Json $jsonEncoder = null,
UploadConfigInterface $imageConfig = null
UploadConfigInterface $imageConfig = null,
UploadResizeConfigInterface $imageUploadConfig = null
) {
$this->_fileSizeService = $fileSize;
$this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
$this->imageConfig = $imageConfig ?: ObjectManager::getInstance()->get(UploadConfigInterface::class);

$this->imageConfig = $imageConfig
?: ObjectManager::getInstance()->get(UploadConfigInterface::class);
$this->imageUploadConfig = $imageUploadConfig
?: ObjectManager::getInstance()->get(UploadResizeConfigInterface::class);
parent::__construct($context, $data);
}

Expand Down Expand Up @@ -111,7 +123,7 @@ public function getFileSizeService()
*/
public function getImageUploadMaxWidth()
{
return $this->imageConfig->getMaxWidth();
return $this->imageUploadConfig->getMaxWidth();
}

/**
Expand All @@ -121,7 +133,7 @@ public function getImageUploadMaxWidth()
*/
public function getImageUploadMaxHeight()
{
return $this->imageConfig->getMaxHeight();
return $this->imageUploadConfig->getMaxHeight();
}

/**
Expand Down
72 changes: 72 additions & 0 deletions app/code/Magento/Backend/Model/Image/UploadResizeConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Model\Image;

/**
* Image uploader config provider.
*/
class UploadResizeConfig implements UploadResizeConfigInterface
{
/**
* Config path for the maximal image width value
*/
const XML_PATH_MAX_WIDTH_IMAGE = 'system/upload_configuration/max_width';

/**
* Config path for the maximal image height value
*/
const XML_PATH_MAX_HEIGHT_IMAGE = 'system/upload_configuration/max_height';

/**
* Config path for the maximal image height value
*/
const XML_PATH_ENABLE_RESIZE = 'system/upload_configuration/enable_resize';

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $config;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
*/
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $config)
{
$this->config = $config;
}

/**
* Get maximal width value for resized image
*
* @return int
*/
public function getMaxWidth(): int
{
return (int)$this->config->getValue(self::XML_PATH_MAX_WIDTH_IMAGE);
}

/**
* Get maximal height value for resized image
*
* @return int
*/
public function getMaxHeight(): int
{
return (int)$this->config->getValue(self::XML_PATH_MAX_HEIGHT_IMAGE);
}

/**
* Get config value for frontend resize
*
* @return bool
*/
public function isResizeEnabled(): bool
{
return (bool)$this->config->getValue(self::XML_PATH_ENABLE_RESIZE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Model\Image;

/**
* Interface UploadResizeConfigInterface
*
* Used to retrieve configuration for frontend image uploader
*/
interface UploadResizeConfigInterface
{
/**
* Get maximal width value for resized image
*
* @return int
*/
public function getMaxWidth(): int;

/**
* Get maximal height value for resized image
*
* @return int
*/
public function getMaxHeight(): int;

/**
* Get config value for frontend resize
*
* @return bool
*/
public function isResizeEnabled(): bool;
}
1 change: 1 addition & 0 deletions app/code/Magento/Backend/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@
</arguments>
</type>
<preference for="CsrfRequestValidator" type="Magento\Backend\App\Request\BackendValidator" />
<preference for="Magento\Backend\Model\Image\UploadResizeConfigInterface" type="Magento\Backend\Model\Image\UploadResizeConfig" />
</config>
15 changes: 13 additions & 2 deletions app/code/Magento/Backend/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,26 @@
</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">
<field id="enable_resize" translate="label" type="select" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Enable Frontend Resize</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Resize performed via javascript before file upload.</comment>
</field>
<field id="max_width" translate="label comment" type="text" sortOrder="300" 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>
<depends>
<field id="enable_resize">1</field>
</depends>
</field>
<field id="max_height" translate="label comment" type="text" sortOrder="200" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<field id="max_height" translate="label comment" type="text" sortOrder="400" 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>
<depends>
<field id="enable_resize">1</field>
</depends>
</field>
</group>
</section>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Backend/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<enable_charts>1</enable_charts>
</dashboard>
<upload_configuration>
<enable_resize>1</enable_resize>
<max_width>1920</max_width>
<max_height>1200</max_height>
</upload_configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
data-mage-init='{
"Magento_Backend/js/media-uploader" : {
"maxFileSize": <?= /* @escapeNotVerified */ $block->getFileSizeService()->getMaxFileSize() ?>,
"maxWidth":<?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?> ,
"maxHeight": <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>
"maxWidth": <?= /* @escapeNotVerified */ $block->getImageUploadMaxWidth() ?>,
"maxHeight": <?= /* @escapeNotVerified */ $block->getImageUploadMaxHeight() ?>,
"isResizeEnabled": <?= /* @noEscape */ $block->getImageUploadConfigData()->getIsResizeEnabled() ?>
}
}'
>
Expand Down
29 changes: 19 additions & 10 deletions app/code/Magento/Backend/view/adminhtml/web/js/media-uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ define([
* @private
*/
_create: function () {
var
self = this,
progressTmpl = mageTemplate('[data-template="uploader"]');
var self = this,
progressTmpl = mageTemplate('[data-template="uploader"]'),
isResizeEnabled = this.options.isResizeEnabled,
resizeConfiguration = {
action: 'resize',
maxWidth: this.options.maxWidth,
maxHeight: this.options.maxHeight
};

if (!isResizeEnabled) {
resizeConfiguration = {
action: 'resize'
};
}

this.element.find('input[type=file]').fileupload({
dataType: 'json',
Expand All @@ -52,8 +63,7 @@ define([
* @param {Object} data
*/
add: function (e, data) {
var
fileSize,
var fileSize,
tmpl;

$.each(data.files, function (index, file) {
Expand Down Expand Up @@ -123,11 +133,10 @@ define([
this.element.find('input[type=file]').fileupload('option', {
process: [{
action: 'load',
fileTypes: /^image\/(gif|jpeg|png)$/,
maxFileSize: this.options.maxFileSize
}, {
action: 'resize'
}, {
fileTypes: /^image\/(gif|jpeg|png)$/
},
resizeConfiguration,
{
action: 'save'
}]
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
*/
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery;

use Magento\Framework\App\ObjectManager;
use Magento\Backend\Block\Media\Uploader;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;

/**
* Block for gallery content.
Expand All @@ -43,21 +45,30 @@ class Content extends \Magento\Backend\Block\Widget
*/
private $imageHelper;

/**
* @var ImageUploadConfigDataProvider
*/
private $imageUploadConfigDataProvider;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
* @param array $data
* @param ImageUploadConfigDataProvider $imageUploadConfigDataProvider
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
array $data = []
array $data = [],
ImageUploadConfigDataProvider $imageUploadConfigDataProvider = null
) {
$this->_jsonEncoder = $jsonEncoder;
$this->_mediaConfig = $mediaConfig;
parent::__construct($context, $data);
$this->imageUploadConfigDataProvider = $imageUploadConfigDataProvider
?: ObjectManager::getInstance()->get(ImageUploadConfigDataProvider::class);
}

/**
Expand All @@ -67,7 +78,11 @@ public function __construct(
*/
protected function _prepareLayout()
{
$this->addChild('uploader', \Magento\Backend\Block\Media\Uploader::class);
$this->addChild(
'uploader',
\Magento\Backend\Block\Media\Uploader::class,
['image_upload_config_data' => $this->imageUploadConfigDataProvider]
);

$this->getUploader()->getConfig()->setUrl(
$this->_urlBuilder->addSessionParam()->getUrl('catalog/product_gallery/upload')
Expand Down
Loading

0 comments on commit 1022233

Please sign in to comment.