Advertisement
Guest User

dfssssssssssssss

a guest
Jan 22nd, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.28 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Site_model extends CI_Model {
  4.    
  5.     var $upload_path;
  6.     var $upload_path_url;
  7.     var $inserted_id;
  8.     function Site_model() {
  9.         parent::__construct();  
  10.         $this->upload_path = '/var/www/domain.com/test1/images';
  11.         $this->upload_path_url = 'http://i.domain.com/';
  12.         $this->base_path = 'http://domain.com/test1/';
  13.     }
  14.  
  15.  
  16.  
  17.    
  18.     function image_properties($image) { /* $image is full path and filename of image */
  19.  
  20.         // get some image properties
  21.         list($width, $height, $type, $attr) = getimagesize($image);
  22.         $details = pathinfo($image);
  23.         $filesize = filesize($image);
  24.  
  25.         return array ( //setup mimic array to return
  26.             'file_name'         => $details['filename'],
  27.             'file_ext'          => $details['extension'],
  28.             'file_path'         => $details['dirname'],
  29.             'full_path'         => $details['dirname'].'/'.$details['basename'],
  30.             'raw_name'          => $details['basename'],
  31.             'orig_name'         => $details['basename'],
  32.             'client_name'       => $details['basename'],
  33.             'file_ext'          => $details['extension'],
  34.             'file_size'         => $filesize,
  35.             'is_image'          => ($details['extension'] == 'jpeg' || $details['extension'] == 'jpg' || $details['extension'] == 'gif' || $details['extension'] == 'png'),
  36.             'image_width'       => $width,
  37.             'image_height'      => $height,
  38.             'image_type'        => $details['extension'],
  39.             'image_size_str'    => $attr
  40.         );
  41.  
  42.     }
  43.  
  44.     function isImage($img){
  45.  
  46.         if(!getimagesize($img)){
  47.             return FALSE;
  48.         } else {
  49.             return TRUE;
  50.         }
  51.  
  52.     }
  53.    
  54.  
  55.     function save_from_url() {
  56.  
  57.         $url = $_POST['upload_links'];
  58.  
  59.         $config = array(
  60.             'allowed_types' => 'jpg|jpeg|gif|png|bmp',
  61.             'upload_path' => $this->upload_path,
  62.             'max_size' => '5120'
  63.         );
  64.  
  65.         $this->load->helper('my_img');
  66.  
  67.  
  68.         $this->load->library('image_lib', $config);
  69.  
  70.             $result = $url;
  71.             // Valid url, valid http response
  72.             if ($result)
  73.             {
  74.                 $fragment = explode('/', $url);
  75.                 $file = $fragment[count($fragment)-1];
  76.                 $content = file_get_contents($url);
  77.  
  78.                 if(check_image($result)) { //check it's an image
  79.  
  80.                 file_put_contents($this->upload_path.'/'.$file, $content);
  81.                 $location = $this->upload_path.'/'.$file;
  82.                 //return $location;
  83.  
  84.                 $img_data = $this->image_properties($location);
  85.                 var_dump($img_data);
  86.  
  87.                 //process each image one by one
  88.                 $this->process_image($img_data);
  89.  
  90.                 } else {  //error output
  91.                     if (file_exists($location)) { unlink($location); }
  92.                     $this->session->set_flashdata('error', 'The filetype you are attempting to upload is not allowed.');
  93.                     redirect("");
  94.                 }
  95.             }
  96.  
  97.     } //func save_from_url()
  98.  
  99.  
  100.  
  101.     function process_image($image_data) {
  102.  
  103.         //set all the image properties vars
  104.         if(count($image_data) == 14 ) { //if local upload
  105.                 $dir_path = $image_data['file_path'];
  106.                 $img_path = $image_data['full_path'];
  107.                 $img_thumb = $this->upload_path . '/small/' . $image_data['file_name'];
  108.                 $img_name = $image_data['raw_name'];
  109.                 $img_type = $image_data['image_type'];
  110.                 $img_ext = strtolower($image_data['file_ext']);
  111.                 $img_width = $image_data['image_width'];
  112.                 $img_height = $image_data['image_height'];
  113.                 $file_size = $image_data['file_size'];
  114.         } else if (count($image_data) == 13 ) { //if img remote upload
  115.                 $dir_path = $image_data['file_path'] . '/';
  116.                 $img_path = $image_data['full_path'];
  117.                 $img_ext = str_replace('.','',strtolower($image_data['file_ext']));
  118.                 $img_ext = '.' . $img_ext; //ext with dot
  119.                 $img_thumb = $this->upload_path . '/small/' . $image_data['file_name'] . $img_ext; 
  120.                 $img_name = preg_replace("/\\.[^.\\s]{3,4}$/", "", $image_data['raw_name']);
  121.                 $img_type = $image_data['image_type'];
  122.                 $img_width = $image_data['image_width'];
  123.                 $img_height = $image_data['image_height'];
  124.                 $file_size = $image_data['file_size'];
  125.  
  126.                 if($img_type == 'jpg') {
  127.                     $img_type = 'jpeg';
  128.                 }
  129.  
  130.                 if ($file_size > 0)
  131.                 {
  132.                     $file_size = round($file_size/1024, 2);
  133.                 }
  134.         }
  135.  
  136.         //clean up extensions
  137.         if(starts_with($img_ext, '.png') || starts_with($img_type, 'png')) {
  138.             $img_ext = '.png';
  139.             $img_type = 'png';
  140.         } else if(starts_with($img_ext, '.jpg') || starts_with($img_type, 'jpg')) {
  141.             $img_ext = '.jpg';
  142.             $img_type = 'jpg';
  143.         } else if(starts_with($img_ext, '.gif') || starts_with($img_type, 'gif')) {
  144.             $img_ext = '.gif';
  145.             $img_type = 'jpg';
  146.         }
  147.  
  148.         $file_name = $img_name . $img_ext;
  149.  
  150.         $img_name = uniqid();
  151.  
  152.         echo $dir_path . '<br />';
  153.         echo $img_path . '<br />';
  154.         echo $img_thumb . '<br />';
  155.         echo $img_name . '<br />';
  156.         echo $img_type . '<br />';
  157.         echo $img_ext . '<br />';
  158.         echo $img_width . '<br />';
  159.         echo $img_height . '<br />';
  160.  
  161.        
  162.                
  163.                 //remove exif data!
  164.                 if($img_ext == '.jpeg' || $img_ext == '.jpg') {
  165.                     exec('exiftool -all= ' . $img_path);
  166.                 }
  167.                
  168.                 //correct jpeg naming
  169.                 if($img_ext == '.jpeg') {
  170.                     $img_ext = '.jpg';
  171.                 }
  172.  
  173.                 // ***** add id, filename to database
  174.                 $db_data = array(
  175.                 'raw_name' => $img_name,
  176.                 'image_type' => $img_type,
  177.                 'file_ext' => $img_ext,
  178.                 'image_width' => $img_width,
  179.                 'image_height' => $img_height,
  180.                 'file_size' => $file_size,
  181.                 'submitted' => time()
  182.                 );
  183.                 $this->db->insert('images', $db_data);
  184.                 unset($db_data);
  185.                
  186.                 //add alpha filename to database
  187.                 $inserted_id = alphaID($this->db->insert_id(), false);
  188.                 $db_data = array(
  189.                 'alpha_id' => $inserted_id
  190.                 );
  191.                 $this->db->where('id', alphaID($inserted_id, true));
  192.                 $this->db->update('images', $db_data);
  193.  
  194.                 $image_data['file_name'] = $inserted_id.$img_ext;
  195.  
  196.                 //compress orig to medium
  197.                 if($img_type == 'gif') {
  198.                     $config['image_library'] = 'gd2';
  199.                 } else {
  200.                     $config['image_library'] = 'imagemagick';
  201.                     $config['library_path'] = '/usr/bin/';
  202.                 }
  203.                 $config['source_image'] = $img_path;
  204.                 $config['new_image'] = $this->upload_path . '/medium/' . $image_data['file_name'];
  205.                 list($_width, $_height) = getimagesize($img_path);
  206.                 $config['width'] = $_width;
  207.                 $config['height'] = $_height;
  208.                 $config['quality'] = 80;
  209.                 //$this->load->library('image_lib');
  210.                 $this->image_lib->initialize($config);
  211.                 if( ! $this->image_lib->resize()) {
  212.                     echo $this->image_lib->display_errors();
  213.                     echo '<hr />';
  214.                 }
  215.                 unset($config);
  216.                 unset($_width);
  217.                 unset($_height);
  218.                
  219.                 //compress orig to orig, 100%
  220.                 $config['image_library'] = 'gd2';
  221.                 //$config['image_library'] = 'imagemagick';
  222.                 //$config['library_path'] = '/usr/bin/';
  223.                 $config['source_image'] = $img_path;
  224.                 $config['new_image'] = $this->upload_path . '/orig/' . $image_data['file_name'];
  225.                 list($_width, $_height) = getimagesize($img_path);
  226.                 $config['width'] = $_width;
  227.                 $config['height'] = $_height;
  228.                 $config['quality'] = 100;
  229.                 //$this->load->library('image_lib');
  230.                 $this->image_lib->initialize($config);
  231.                 if( ! $this->image_lib->resize()) {
  232.                     echo $this->image_lib->display_errors();
  233.                     echo '<hr />';
  234.                 }
  235.                 unset($config);
  236.                 unset($_width);
  237.                 unset($_height);
  238.                
  239.                
  240.  
  241.                 // resize process
  242.                 list($_width, $_height) = getimagesize($img_path);
  243.                 $config['image_library'] = 'gd2';
  244.                 //$config['image_library'] = 'imagemagick';
  245.                 //$config['library_path'] = '/usr/bin/';
  246.                 $config['source_image'] = $this->upload_path . '/orig/' . $image_data['file_name'];
  247.                 $config['new_image'] = $this->upload_path . '/small/' . $image_data['file_name'];
  248.                 $config['create_thumb'] = FALSE;
  249.                 $config['maintain_ratio'] = FALSE; 
  250.                 $config['quality'] = 80;       
  251.  
  252.            
  253.                 $img_type = '';
  254.                 $thumb_size = 160;
  255.            
  256.                 if ($_width > $_height) {
  257.                     // wide image
  258.                     $config['width'] = intval(($_width / $_height) * $thumb_size);
  259.                     if ($config['width'] % 2 != 0)
  260.                     {
  261.                         $config['width']++;
  262.                     }
  263.                     $config['height'] = $thumb_size;
  264.                     $img_type = 'wide';
  265.                 } else if ($_width < $_height) {
  266.                     // landscape image
  267.                     $config['width'] = $thumb_size;
  268.                     $config['height'] = intval(($_height / $_width) * $thumb_size);
  269.                     if ($config['height'] % 2 != 0)
  270.                     {
  271.                         $config['height']++;
  272.                     }
  273.                     $img_type = 'landscape';
  274.                 } else {
  275.                     // square image
  276.                     $config['width'] = $thumb_size;
  277.                     $config['height'] = $thumb_size;
  278.                     $img_type = 'square';
  279.                 }
  280.            
  281.            
  282.                 $this->load->library('image_lib');
  283.                 $this->image_lib->initialize($config);
  284.                 if( ! $this->image_lib->resize()) {
  285.                     echo $this->image_lib->display_errors();
  286.                     echo '<hr />';
  287.                 }
  288.                
  289.                 // cropping process for thumb          
  290.                 $conf_new = array(
  291.                     'image_library' => 'gd2',
  292.                     //'image_library' => 'imagemagick',
  293.                     //'library_path' => '/usr/bin',
  294.                     'source_image' => $this->upload_path . '/small/' . $image_data['file_name'],
  295.                     'new_image' => $this->upload_path . '/small/' . $image_data['file_name'],
  296.                     'create_thumb' => FALSE,
  297.                     'maintain_ratio' => FALSE,
  298.                     'width' => $thumb_size,
  299.                     'height' => $thumb_size,
  300.                     'quality' => 80,
  301.                 );
  302.            
  303.                 if ($img_type == 'wide') {
  304.                     $conf_new['x_axis'] = ($config['width'] - $thumb_size) / 2 ;
  305.                     $conf_new['y_axis'] = 0;
  306.                 } else if ($img_type == 'landscape') {
  307.                     $conf_new['x_axis'] = 0;
  308.                     $conf_new['y_axis'] = ($config['height'] - $thumb_size) / 2;
  309.                 } else {
  310.                     $conf_new['x_axis'] = 0;
  311.                     $conf_new['y_axis'] = 0;
  312.                 }
  313.            
  314.                 $this->image_lib->initialize($conf_new);
  315.            
  316.                 if( ! $this->image_lib->crop()) {
  317.                     echo $this->image_lib->display_errors();
  318.                 }
  319.                 //fin
  320.                 if (file_exists($img_path)) { unlink($img_path); }
  321.                 if (file_exists($img_path . '_original')) { unlink($img_path . '_original'); }
  322.     } //func process_image
  323.  
  324.  
  325.  
  326.     function do_upload() {
  327.        
  328.         //do the upload!
  329.         $config = array(
  330.             'allowed_types' => 'jpg|jpeg|gif|png|bmp',
  331.             'upload_path' => $this->upload_path,
  332.             'max_size' => '5120'
  333.         );
  334.  
  335.         //only init stuff once!
  336.         $this->load->library('upload', $config);
  337.         $this->load->library('image_lib', $config);
  338.         $this->load->helper('my_img');
  339.  
  340.         //go through multi uploads, re-serialize files array
  341.         foreach($_FILES['userfile'] as $key => $file) {
  342.             $i = 0;
  343.             foreach ($file as $item)
  344.             {
  345.                 $data[$i][$key] = $item;
  346.                 $i++;
  347.             }
  348.         }
  349.  
  350.         reset($file);
  351.  
  352.         $_FILES = $data; // re-declarate
  353.         for($i = 0; $i < count($data); $i++) {
  354.             if( ! $this->upload->do_upload($i)){ //if failed show errors
  355.                 //return $error;
  356.                 $this->session->set_flashdata('error', $this->upload->display_errors('',''));
  357.                 redirect("");
  358.            
  359.                
  360.             } else { //ok, valid, do upload
  361.                 $image_data[$i] = $this->upload->data();
  362.                 //var_dump($image_data[$i]);
  363.                 //process each image one by one
  364.                 $this->process_image($image_data[$i]);
  365.             }
  366.         }
  367.     } //func do_upload()
  368.  
  369.  
  370.    
  371.     function get_images() { //show uploaded images
  372.         $this->db->order_by('id desc');
  373.         $query = $this->db->get('images', 16);
  374.  
  375.         return $query->result();
  376.     }
  377.    
  378.     function get_image($id) { //show uploaded images where id =
  379.         $query = $this->db->get('images');
  380.         $query = $this->db->get_where('images', array('id' => $id), 1);
  381.         return $query->result();
  382.         //return $query[1]->result(); cant use array here
  383.     }
  384.  
  385.     function get_last() {
  386.         $query = $this->db->query('SELECT id FROM images ORDER BY id desc LIMIT 0,1');
  387.         $row = $query->row();
  388.         return alphaID($row->id, false);;
  389.     }
  390.    
  391.    
  392.     function add_record() {
  393.         //$this->db->set('images', $data);
  394.         return;
  395.     }
  396.    
  397.     function update_record() {
  398.         $this->db->where('images', $id);
  399.         $this->db->update('images', $data);
  400.     }
  401.    
  402.     function delete_row() {
  403.         $this->db->where('id', $this->uri->segment(3));
  404.         $this->db->delete('images');       
  405.     }
  406.    
  407. } //class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement