Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Site_model extends CI_Model {
- var $upload_path;
- var $upload_path_url;
- var $inserted_id;
- function Site_model() {
- parent::__construct();
- $this->upload_path = '/var/www/domain.com/test1/images';
- $this->upload_path_url = 'http://i.domain.com/';
- $this->base_path = 'http://domain.com/test1/';
- }
- function image_properties($image) { /* $image is full path and filename of image */
- // get some image properties
- list($width, $height, $type, $attr) = getimagesize($image);
- $details = pathinfo($image);
- $filesize = filesize($image);
- return array ( //setup mimic array to return
- 'file_name' => $details['filename'],
- 'file_ext' => $details['extension'],
- 'file_path' => $details['dirname'],
- 'full_path' => $details['dirname'].'/'.$details['basename'],
- 'raw_name' => $details['basename'],
- 'orig_name' => $details['basename'],
- 'client_name' => $details['basename'],
- 'file_ext' => $details['extension'],
- 'file_size' => $filesize,
- 'is_image' => ($details['extension'] == 'jpeg' || $details['extension'] == 'jpg' || $details['extension'] == 'gif' || $details['extension'] == 'png'),
- 'image_width' => $width,
- 'image_height' => $height,
- 'image_type' => $details['extension'],
- 'image_size_str' => $attr
- );
- }
- function isImage($img){
- if(!getimagesize($img)){
- return FALSE;
- } else {
- return TRUE;
- }
- }
- function save_from_url() {
- $url = $_POST['upload_links'];
- $config = array(
- 'allowed_types' => 'jpg|jpeg|gif|png|bmp',
- 'upload_path' => $this->upload_path,
- 'max_size' => '5120'
- );
- $this->load->helper('my_img');
- $this->load->library('image_lib', $config);
- $result = $url;
- // Valid url, valid http response
- if ($result)
- {
- $fragment = explode('/', $url);
- $file = $fragment[count($fragment)-1];
- $content = file_get_contents($url);
- if(check_image($result)) { //check it's an image
- file_put_contents($this->upload_path.'/'.$file, $content);
- $location = $this->upload_path.'/'.$file;
- //return $location;
- $img_data = $this->image_properties($location);
- var_dump($img_data);
- //process each image one by one
- $this->process_image($img_data);
- } else { //error output
- if (file_exists($location)) { unlink($location); }
- $this->session->set_flashdata('error', 'The filetype you are attempting to upload is not allowed.');
- redirect("");
- }
- }
- } //func save_from_url()
- function process_image($image_data) {
- //set all the image properties vars
- if(count($image_data) == 14 ) { //if local upload
- $dir_path = $image_data['file_path'];
- $img_path = $image_data['full_path'];
- $img_thumb = $this->upload_path . '/small/' . $image_data['file_name'];
- $img_name = $image_data['raw_name'];
- $img_type = $image_data['image_type'];
- $img_ext = strtolower($image_data['file_ext']);
- $img_width = $image_data['image_width'];
- $img_height = $image_data['image_height'];
- $file_size = $image_data['file_size'];
- } else if (count($image_data) == 13 ) { //if img remote upload
- $dir_path = $image_data['file_path'] . '/';
- $img_path = $image_data['full_path'];
- $img_ext = str_replace('.','',strtolower($image_data['file_ext']));
- $img_ext = '.' . $img_ext; //ext with dot
- $img_thumb = $this->upload_path . '/small/' . $image_data['file_name'] . $img_ext;
- $img_name = preg_replace("/\\.[^.\\s]{3,4}$/", "", $image_data['raw_name']);
- $img_type = $image_data['image_type'];
- $img_width = $image_data['image_width'];
- $img_height = $image_data['image_height'];
- $file_size = $image_data['file_size'];
- if($img_type == 'jpg') {
- $img_type = 'jpeg';
- }
- if ($file_size > 0)
- {
- $file_size = round($file_size/1024, 2);
- }
- }
- //clean up extensions
- if(starts_with($img_ext, '.png') || starts_with($img_type, 'png')) {
- $img_ext = '.png';
- $img_type = 'png';
- } else if(starts_with($img_ext, '.jpg') || starts_with($img_type, 'jpg')) {
- $img_ext = '.jpg';
- $img_type = 'jpg';
- } else if(starts_with($img_ext, '.gif') || starts_with($img_type, 'gif')) {
- $img_ext = '.gif';
- $img_type = 'jpg';
- }
- $file_name = $img_name . $img_ext;
- $img_name = uniqid();
- echo $dir_path . '<br />';
- echo $img_path . '<br />';
- echo $img_thumb . '<br />';
- echo $img_name . '<br />';
- echo $img_type . '<br />';
- echo $img_ext . '<br />';
- echo $img_width . '<br />';
- echo $img_height . '<br />';
- //remove exif data!
- if($img_ext == '.jpeg' || $img_ext == '.jpg') {
- exec('exiftool -all= ' . $img_path);
- }
- //correct jpeg naming
- if($img_ext == '.jpeg') {
- $img_ext = '.jpg';
- }
- // ***** add id, filename to database
- $db_data = array(
- 'raw_name' => $img_name,
- 'image_type' => $img_type,
- 'file_ext' => $img_ext,
- 'image_width' => $img_width,
- 'image_height' => $img_height,
- 'file_size' => $file_size,
- 'submitted' => time()
- );
- $this->db->insert('images', $db_data);
- unset($db_data);
- //add alpha filename to database
- $inserted_id = alphaID($this->db->insert_id(), false);
- $db_data = array(
- 'alpha_id' => $inserted_id
- );
- $this->db->where('id', alphaID($inserted_id, true));
- $this->db->update('images', $db_data);
- $image_data['file_name'] = $inserted_id.$img_ext;
- //compress orig to medium
- if($img_type == 'gif') {
- $config['image_library'] = 'gd2';
- } else {
- $config['image_library'] = 'imagemagick';
- $config['library_path'] = '/usr/bin/';
- }
- $config['source_image'] = $img_path;
- $config['new_image'] = $this->upload_path . '/medium/' . $image_data['file_name'];
- list($_width, $_height) = getimagesize($img_path);
- $config['width'] = $_width;
- $config['height'] = $_height;
- $config['quality'] = 80;
- //$this->load->library('image_lib');
- $this->image_lib->initialize($config);
- if( ! $this->image_lib->resize()) {
- echo $this->image_lib->display_errors();
- echo '<hr />';
- }
- unset($config);
- unset($_width);
- unset($_height);
- //compress orig to orig, 100%
- $config['image_library'] = 'gd2';
- //$config['image_library'] = 'imagemagick';
- //$config['library_path'] = '/usr/bin/';
- $config['source_image'] = $img_path;
- $config['new_image'] = $this->upload_path . '/orig/' . $image_data['file_name'];
- list($_width, $_height) = getimagesize($img_path);
- $config['width'] = $_width;
- $config['height'] = $_height;
- $config['quality'] = 100;
- //$this->load->library('image_lib');
- $this->image_lib->initialize($config);
- if( ! $this->image_lib->resize()) {
- echo $this->image_lib->display_errors();
- echo '<hr />';
- }
- unset($config);
- unset($_width);
- unset($_height);
- // resize process
- list($_width, $_height) = getimagesize($img_path);
- $config['image_library'] = 'gd2';
- //$config['image_library'] = 'imagemagick';
- //$config['library_path'] = '/usr/bin/';
- $config['source_image'] = $this->upload_path . '/orig/' . $image_data['file_name'];
- $config['new_image'] = $this->upload_path . '/small/' . $image_data['file_name'];
- $config['create_thumb'] = FALSE;
- $config['maintain_ratio'] = FALSE;
- $config['quality'] = 80;
- $img_type = '';
- $thumb_size = 160;
- if ($_width > $_height) {
- // wide image
- $config['width'] = intval(($_width / $_height) * $thumb_size);
- if ($config['width'] % 2 != 0)
- {
- $config['width']++;
- }
- $config['height'] = $thumb_size;
- $img_type = 'wide';
- } else if ($_width < $_height) {
- // landscape image
- $config['width'] = $thumb_size;
- $config['height'] = intval(($_height / $_width) * $thumb_size);
- if ($config['height'] % 2 != 0)
- {
- $config['height']++;
- }
- $img_type = 'landscape';
- } else {
- // square image
- $config['width'] = $thumb_size;
- $config['height'] = $thumb_size;
- $img_type = 'square';
- }
- $this->load->library('image_lib');
- $this->image_lib->initialize($config);
- if( ! $this->image_lib->resize()) {
- echo $this->image_lib->display_errors();
- echo '<hr />';
- }
- // cropping process for thumb
- $conf_new = array(
- 'image_library' => 'gd2',
- //'image_library' => 'imagemagick',
- //'library_path' => '/usr/bin',
- 'source_image' => $this->upload_path . '/small/' . $image_data['file_name'],
- 'new_image' => $this->upload_path . '/small/' . $image_data['file_name'],
- 'create_thumb' => FALSE,
- 'maintain_ratio' => FALSE,
- 'width' => $thumb_size,
- 'height' => $thumb_size,
- 'quality' => 80,
- );
- if ($img_type == 'wide') {
- $conf_new['x_axis'] = ($config['width'] - $thumb_size) / 2 ;
- $conf_new['y_axis'] = 0;
- } else if ($img_type == 'landscape') {
- $conf_new['x_axis'] = 0;
- $conf_new['y_axis'] = ($config['height'] - $thumb_size) / 2;
- } else {
- $conf_new['x_axis'] = 0;
- $conf_new['y_axis'] = 0;
- }
- $this->image_lib->initialize($conf_new);
- if( ! $this->image_lib->crop()) {
- echo $this->image_lib->display_errors();
- }
- //fin
- if (file_exists($img_path)) { unlink($img_path); }
- if (file_exists($img_path . '_original')) { unlink($img_path . '_original'); }
- } //func process_image
- function do_upload() {
- //do the upload!
- $config = array(
- 'allowed_types' => 'jpg|jpeg|gif|png|bmp',
- 'upload_path' => $this->upload_path,
- 'max_size' => '5120'
- );
- //only init stuff once!
- $this->load->library('upload', $config);
- $this->load->library('image_lib', $config);
- $this->load->helper('my_img');
- //go through multi uploads, re-serialize files array
- foreach($_FILES['userfile'] as $key => $file) {
- $i = 0;
- foreach ($file as $item)
- {
- $data[$i][$key] = $item;
- $i++;
- }
- }
- reset($file);
- $_FILES = $data; // re-declarate
- for($i = 0; $i < count($data); $i++) {
- if( ! $this->upload->do_upload($i)){ //if failed show errors
- //return $error;
- $this->session->set_flashdata('error', $this->upload->display_errors('',''));
- redirect("");
- } else { //ok, valid, do upload
- $image_data[$i] = $this->upload->data();
- //var_dump($image_data[$i]);
- //process each image one by one
- $this->process_image($image_data[$i]);
- }
- }
- } //func do_upload()
- function get_images() { //show uploaded images
- $this->db->order_by('id desc');
- $query = $this->db->get('images', 16);
- return $query->result();
- }
- function get_image($id) { //show uploaded images where id =
- $query = $this->db->get('images');
- $query = $this->db->get_where('images', array('id' => $id), 1);
- return $query->result();
- //return $query[1]->result(); cant use array here
- }
- function get_last() {
- $query = $this->db->query('SELECT id FROM images ORDER BY id desc LIMIT 0,1');
- $row = $query->row();
- return alphaID($row->id, false);;
- }
- function add_record() {
- //$this->db->set('images', $data);
- return;
- }
- function update_record() {
- $this->db->where('images', $id);
- $this->db->update('images', $data);
- }
- function delete_row() {
- $this->db->where('id', $this->uri->segment(3));
- $this->db->delete('images');
- }
- } //class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement