[eluser]CoffeeBeanDesign[/eluser]
For anyone who might be interested. I reversed the image process so it goes like this :
- resize to max dimensions
- resize a copy to thumb height/width (depending on orientation)
- square off the thumb
I think this is the most efficient order to do things. I also added the filesize function to 'add_to_store' to check the file exists and also to get the new size after it has been shrunk.
Code:
// --------------------------------------------------------------------
/**
* Add file to store
*
* @access public
* @param field_name
* @param type
* @return boolean/string
*/
function add_to_store($field_name = '', $type = 2)
{
$CI =& get_instance();
$config['file_name'] = time() . rand(10,99);
$config['upload_path'] = './' . $this->config['store'];
$config['max_size'] = $this->config['max_size'] * 1024;
$config['allowed_types'] = ($type == 1) ? 'gif|jpg|png' :
'csv|exe|psd|pdf|ai|eps|php|js|swf|xhtml|zip|mp3|wav|bmp|gif|jpeg|jpg|png|tiff|tif|css|html|htm|shtml|txt|text|mov|avi|doc|docx|word';
$CI->load->library('upload', $config);
if ( ! $CI->upload->do_upload($field_name)) :
return $CI->upload->display_errors('','');
else :
$data = $CI->upload->data();
if($type == 1) :
$data['thumb_name'] = str_replace($data['file_ext'], '' , $data['file_name']) . '_th' . $data['file_ext'];
$process_result = $this->process_image($data);
if($process_result !== TRUE) return $process_result;
$new_size = @filesize($data['full_path']);
if( ! $new_size) return 'Image upload failed.';
$data['file_size'] = $new_size;
return $data;
else :
return $data;
endif;
endif;
}
// --------------------------------------------------------------------
/**
* Process Image - create square thumb and resize
*
* @access public
* @param data
* @return boolean/string
*/
function process_image($data = '')
{
if($data == '') return FALSE;
$ori = ($data['image_height'] > $data['image_width'] ) ? 'portrait' : 'landscape';
$ratio = ($ori == 'portrait') ? $data['image_height'] / $data['image_width'] : $data['image_width'] / $data['image_height'];
$thumb_size = $this->config['thumb_size'];
$max_height = $this->config['image_height'];
$max_width = $this->config['image_width'];
$CI =& get_instance();
$CI->load->library('image_lib');
$config['source_image'] = $data['full_path'];
$config['maintain_ratio'] = TRUE;
$config['height'] = $max_height;
$config['width'] = $max_width;
$CI->image_lib->initialize($config);
if( ! $CI->image_lib->resize()) return $CI->image_lib->display_errors('','');
$CI->image_lib->clear();
unset($config);
$config['source_image'] = $data['full_path'];
$config['new_image'] = $data['file_path'] . $data['thumb_name'];
$config['maintain_ratio'] = TRUE;
$config['quality'] = 90;
if($ori == 'landscape') $config['height'] = $thumb_size;
if($ori == 'portrait') $config['width'] = $thumb_size;
$CI->image_lib->initialize($config);
if( ! $CI->image_lib->resize()) return $CI->image_lib->display_errors('','');
$CI->image_lib->clear();
unset($config);
if ($ori == 'portrait') :
$height = ($thumb_size * $ratio);
$config['x_axis'] = 0;
$config['y_axis'] = ($height - $thumb_size) / 2;
else :
$width = ($thumb_size * $ratio);
$config['y_axis'] = 0;
$config['x_axis'] = ($width - $thumb_size) / 2;
endif;
$config['height'] = $thumb_size;
$config['width'] = $thumb_size;
$config['source_image'] = $data['file_path'] . $data['thumb_name'];
$config['maintain_ratio'] = FALSE;
$CI->image_lib->initialize($config);
if( ! $CI->image_lib->crop()) return $CI->image_lib->display_errors('','');
return TRUE;
}