I want to change the MAX_IMAGE_WIDTH and MAX_IMAGE_HEIGHT constants of image uploader in Admin.
I think we should override vendor/magento/framework/File/Uploader.php. Can anyone give me a suggestion?
I want to change the MAX_IMAGE_WIDTH and MAX_IMAGE_HEIGHT constants of image uploader in Admin.
I think we should override vendor/magento/framework/File/Uploader.php. Can anyone give me a suggestion?
We should not override the constant variables of class.
If having already a custom admin theme, you can override these templates in your custom admin theme.
I saw some templates which use these constant variables:
vendor/magento/module-backend/view/adminhtml/templates/media/uploader.phtml
vendor/magento/module-cms/view/adminhtml/templates/browser/content/uploader.phtml
Read more here: http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/themes/admin_theme_create.html
I needed this only for product images and I didn't want to create a custom admin theme. So I used the event catalog_product_gallery_prepare_layout.
adminhtml/events.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="catalog_product_gallery_prepare_layout"> <observer name="vendor_module_catalog_product_gallery_prepare_layout" instance="Vendor\Module\Observer\CatalogProductGalleryPrepareLayout" /> </event> Observer/CatalogProductGalleryPrepareLayout.php
<?php namespace Vendor\Module\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class CatalogProductGalleryPrepareLayout implements ObserverInterface { public function execute(Observer $observer) { $block = $observer->getBlock(); if (!$block) return; $uploaderBlock = $block->getChildBlock('uploader'); if (!$uploaderBlock) return; $uploaderBlock->setTemplate('Vendor_Module::media/uploader.phtml'); } } and of course you should override the template:
view/adminhtml/templates/media/uploader.phtml
As from Magento 2.3.3 you no-longer need to change this setting by overriding files as it is configurable within the admin area:
Stores > Configuration > Advanced > System > Images Upload Configuration
Commit: https://github.com/magento/magento2/commit/88e26f2b958d6835f877d0feea8759418326a7f5
You have to just create one module inside app/code folder.
Inside etc/di.xml file,
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\File\Uploader" type="Vendor\Module\File\Uploader" /> </config> Now just create one folder under Module, File and create Uploader.php file.
app/code/Vendor/Module/File Uploader.php file,
<?php namespace Vendor\Module\File; use Magento\Framework\Filesystem\DriverInterface; class Uploader extends \Magento\Framework\File\Uploader { /** * Uploaded file handle (copy of $_FILES[] element) * * @var array * @access protected */ protected $_file; /** * Uploaded file mime type * * @var string * @access protected */ protected $_fileMimeType; /** * Upload type. Used to right handle $_FILES array. * * @var \Magento\Framework\File\Uploader::SINGLE_STYLE|\Magento\Framework\File\Uploader::MULTIPLE_STYLE * @access protected */ protected $_uploadType; /** * The name of uploaded file. By default it is original file name, but when * we will change file name, this variable will be changed too. * * @var string * @access protected */ protected $_uploadedFileName; /** * The name of destination directory * * @var string * @access protected */ protected $_uploadedFileDir; /** * If this variable is set to TRUE, our library will be able to automatically create * non-existent directories. * * @var bool * @access protected */ protected $_allowCreateFolders = true; /** * If this variable is set to TRUE, uploaded file name will be changed if some file with the same * name already exists in the destination directory (if enabled). * * @var bool * @access protected */ protected $_allowRenameFiles = false; /** * If this variable is set to TRUE, files dispertion will be supported. * * @var bool * @access protected */ protected $_enableFilesDispersion = false; /** * This variable is used both with $_enableFilesDispersion == true * It helps to avoid problems after migrating from case-insensitive file system to case-insensitive * (e.g. NTFS->ext or ext->NTFS) * * @var bool * @access protected */ protected $_caseInsensitiveFilenames = true; /** * @var string * @access protected */ protected $_dispretionPath = null; /** * @var bool */ protected $_fileExists = false; /** * @var null|string[] */ protected $_allowedExtensions = null; /** * Validate callbacks storage * * @var array * @access protected */ protected $_validateCallbacks = []; /**#@+ * File upload type (multiple or single) */ const SINGLE_STYLE = 0; const MULTIPLE_STYLE = 1; /**#@-*/ /** * Temp file name empty code */ const TMP_NAME_EMPTY = 666; /** * Max Image Width resolution in pixels. For image resizing on client side */ const MAX_IMAGE_WIDTH = 1920; /** * Max Image Height resolution in pixels. For image resizing on client side */ const MAX_IMAGE_HEIGHT = 1200; }