CodeIgniter Forums
File Uploading Library. Error if image < 300 pixels? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: File Uploading Library. Error if image < 300 pixels? (/thread-33201.html)

Pages: 1 2


File Uploading Library. Error if image < 300 pixels? - El Forum - 09-25-2010

[eluser]Sinclair[/eluser]
Hi,

Here is the extension to the Upload class. But it is not working...

Code:
&lt;?PHP

class MY_Upload extends CI_Upload {
    
    var $min_width        = 0;
    var $min_height        = 0;
    
    
    /**
     * Initialize preferences
     *
     * @access    public
     * @param    array
     * @return    void
     */        
    function initialize($config = array())
        {
            $defaults = array(
                                'max_size'            => 0,
                                'max_width'            => 0,
                                'max_height'        => 0,
                                'max_filename'        => 0,
                                'min_width'            => 0,
                                'min_height'            => 0,
                                'allowed_types'        => "",
                                'file_temp'            => "",
                                'file_name'            => "",
                                'orig_name'            => "",
                                'file_type'            => "",
                                'file_size'            => "",
                                'file_ext'            => "",
                                'upload_path'        => "",
                                'overwrite'            => FALSE,
                                'encrypt_name'        => FALSE,
                                'is_image'            => FALSE,
                                'image_width'        => '',
                                'image_height'        => '',
                                'image_type'        => '',
                                'image_size_str'    => '',
                                'error_msg'            => array(),
                                'mimes'                => array(),
                                'remove_spaces'        => TRUE,
                                'xss_clean'            => FALSE,
                                'temp_prefix'        => "temp_file_"
                            );    
        
        
            foreach ($defaults as $key => $val)
            {
                if (isset($config[$key]))
                {
                    $method = 'set_'.$key;
                    if (method_exists($this, $method))
                    {
                        $this->$method($config[$key]);
                    }
                    else
                    {
                        $this->$key = $config[$key];
                    }            
                }
                else
                {
                    $this->$key = $val;
                }
            }
        }

    
    
    /**
     * Set Minimum Image Height
     *
     * @access    public
     * @param    integer
     * @return    void
     */    
    function set_min_height($n)
    {
        $this->min_height = ((int) $n < 0) ? 0: (int) $n;
    }
    
    /**
     * Set Minimum Image Width
     *
     * @access    public
     * @param    integer
     * @return    void
     */    
    function set_min_width($n)
    {
        $this->min_width = ((int) $n < 0) ? 0: (int) $n;
    }
    

    /**
     * Verify that the image is within the allowed width/height
     *
     * @access    public
     * @return    bool
     */    
    function is_allowed_dimensions()
        {
            if ( ! $this->is_image())
            {
                return TRUE;
            }
    
            if (function_exists('getimagesize'))
            {
                $D = @getimagesize($this->file_temp);
    
                if ($this->max_width > 0 AND $D['0'] > $this->max_width)
                {
                    return FALSE;
                }
    
                if ($this->max_height > 0 AND $D['1'] > $this->max_height)
                {
                    return FALSE;
                }
    
                if ($this->min_width > 0 AND $D['0'] < $this->min_width)
                {
                    return FALSE;
                }
    
                if ($this->min_height > 0 AND $D['1'] < $this->min_height)
                {
                    return FALSE;
                }
    
                return TRUE;
            }
    
            return TRUE;
        }  
    
}

In the config.php I have added:

Code:
$config['min_width'] = '300';
$config['min_height'] = '300';

What I need to do more to get this extension working?

Best Regards,


File Uploading Library. Error if image < 300 pixels? - El Forum - 09-30-2010

[eluser]Flynn[/eluser]
Did you put

Code:
$config['min_width'] = '300';
$config['min_height'] = '300';

in controller where you initialize upload library?
It shouldn't be in config.php.


File Uploading Library. Error if image < 300 pixels? - El Forum - 09-30-2010

[eluser]Sinclair[/eluser]
Thanks for the reply.

Yes. I have added the controllers:

Code:
$config['min_width'] = '300';
$config['min_height'] = '300';

It is now working with the code above.

Thanks for your help extending the class!

Best Regards,


File Uploading Library. Error if image < 300 pixels? - El Forum - 07-05-2012

[eluser]obay[/eluser]
The code Sinclair provides has a missing line (CI 2.1.0). Be sure to add this line before initialize() ends:

Code:
// if a file_name was provided in the config, use it instead of the user input
// supplied file name for all uploads until initialized again
$this->_file_name_override = $this->file_name;

The file upload_lang.php should also be modified so as not to confuse the user. Add the text "or minimum":

Code:
$lang['upload_invalid_dimensions'] = "The image you are attempting to upload exceedes the maximum or minimum height or width.";