Skip to content

Magento2 – How to validate image width and height in admin category form

Created a custom image in category_form.xml i need to validate the image height and width while upload if its exceeds the allowed dimensions should display the alert message

<field name="thumbnail_logo" sortOrder="104" formElement="imageUploader">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">category</item>
            </item>
        </argument>
        <settings>
            <elementTmpl>ui/form/element/uploader/image</elementTmpl>
            <dataType>string</dataType>
            <label translate="true">Category Image</label>
            <visible>true</visible>
            <required>false</required>
        </settings>
        <formElements>
            <imageUploader>
                <settings>
                    <required>false</required>
                    <uploaderConfig>
                        <param xsi:type="url" name="url" path="catalog/category_image/upload"/>
                    </uploaderConfig>
                    <previewTmpl>Magento_Catalog/image-preview</previewTmpl>
                    <openDialogTitle>Media Gallery</openDialogTitle>
                    <initialMediaGalleryOpenSubpath>catalog/category</initialMediaGalleryOpenSubpath>
                    <allowedExtensions>jpg jpeg gif png</allowedExtensions>
                    <maxFileSize>4194304</maxFileSize>
                    
                </settings>
            </imageUploader>
        </formElements>
    </field>