Autor Zpráva
Tirus
Profil
Jak na toto?
změna rozměrů obrázků by se dala udělat asi pomocí getimagesize, ovšem google nebyl moc moudrý...
následně bych potřeboval poradit co vše u obrázků kontrolovat a jak ošetřit aby se tam nedostalo to co nemá. (snažím se udělat fotogalerii)...

Jak nejlépe zmenšovat abych obrázek nezdeformoval?
<?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;
      }
      

      private 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);
          file_get_contents($tmp_name.'/'.$name);
                    
          
          $small_photo['name'] = 's'.date('d_H_i_s').'.jpg';
          $small_photo['link'] = $this->photo_dir. $small_photo['name'];
          $small_photo['size'] = file_put_contents($small_photo['link'],$small_photo['data']);
          $small_photo['width'] = $image_info_small[0];
          $small_photo['height'] = $image_info_small[1];
                   
          
          $big_photo['name'] = 'b'.date('d_H_i_s').'.jpg';
          $big_photo['link'] = $this->photo_dir.'b'.$big_photo['name'];
          $big_photo['size'] = file_put_contents($big_photo['link'],$big_photo['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,$height){
          $this->max_height = floor($height);
          $this->max_width = floor($width);
      }
      
  }
btw. jedna metoda je tam zbytečně (vím to, odeberu ji)
Alphard
Profil
Tirus:
Jaká přesně je otázka, kde je problém? Nebo hrajeme hru najdi zbytečnou metodu?
Zmenšení obrázků je milionkrát vyřešené, odkázané i v PHP FAQ a to http://php.vrana.cz/zmensovani-obrazku.php nic se nedeformuje, rozměry se spočítají dle požadavků.
Jak nejlépe záleží na kritériích, pokud jde o deformace, je odkázaný způsob dostačující, máte-li k dispozici ImageMagick, zpracujete i obrázky větší, než je paměťový limit PHP, a můžete je pak snadno i doostřit.

U ošetřování se to většinou až tak neřeší, co projde funkcí getimagesize() se považuje za obrázek. Z bezpečnostních důvodů to nesmí mít název *.php ani jiný spustitelný (do obrázku lze schovat <?php #neco znic; ?>).

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0