[eluser]charlie spider[/eluser]
Just upload the image to a folder then store the renamed file in the database.
This is my function for a bakery site where they could upload pictures of wedding cakes to a gallery. The function will resize your images so that the longest length of the image is equal to whaterver you specify for the main image as well as the thumbnail size. Although i don't do it here, I would recommend adding a timestamp to the end of the renamed file and possibly hashing the filename altogether if you are paranoid about security. This was from my first CI project so it's not as ignited as it could be. Plus I have only been coding for about a year now part-time so I'm sure there are tons of inefficiencies in here. If anyone wants to comment I would greatly appreciate the input.
Code:
function upload()
{
$this->load->helper('url');
if($this->session->userdata('logged_in'))
{
//User is logged in
$this->load->model('CMS_model');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg';
$config['overwrite'] = 'TRUE';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->cake_error($error);
}
else
{
$data = $this->upload->data();
foreach( $data as $item => $value ) { $filedata[$item] = $value; }
$next = $this->Bakery_model->next_cake_ID();
// instead of using auto_increment
$next = intval($next) + 1;
$newfile = 'cake' . '_' . $next;
rename($filedata['file_path'].$filedata['file_name'], $filedata['file_path'] . $newfile . '.jpg' );
$path_n_file = $filedata['file_path'] . $newfile . '.jpg';
$src_img = imagecreatefromjpeg($path_n_file);
$src_size = getimagesize("$path_n_file");
//set longest length for images in pixels
$thumb_size = 72;
$pic_size = 420;
$src_size[0] = $filedata['image_width'];
$src_size[1] = $filedata['image_height'];
//generate thumb
if ( $src_size[0] > $src_size[1] ) { $thumb_divisor = $src_size[0] / $thumb_size; }
else { $thumb_divisor = $src_size[1] / $thumb_size; }
$thumb_width = $src_size[0] / $thumb_divisor;
$thumb_height = $src_size[1] / $thumb_divisor;
$thumb_img = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb_img, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $src_size[0], $src_size[1]);
imagejpeg($thumb_img, $filedata['file_path'] . $newfile . '_thumb.jpg', 100);
imagedestroy($thumb_img);
// generate main pic
if ( $src_size[0] > $src_size[1] ) { $pic_divisor = $src_size[0] / $pic_size; }
else { $pic_divisor = $src_size[1] / $pic_size; }
$pic_width = $src_size[0] / $pic_divisor;
$pic_height = $src_size[1] / $pic_divisor;
$pic_img = imagecreatetruecolor($pic_width, $pic_height);
imagecopyresampled($pic_img, $src_img, 0, 0, 0, 0, $pic_width, $pic_height, $src_size[0], $src_size[1]);
imagejpeg($pic_img, $filedata['file_path'] . $newfile . '.jpg', 100);
imagedestroy($src_img);
imagedestroy($pic_img);
$this->Bakery_model->insert_cake($next, $newfile);
redirect('weddingcakes/cake_cms', 'refresh');
}
}
else
{
//User is not logged in
redirect('cms/login', 'refresh');
}
}
The model stuff is super basic, although I use the active record method now cuz it's so much easier:
Code:
function insert_cake($ID, $cake_filename)
{
$data = array( 'cake_ID' => $ID, 'cake_filename' => $cake_filename );
$this->db->insert('tb_cake', $data);
}
function next_cake_ID()
{
$sql = "SELECT cake_ID FROM tb_cake";
$query = $this->db->query($sql);
if ($query->num_rows() > 0)
{
$row = $query->last_row();
return $row->cake_ID;
}
}