My CodeIgniter Image Uploader.
photo_model.php
<?php
class Photo_model extends CI_Model {
private $photo_path;
private $photo_path_url;
public $result;
public $photo_uploaded;
function __construct() {
parent::__construct();
$this->photo_path = realpath(APPPATH . "public/img/");
$this->photo_path_url = base_url() . 'application/public/img/';
$this->result = "";
$this->load->library('image_lib');
}
function do_upload() {
// configure and copy the original size photo
$config_original = array(
'encrypt_name' => TRUE,
'allowed_types' => 'jpg|gif|png',
'upload_path' => $this->photo_path . '/original',
'max_size' => 2048
);
$this->load->library('upload', $config_original);
// if the upload is successfull! begin adding to db.
if ($this->upload->do_upload()) {
// initialize some data after upload
$image_data = $this->upload->data();
$this->photo_uploaded = $image_data;
// make a better crop
$result = $this->resize($image_data['full_path'], $this->photo_path . '/thumbs/' . $image_data['file_name'], '180','121');
if ($result != 'success') {
$this->result = $result;
return false;
}
// configure and copy the thumbnail size photo
// $config_thumb = array(
// 'source_image' => $image_data['full_path'],
// 'new_image' => $this->photo_path . '/thumbs',
// 'create_thumb' => false,
// 'maintain_ratio' => true,
// 'width' => 180,
// 'height' => 121
// );
// $this->image_lib->initialize($config_thumb);
// $this->image_lib->resize();
// configure and copy the preview size photo
$config_preview = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->photo_path . '/preview',
'maintain_ratio' => true,
'create_thumb' => false,
'width' => 720,
'height' => 720
);
$this->image_lib->initialize($config_preview);
$this->image_lib->resize();
// save photo details to database
$this->set_database_info($image_data);
return true;
} else {
return false;
}
}
public function resize($oldFile, $newFile, $width, $height)
{
/*
* Resize image
*/
$config['source_image'] = $oldFile;
$config['new_image'] = $newFile;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';
$config['width'] = $width + 2;
$config['height'] = $height + 2;
$this->image_lib->initialize($config);
if ($this->image_lib->resize() == false){
return $this->CI->image_lib->display_errors();
die();
}
$size = $this->_get_size($newFile);
unset($config); // clear $config
/*
* Crop image in weight, height
*/
$config['source_image'] = $newFile;
$config['maintain_ratio'] = FALSE;
$config['width'] = $width;
$config['height'] = $height;
$config['y_axis'] = round(($size['height'] - $height) / 2);
$config['x_axis'] = 0;
$this->image_lib->clear();
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
return $this->image_lib->display_errors();
die();
}
return 'success';
}
private function _get_size($image) {
$img = getimagesize($image);
return Array('width'=>$img['0'], 'height'=>$img['1']);
}
function get_photos() {
$files = scandir($this->photo_path . '\thumbs\\' );
$files = array_diff($files, array('.', '..', 'thumbs', 'original', 'preview', 'Thumbs.db'));
$images = array();
foreach ($files as $file) {
$images []= array (
'url' => $this->photo_path_url . 'preview/' . $file,
'thumb_url' => $this->photo_path_url . 'thumbs/' . $file,
'original_url' => $this->photo_path_url . 'original/' . $file,
'file' => $this->get_filename_only($file)
);
}
return $images;
}
function get_photo($id) {
$this->db->select('*');
$this->db->from('photo');
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() == 1) {
foreach ($query->result() as $row) {
return $row;
}
} else {
return 0;
}
}
private function get_filename_only($filepath){
$info = pathinfo($filepath);
return basename($filepath, '.' . $info['extension']);
}
private function get_file_ext_only($filepath){
$info = pathinfo($filepath);
return $info['extension'];
}
private function set_database_info($image_data){
$new_photo_insert_data = array(
'id' => $image_data['raw_name'],
'original' => $image_data['orig_name'],
'extension' => $image_data['file_ext'],
'title' => $this->input->post('title'),
'description' => $this->input->post('description')
);
$insert = $this->db->insert('photo', $new_photo_insert_data);
return $insert;
}
}