• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Added to Wiki: image resizing and squaring library

#19
[eluser]GDmac - expocom[/eluser]
It looks like SquareThumb first crops and then resizes.
Am i correct that this might cause a bit overhead for large images?
(e.g. it first crops and saves 1920x1024 to 1024x1024 and then resizes and saves to 120x120).

Suggestion: resize first with smallest side set as "master_dim" setting.

Resize and crop
If you want fixed-size thumbs (not necessarily squared) here is a resize_and_crop() method.
First, change the resize method to include master_dim
Code:
function resize ($originalFile, $newFile = '', $newWidth = 120, $newHeight = 120, $enlarge = FALSE, $master_dim='auto')
....
$config['master_dim'] = $master_dim;
And here is resize_and_crop:
Code:
function resize_and_crop($originalFile, $newFile = '', $newWidth = 120, $newHeight = 120)
    {

        // Abort if image does not exist
        if (! file_exists($originalFile) || ! is_file($originalFile)) {
            return FALSE;
        }

        // Get original image data
        $imgData = $this->getSize($originalFile);

        $source_ratio = $imgData['width'] / $imgData['height'];
        $wanted_ratio = $newWidth / $newHeight;
        
        // resize acordding to smallest side
        $this->resize($originalFile, $newFile, $newWidth, $newHeight, TRUE, ( $source_ratio > $wanted_ratio ? 'height' : 'width') );

        // crop newFile
        $config['image_library'] = 'GD2';
        $config['library_path'] = null;
        $config['source_image'] = $newFile;
        $config['maintain_ratio'] = FALSE;
        $config['quality'] = 95;

        // get dimensions after resize
        $imgData = $this->getSize($newFile);

        if ($imgData['width']>=$newWidth || $imgData['height']>=$newHeight)
        {
            $config['width'] = $newWidth;
            $config['x_axis'] = ( $imgData['width'] - $newWidth ) / 2;
        
            $config['height'] = $newHeight;
            $config['y_axis'] = ( $imgData['height'] - $newHeight ) / 2;
        
            // Crop image
            $this->_ci->image_lib->initialize($config);
            
            if (! $this->_ci->image_lib->crop() )
            {
                show_error($this->_ci->image_lib->display_errors());
            }
            
            // Clear lib so we can perform another image action
            $this->_ci->image_lib->clear();
        }
    }


Messages In This Thread
Added to Wiki: image resizing and squaring library - by El Forum - 12-07-2009, 05:29 AM
Added to Wiki: image resizing and squaring library - by El Forum - 02-03-2010, 03:16 PM
Added to Wiki: image resizing and squaring library - by El Forum - 02-04-2010, 05:12 AM
Added to Wiki: image resizing and squaring library - by El Forum - 02-04-2010, 07:29 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-07-2010, 03:02 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-07-2010, 03:46 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-07-2010, 05:08 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-08-2010, 02:22 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-08-2010, 09:37 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-08-2010, 10:06 AM
Added to Wiki: image resizing and squaring library - by El Forum - 04-21-2011, 05:55 AM
Added to Wiki: image resizing and squaring library - by El Forum - 04-27-2011, 05:04 AM
Added to Wiki: image resizing and squaring library - by El Forum - 04-27-2011, 05:11 AM
Added to Wiki: image resizing and squaring library - by El Forum - 04-27-2011, 05:40 AM
Added to Wiki: image resizing and squaring library - by El Forum - 04-27-2011, 06:29 AM
Added to Wiki: image resizing and squaring library - by El Forum - 04-28-2011, 06:36 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-24-2011, 01:14 AM
Added to Wiki: image resizing and squaring library - by El Forum - 06-24-2011, 01:27 AM
Added to Wiki: image resizing and squaring library - by El Forum - 07-18-2011, 11:32 AM
Added to Wiki: image resizing and squaring library - by El Forum - 05-06-2012, 01:14 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.