Advertisement
Tirus

photogalery - upload

Jan 24th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. setlocale(LC_ALL, 'czech'); // záleží na použitém systému
  3.  
  4.   class photoUploadManager{
  5.       protected $registry;
  6.       private $file_addres = array();
  7.       private $file_name = array();
  8.       private $photo_dir = 'photos/';
  9.       private $max_width;
  10.       private $max_height;
  11.      
  12.       function __construct($registry){
  13.           $this->registry = $registry;
  14.       }
  15.      
  16.       /**
  17.       * ošetření vstupu
  18.       * převedení UTF 8 na ansii (zbavení diakritiky)
  19.       * odstranění určitých znaků
  20.       *
  21.       * @param mixed $string
  22.       */
  23.       private function treatment($string){
  24.           $string = str_replace(' ','_',$string);
  25.           $string = str_replace('"','',$string);
  26.           $string = str_replace('\'','',$string);
  27.           $string = str_replace('#','',$string);
  28.           $string = str_replace('?','',$string);
  29.           $string = str_replace('!','',$string);
  30.           $string = iconv("utf-8", "ASCII//TRANSLIT", $string);
  31.                                                          
  32.           return $string;
  33.       }
  34.      
  35.       /**
  36.       * otestování zda existuje defaultní složka pro fotky      
  37.       */
  38.       public function exists_photo_folder(){
  39.           if(is_dir($this->photo_dir.date('m_Y'))){
  40.               $this->set_folder($this->photo_dir.date('m_Y'));
  41.               return true;
  42.           }
  43.           else{
  44.               if($this->exists_folder()){
  45.                 if(mkdir($this->photo_dir.date('m_Y'))){
  46.                   $this->set_folder($this->photo_dir.date('m_Y'));
  47.                   return true;
  48.                 }
  49.               }
  50.               else{
  51.                   if(mkdir($this->photo_dir))
  52.                   {
  53.                     if(mkdir(date('m_Y'))){
  54.                       $this->set_folder($this->photo_dir.date('m_Y'));
  55.                       return true;
  56.                     }
  57.                   }
  58.               }            
  59.           }
  60.           return false;
  61.       }
  62.      
  63.       /**
  64.       * otestování zda existuje výchozí složka pro fotky
  65.       */
  66.       private function exists_folder(){
  67.           if(is_dir($this->photo_dir)){
  68.               return true;
  69.           }
  70.           else{
  71.             if(mkdir($this->photo_dir)){
  72.                 return true;
  73.             }
  74.           }
  75.           return false;
  76.       }
  77.      
  78.  
  79.       public function save_photo($tmp_name,$name,$type,$size){
  80.          
  81.           $image_info = getimagesize($tmp_name.'/'.$name);
  82.           $image_info_big = $this->resize($image_info,$this->max_width,$this->max_height);
  83.           $image_info_small = $this->resize($image_info,150,150);
  84.           $data = file_get_contents($tmp_name.'/'.$name);
  85.  
  86.        
  87.           $file_name = date('d_H_i_s').'.jpg';
  88.           $small_photo['name'] = 's'.$file_name;
  89.           $small_photo['link'] = $this->photo_dir. $small_photo['name'];
  90.           $small_photo['size'] = file_put_contents($small_photo['link'],$data);
  91.           $small_photo['width'] = $image_info_small[0];
  92.           $small_photo['height'] = $image_info_small[1];
  93.                    
  94.            $data = file_get_contents($tmp_name.'/'.$name);
  95.           $big_photo['name'] = 'b'.$file_name;
  96.           $big_photo['link'] = $this->photo_dir.$big_photo['name'];
  97.           $big_photo['size'] = file_put_contents($big_photo['link'],$data);
  98.           $big_photo['width'] = $image_info_big[0];
  99.           $big_photo['height'] = $image_info_big[1];
  100.          
  101.          
  102.          
  103.           return array('original'=>$big_photo,'smaller'=>$small_photo);
  104.       }
  105.      
  106.       /**
  107.       * nastavení výchozí složky pro fotky
  108.       * @param mixed $folder
  109.       */
  110.       public function set_folder($folder){
  111.           $folder = $this->treatment($folder);
  112.           if(substr($folder,-1)== "/"){
  113.               $this->photo_dir = $folder;
  114.           }
  115.           else{
  116.               $this->photo_dir = $folder."/";
  117.           }                                          
  118.       }
  119.      
  120.       private function resize($image_info,$new_height,$new_width){
  121.          
  122.           if($image_info[1] > $image_info[0]){
  123.               $height = $new_height;
  124.               $counter = $image_info[1] / $new_height;
  125.               $width = floor($image_info[0] /$counter);
  126.           }
  127.           else{
  128.               $width = $new_width;
  129.               $counter = $image_info[0] / $new_width;
  130.               $height = floor($image_info[1] / $counter);
  131.           }
  132.           $image_info[0]=$width;
  133.           $image_info[1]=$height;
  134.           $image_info[3]="width=\"$width\" height=\"$height\"";
  135.           return $image_info;
  136.          
  137.          
  138.       }
  139.      
  140.       public function set_resolution($width = 1024,$height = 768){
  141.           $this->max_height = floor($height);
  142.           $this->max_width = floor($width);
  143.       }
  144.      
  145.   }
  146.  
  147.   $a = new photoUploadManager('aa');
  148.   $a->set_folder('photo');
  149.   $a->exists_photo_folder();
  150.   $a->set_resolution(1024,768);
  151.   echo "<pre>";
  152.   var_dump($a->save_photo('./','photo.jpg','',1024));
  153.   echo "</pre>";
  154.  
  155.  
  156. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement