1

I wanted to restrict the dimension of image width & height to minimum in media manager of joomla 3.10. min-width: 100px, min-height:50px. Please help me how can i do this....?

1 Answer 1

1

This can be done with a content plugin:

<?php
defined('_JEXEC') or die;

use Joomla\CMS\Helper\MediaHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;

class PlgContentImagesize extends CMSPlugin
{
    protected $autoloadLanguage = true;

    public function __construct(&$subject, $config)
    {
        parent::__construct($subject, $config);

        $this->params->def('minWidth', 100);
        $this->params->def('minHeight', 50);
    }

    public function onContentBeforeSave($context, &$file, $isNew)
    {
        // Check that we are uploading a file.
        if ($context !== 'com_media.file')
        {
            return true;
        }

        // Check that file is an image.
        $helper = new MediaHelper;

        if (!$helper->isImage($file->filepath))
        {
            return true;
        }

        // Get image info.
        $info = getimagesize($file->tmp_name);

        // Compare image dimensions against params.
        if ($info[0] < $this->params->get('minWidth') || $info[1] < $this->params->get('minHeight'))
        {
            $file->setError(Text::_('PLG_CONTENT_IMAGESIZE_IMAGE_TOO_SMALL'));

            return false;
        }

        return true;

    }
}

See documentation on how to create other plugin files (manifest and language files) https://docs.joomla.org/J3.x:Creating_a_content_plugin.

Note, MediaHelper's isImage() only checks file extension. So it's not very good at determining whether file is an image. There are better ways such as checking the mime type. But for that you'd have to make a list of types you want to support.

1
  • $info = getimagesize($file->tmp_name); is not working. When i am try to compare the value of $info[0] with integer value it's showing true all for . $info[0] < '10' . true for all.
    – mohan
    Commented Aug 31, 2018 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.