Welcome Guest, Not a member yet? Register   Sign In
Image resizing - maintain ratio but within boundaries
#4

[eluser]kaisama[/eluser]
Hmm, sounds similar to what I needed done for my application. This is what I came up with, as a function in my gallery model. Maybe you can try and use it for what you need? Will most likely need to change the coding a bit, but it should give you a start :3

Code:
function thumbnail($image, $width, $height, $align='', $master_dim='auto')
    {
        // Path to main image directory and thumbnail directory
        $full_image_directory            = '/yourdomain.com/application/assets/img/';
        $full_thumbnail_directory        = '/yourdomain.com/application/assets/img/thumbs/';
        $image_directory                = 'application/assets/img/';
        $thumbnail_directory            = 'application/assets/img/thumbs/';
        $thumbnail_folder                = $width . '_x_' . $height . '_' . $master_dim . '/';
            
        // Path to image and thumbnails
        $image_path                        = $image_directory . $image;
        $thumbnail_path                    = $thumbnail_directory . $thumbnail_folder;
        $thumbnail_path_create            = $full_thumbnail_directory . $thumbnail_folder;
        
        $this->create_folder($thumbnail_path);
        
        // Path to image thumbnail
        $image_thumb = $thumbnail_path . $image;
        
        // LOAD LIBRARY
        $this->load->library('image_lib');
    
        if( ! file_exists($image_thumb))
        {
            // CONFIGURE IMAGE LIBRARY
            $config['image_library']    = 'gd2';
            $config['source_image']        = $image_path;
            $config['new_image']        = $image_thumb;
            $config['maintain_ratio']    = TRUE;
            $config['height']            = $height;
            $config['width']            = $width;
            $config['master_dim']        = $master_dim;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
        else
        {
            // Pull the create date from the image and store to a variable
            $original_create_date = filemtime($image_path);
            
            // Pull the create date from the thumbnail image and store to a variable
            $thumbnail_create_date = filemtime($image_thumb);
            
            // Check the dates. If the original image create date is greater than the thumbnails create date, update the thumbnail image. If not, simply echo out the thumbnail.
            if ($original_create_date > $thumbnail_create_date)
            {
                // CONFIGURE IMAGE LIBRARY
                $config['image_library']    = 'gd2';
                $config['source_image']        = $image_path;
                $config['new_image']        = $image_thumb;
                $config['maintain_ratio']    = TRUE;
                $config['height']            = $height;
                $config['width']            = $width;
                $config['master_dim']        = $master_dim;
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->image_lib->clear();
            }
        }
    
        return '<img align="' . $align . '" src="' . base_url() . $image_thumb . '" />';
    }

function create_folder($folder)
    {
        if (! file_exists($folder))
        {
            $this->load->library('ftp');          
                    
            // Create the directory and CHMOD it to 777.
            $this->ftp->connect();
            $this->ftp->mkdir('yourdomain.com/'.$folder, DIR_WRITE_MODE);
            $this->ftp->close();
          }
     }


Messages In This Thread
Image resizing - maintain ratio but within boundaries - by El Forum - 12-06-2010, 08:40 PM
Image resizing - maintain ratio but within boundaries - by El Forum - 12-06-2010, 09:19 PM
Image resizing - maintain ratio but within boundaries - by El Forum - 12-06-2010, 09:34 PM
Image resizing - maintain ratio but within boundaries - by El Forum - 12-06-2010, 09:53 PM
Image resizing - maintain ratio but within boundaries - by El Forum - 12-07-2010, 02:15 AM
Image resizing - maintain ratio but within boundaries - by El Forum - 12-07-2010, 08:04 PM
Image resizing - maintain ratio but within boundaries - by El Forum - 12-08-2010, 02:40 AM



Theme © iAndrew 2016 - Forum software by © MyBB