[eluser]Stompfrog[/eluser]
I have almost achieved what I wanted.
I have 3 functions.
avatar() Uploads an image and saves it into the avatar folder.
avatar2() does some maths and then crops the central square of the rectangular image.
avatar3() then resizes the central square to be 60px x 60px
The code is as follows...
Code:
function avatar(){
$data['title'] = "My avatar Title";
$config['upload_path'] = './avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('static/header', $data);
$this->load->view('avatar_view', $error);
$this->load->view('static/footer');
}
else{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$this->avatar2($upload_data);
}
}
function avatar2($upload_data){
$width = $upload_data['image_width'] ;
$height = $upload_data['image_height'] ;
$config[] = 'GD2';
$config['source_image'] = "./avatars/".$upload_data['file_name'];
$config['maintain_ratio'] = FALSE;
$config['quality'] = 100;
if($height>$width){
$config['x_axis'] = 0;
$config['y_axis'] = ($height-$width)/2;
$config['height'] = $width;
$config['width'] = $width;
}else{
$config['y_axis'] = 0;
$config['x_axis'] = ($width-$height)/2;
$config['height'] = $height;
$config['width'] = $height;
}
$this->load->library('image_lib', $config);
$this->image_lib->crop();
$this->avatar3($upload_data);
}
function avatar3($upload_data){
$config[] = 'GD2';
$config['source_image'] = "./avatars/".$upload_data['file_name'];
$config['new_image'] = 'thisisatest.jpg';
$config['maintain_ratio'] = TRUE;
$config['height'] = 60;
$config['width'] = 60;
$config['quality'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
If i submit the upload form with this code i end up with a single 300px x 300px image in the avatar folders, this is great, it means avatar() and avatar2() are doing there job. avatar3() appears to be failing tho as the image is not 60px x 60px.
It I tweak avatar() to make a call to avatar3() instead of avatar2() I get the expected 60px x 30px "thisisatest.jpg".
To summarise:
avatar() --> avatar2() works
avatar() --> avatar3() works
avatar() --> avatar2() --> avatar3() doesn't work
In this third scenario there are no errors it just doesn't do the last step of the image processing.
Any ideas?