CodeIgniter Forums
getting image dimensions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: getting image dimensions (/showthread.php?tid=1876)



getting image dimensions - El Forum - 07-02-2007

[eluser]andyd[/eluser]
What is the best way to get the image dimensions of a gif or jpeg file on the server? Does the image manipulation helper offer function to provide these values, before trying to change the file.

Is it easy to get the image dimensions and resolution (width, height, dpi) with CI or PHP?

Kind regards

Andy


getting image dimensions - El Forum - 07-02-2007

[eluser]sophistry[/eluser]
Here you go... it's your birthday today so I went to the PHP site and I searched on get image size and, what do you know? I found a function called getimagesize(). pretty cool, huh?

http://us2.php.net/manual/en/function.getimagesize.php

good luck in your future PHP manual searches. please be sure to share your learnings with us!

As you so kindly requested, I also looked in the CI codebase for you and found that the Image Library does indeed have a function (undocumented) that can get you the data too. But, you probably don't want to rely on undocumented features... they might change.

Code:
// --------------------------------------------------------------------
    
    /**
     * Get image properties
     *
     * A helper function that gets info about the file
     *
     * @access    public
     * @param    string
     * @return    mixed
     */            
    function get_image_properties($path = '', $return = FALSE)
    {
        // For now we require GD but we should
        // find a way to determine this using IM or NetPBM
        
        if ($path == '')
            $path = $this->full_src_path;
                
        if ( ! file_exists($path))
        {
            $this->set_error('imglib_invalid_path');        
            return FALSE;                
        }
        
        $vals = @getimagesize($path);
        
        $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
        
        $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
                
        if ($return == TRUE)
        {
            $v['width']            = $vals['0'];
            $v['height']        = $vals['1'];
            $v['image_type']    = $vals['2'];
            $v['size_str']        = $vals['3'];
            $v['mime_type']        = $mime;
            
            return $v;
        }
        
        $this->orig_width    = $vals['0'];
        $this->orig_height    = $vals['1'];
        $this->image_type    = $vals['2'];
        $this->size_str        = $vals['3'];
        $this->mime_type    = $mime;
        
        return TRUE;
    }

Cheers.


getting image dimensions - El Forum - 07-03-2007

[eluser]andyd[/eluser]
Thank you so much, i have got so involved with codeigniter, that i forgot about normal php functions.

I have got it working lovely.

Andy