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/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: File Uploading Library. Error if image < 300 pixels? (/showthread.php?tid=33201)

Pages: 1 2


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-18-2010

[eluser]Sinclair[/eluser]
Hi,

I need to display an error if the image is < than 300 pixels. Which is the best way of doing this?

Sorry for my bad english.

Best Regards,


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-18-2010

[eluser][254]Prof[/eluser]
You can add it as a rule when you load the File Uploading Library:

Code:
$config['max_width'] = 300;
$this->load->library('upload', $config);

if (!this->upload->do_upload()) {
    $error = $this->upload->display_errors();
} else {
    // Your code here
}

In your view, add:
Code:
if (isset($error)) echo $error;



File Uploading Library. Error if image < 300 pixels? - El Forum - 08-18-2010

[eluser]Sinclair[/eluser]
Hi, thanks for the reply.

I don't want to pop up an error if the max_width is larger than 300 pixels. I need to do the opposite. I need the display error if the size is less than 300 pixels.

I think I must to extend the Upload Library. Correct?

Best Regards,


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-18-2010

[eluser][254]Prof[/eluser]
Sorry, I had misunderstood the question.

I think you will have to extend the Upload library

Kind regards


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-19-2010

[eluser]Sinclair[/eluser]
Hi,

I'm trying to extend the Upload Library to pop up an Error when images have less than X pixels.

Code:
&lt;?PHP

class MY_Upload extends CI_Upload {
    
    var $min_width        = 0;
    var $min_height        = 0;
    
    /**
     * 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 minimum width/height
     *
     * @access    public
     * @return    bool
     */    
    function is_allowed_min_dimensions()
    {
        if ( ! $this->is_image())
        {
            return TRUE;
        }

        if (function_exists('getimagesize'))
        {
            $D = @getimagesize($this->file_temp);

            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;
    }
    
    
}

I have this until now. What I need to do more to get this extend working. And wich is the best way of doing this extend without broke the CI compatibility?

Best Regards,


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-28-2010

[eluser]Flynn[/eluser]
Try this one instead of function is_allowed_min_dimensions
Code:
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;
    }



File Uploading Library. Error if image < 300 pixels? - El Forum - 08-28-2010

[eluser]Sinclair[/eluser]
Hi, thanks for the reply.

And how to add the "min_width" and the "min_height" to the config file?

To the "max_width" and "max_height" we use:

Code:
$config['max_width'] = 7000;
$config['max_height'] = 6000;

How to add this config lines to the "min_width" and the "min_height"?


Best Regards,


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-28-2010

[eluser]Flynn[/eluser]
Keep this:

Code:
function set_min_height($n)
    {
        $this->min_height = ((int) $n < 0) ? 0: (int) $n;
    }

    function set_min_width($n)
    {
        $this->min_width = ((int) $n < 0) ? 0: (int) $n;
    }

And add this at the beginning:

Code:
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;
            }
        }
    }

I think that'll do the work.


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-28-2010

[eluser]juanvillegas[/eluser]
Good job! But are you sure no one has already extend this? Maybe some google would save you some coding hours


File Uploading Library. Error if image < 300 pixels? - El Forum - 08-29-2010

[eluser]Flynn[/eluser]
Well, i wasn't in the mood to dig google and i was sure that it wouldn't take much time to extend library with "minimum check". It took only 5-10 minutes to check what is where and extend it.