[eluser]RMinor[/eluser]
Hello,
I am trying to upload a photo, re-size it so that the smallest size (width or height) is 100px, and then crop the re-sized photo to 100px x 100px from the center. I am doing this to create square thumbnails while still trying to get as much of the photo as I can in the thumbnail. For some reason the photo I am getting is mostly black with a small area of the photo in the upper left corner (The photo I am uploading is 152px x 264px). My code is below. Can anyone help me out with this and see where I am messing up?
Code:
/**
* Method to add a gallery
*/
public function add()
{
$data['page_info'] = $this->Global_model->pageInfo($this->_page);
$data['gallery_saved'] = FALSE;
$data['photo_saved'] = FALSE;
$this->load->helper('form');
if ($this->input->post('submit')) {
$this->load->library('form_validation');
$this->form_validation->set_rules('gallery_title', 'Title', 'trim|required');
if ($this->form_validation->run() == FALSE) {} else {
$insert_data = array();
$insert_data['gallery_title'] = $this->input->post('gallery_title');
$insert_data['gallery_person'] = $this->session->userdata('person_id');
// Upload photo
$this->load->library('upload');
$config['upload_path'] = './uploads/photo/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->upload->initialize($config);
if (!$this->upload->do_upload('gallery_photo')) {
$data['upload_errors'] = array('error' => $this->upload->display_errors());
} else {
$this->load->library('image_lib');
$photo_info = $this->upload->data();
// Resize to max of 100
$config['source_image'] = $photo_info['full_path'];
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = FALSE;
$config['new_image'] = './uploads/photo/' . $photo_info['file_name'];
if ($photo_info['image_width'] > $photo_info['image_height']) {
$config['height'] = 100;
} else {
$config['width'] = 100;
}
$this->image_lib->initialize($config);
if ($this->image_lib->resize()) {
// Create thumbnail version
$this->load->library('image_lib');
$config['source_image'] = './uploads/photo/' . $photo_info['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['new_image'] = './uploads/photo/' . $photo_info['file_name'];
$config['width'] = 100;
$config['height'] = 100;
if ($photo_info['image_width'] > $photo_info['image_height']) {
$config['x_axis'] = (($photo_info['image_width'] / 2) - ($config['width'] / 2));
} else {
$config['y_axis'] = (($photo_info['image_height'] / 2) - ($config['height'] / 2));
}
$this->image_lib->initialize($config);
if ($this->image_lib->crop()) {
$photo = $photo_info['file_name'];
// Save new gallery information
if ($this->Person_model->addGallery($insert_data)) {
$data['gallery_saved'] = TRUE;
}
// Save new gallery photo
if ($this->Person_model->saveGalleryPhoto($this->session->userdata('person_id'), $photo)) {
$data['photo_saved'] = TRUE;
}
$this->image_lib->clear();
} else {
$data['thumb_errors'] = array('error' => $this->image_lib->display_errors());
}
} else {
$data['resize_errors'] = array('error' => $this->image_lib->display_errors());
}
}
}
}
// Load the required view and data
$this->load->view('profile/add-gallery_view', $data);
}