[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();
}
}