[eluser]Agung Wahyudiono[/eluser]
Hello, all
At this time i will share my first library on Code Igniter
But first , i want to say sorry because of my bad English.
I build this library because i want to use Jcrop ( Jquery Plugins for cropping and uploading Image ) as image cropper. This is verry usefull when i want to create thumbnail in specific size with still best proportion. with this library , we could make more than one upload and crop process ...
so take alook for the code
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Jcrop {
private $CI;
private $prefix;
private $folder;
private $target_w;
private $target_h;
private $form = '';
private $create_thumb = FALSE;
private $thumb_folder;
public function __construct(){
$this->CI = & get_instance();
$this->CI->load->library('session');
$this->CI->load->helper('file');
}
public function set_data($set){
if (is_array($set)){
if(array_key_exists('prefix',$set)){$this->prefix = $set['prefix'];};
if(array_key_exists('folder',$set)){$this->folder = $set['folder'];};
if(array_key_exists('target_w',$set)){$this->target_w = $set['target_w'];};
if(array_key_exists('target_h',$set)){$this->target_h = $set['target_h'];};
if(array_key_exists('create_thumb',$set)){$this->create_thumb=$set['create_thumb'];};
if(array_key_exists('thumb_folder',$set)){$this->thumb_folder=$set['thumb_folder'];};
}else{
echo "Set of data not valid";
}
}
public function uploading(& $status = ''){
$config['upload_path'] = $this->folder;
$config['allowed_types'] = 'gif|jpeg|jpg|png';
$config['max_size'] = '2048';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->CI->load->library('upload',$config);
if(!$this->CI->upload->do_upload($this->prefix.'picture')) {
$status = $this->CI->upload->display_errors('<p>','</p><br />');
} else {
$upload_status = array($this->prefix.'uploaded'=>TRUE);
foreach($this->CI->upload->data() as $key => $value ) {
$imgdata[$this->prefix.$key] = $value;
}
$imgdata = array_merge($imgdata,$upload_status);
$this->CI->session->set_userdata($imgdata);
}
}
THis is part of code.
There are two function : first setdata and the second is uploading.
This function is used to set the data properties , and the uploading function is used to upload the image that using CI core.I used Prefix and session on this library. So if you upload a picture but you not save or doing the process on the picture , you will still see the picture on your screen and it s prevent you to upload another picture.
Code:
public function is_uploaded(& $thepicture,& $orig_w,& $orig_h,& $ratio){
if($this->CI->session->userdata($this->prefix.'uploaded') == false){
return false;
}else{
$orig_w = 677;
$orig_h = ($this->CI->session->userdata($this->prefix.'image_height')/$this->CI->session->userdata($this->prefix.'image_width'))* $orig_w;
$ratio = $this->target_w/$this->target_h;
$imgconf['image_library'] = 'gd2';
$imgconf['source_image'] = $this->folder.$this->CI->session->userdata($this->prefix.'orig_name');
$imgconf['maintain_ratio'] = TRUE;
if($this->CI->session->userdata($this->prefix.'image_width') >= $orig_w) {
$imgconf['width'] = $orig_w;
$imgconf['height'] = $orig_h;
} elseif ($this->CI->session->userdata($this->prefix.'image_width') < $orig_w) {
$orig_w = $this->CI->session->userdata($this->prefix.'image_width');
$orig_h = $this->CI->session->userdata($this->prefix.'image_height');
}
$this->CI->load->library('image_lib',$imgconf);
$this->CI->image_lib->resize();
$thepicture = base_url().$this->folder.$this->CI->session->userdata($this->prefix.'orig_name');
return $thepicture;
return $orig_w;
return $orig_h;
return $ratio;
return true;
}
}
public function cancel() {
if(file_exists($this->CI->session->userdata($this->prefix.'full_path'))){
unlink($this->CI->session->userdata($this->prefix.'full_path'));
}
$this->end_session();
}
on Above, we could see another 2 function.
First is_uploaded : use to detect that we have already upload a picture or not
Second cancel : use to break the session.