Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- setlocale(LC_ALL, 'czech'); // záleží na použitém systému
- class photoUploadManager{
- protected $registry;
- private $file_addres = array();
- private $file_name = array();
- private $photo_dir = 'photos/';
- private $max_width;
- private $max_height;
- function __construct($registry){
- $this->registry = $registry;
- }
- /**
- * ošetření vstupu
- * převedení UTF 8 na ansii (zbavení diakritiky)
- * odstranění určitých znaků
- *
- * @param mixed $string
- */
- private function treatment($string){
- $string = str_replace(' ','_',$string);
- $string = str_replace('"','',$string);
- $string = str_replace('\'','',$string);
- $string = str_replace('#','',$string);
- $string = str_replace('?','',$string);
- $string = str_replace('!','',$string);
- $string = iconv("utf-8", "ASCII//TRANSLIT", $string);
- return $string;
- }
- /**
- * otestování zda existuje defaultní složka pro fotky
- */
- public function exists_photo_folder(){
- if(is_dir($this->photo_dir.date('m_Y'))){
- $this->set_folder($this->photo_dir.date('m_Y'));
- return true;
- }
- else{
- if($this->exists_folder()){
- if(mkdir($this->photo_dir.date('m_Y'))){
- $this->set_folder($this->photo_dir.date('m_Y'));
- return true;
- }
- }
- else{
- if(mkdir($this->photo_dir))
- {
- if(mkdir(date('m_Y'))){
- $this->set_folder($this->photo_dir.date('m_Y'));
- return true;
- }
- }
- }
- }
- return false;
- }
- /**
- * otestování zda existuje výchozí složka pro fotky
- */
- private function exists_folder(){
- if(is_dir($this->photo_dir)){
- return true;
- }
- else{
- if(mkdir($this->photo_dir)){
- return true;
- }
- }
- return false;
- }
- public function save_photo($tmp_name,$name,$type,$size){
- $image_info = getimagesize($tmp_name.'/'.$name);
- $image_info_big = $this->resize($image_info,$this->max_width,$this->max_height);
- $image_info_small = $this->resize($image_info,150,150);
- $data = file_get_contents($tmp_name.'/'.$name);
- $file_name = date('d_H_i_s').'.jpg';
- $small_photo['name'] = 's'.$file_name;
- $small_photo['link'] = $this->photo_dir. $small_photo['name'];
- $small_photo['size'] = file_put_contents($small_photo['link'],$data);
- $small_photo['width'] = $image_info_small[0];
- $small_photo['height'] = $image_info_small[1];
- $data = file_get_contents($tmp_name.'/'.$name);
- $big_photo['name'] = 'b'.$file_name;
- $big_photo['link'] = $this->photo_dir.$big_photo['name'];
- $big_photo['size'] = file_put_contents($big_photo['link'],$data);
- $big_photo['width'] = $image_info_big[0];
- $big_photo['height'] = $image_info_big[1];
- return array('original'=>$big_photo,'smaller'=>$small_photo);
- }
- /**
- * nastavení výchozí složky pro fotky
- * @param mixed $folder
- */
- public function set_folder($folder){
- $folder = $this->treatment($folder);
- if(substr($folder,-1)== "/"){
- $this->photo_dir = $folder;
- }
- else{
- $this->photo_dir = $folder."/";
- }
- }
- private function resize($image_info,$new_height,$new_width){
- if($image_info[1] > $image_info[0]){
- $height = $new_height;
- $counter = $image_info[1] / $new_height;
- $width = floor($image_info[0] /$counter);
- }
- else{
- $width = $new_width;
- $counter = $image_info[0] / $new_width;
- $height = floor($image_info[1] / $counter);
- }
- $image_info[0]=$width;
- $image_info[1]=$height;
- $image_info[3]="width=\"$width\" height=\"$height\"";
- return $image_info;
- }
- public function set_resolution($width = 1024,$height = 768){
- $this->max_height = floor($height);
- $this->max_width = floor($width);
- }
- }
- $a = new photoUploadManager('aa');
- $a->set_folder('photo');
- $a->exists_photo_folder();
- $a->set_resolution(1024,768);
- echo "<pre>";
- var_dump($a->save_photo('./','photo.jpg','',1024));
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement