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

[eluser]prawn_king[/eluser]
Hello!

I have a bit of an issue wherein I need to take an uploaded image, resize it a few times for different areas and maintain the ratio, but within a maximum width and height. The image needs to be resized into boxes that are of varying shape - some more or less square, others portrait for example.

For display, I'm using CSS to centre the image horizontally/vertically in a div of fixed size but every now and again an image will burst out and be larger than the maximum dimension (happens both for width & height). It seems to be that master_dim will only take into account one or other of the sizes. While I understand that is intentional - I was wondering if there was a way of setting max boundaries?

Does any of that make sense? I have a feeling it's too late for me to be fully cohernt, so for that, I apologise!
#2

[eluser]TaylorOtwell[/eluser]
http://phpthumb.gxdlabs.com/
#3

[eluser]prawn_king[/eluser]
[quote author="TaylorOtwell" date="1291713549"]http://phpthumb.gxdlabs.com/[/quote]

Ah. I was hoping to find a way using the CI image library but if not, that looks perfect. Thanks Smile
#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();
          }
     }
#5

[eluser]Mat-Moo[/eluser]
Check out image_moo in my sig Smile
#6

[eluser]prawn_king[/eluser]
[quote author="Mat-Moo" date="1291731351"]Check out image_moo in my sig Smile[/quote]

Perfick. Exactly what I needed.
#7

[eluser]Mat-Moo[/eluser]
No probs, glad I could help!




Theme © iAndrew 2016 - Forum software by © MyBB