Advertisement
Guest User

Untitled

a guest
May 21st, 2013
1,628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1. <?php
  2.     class ImageProcessor
  3.     {
  4.         protected $I;
  5.         function __construct($Gd = null)
  6.         {
  7.             if($Gd)
  8.                 $this->I = $Gd;
  9.         }
  10.        
  11.         public function LoadFile($Path)
  12.         {
  13.             $Data = @getimagesize($Path);
  14.             if(!$Data)
  15.                 return 0;
  16.             if($Data[2] == IMAGETYPE_JPEG)
  17.             {
  18.                 $this->I = imagecreatefromjpeg($Path);
  19.                 return 1;
  20.             }
  21.             if($Data[2] == IMAGETYPE_PNG)
  22.             {
  23.                 $this->I = imagecreatefrompng($Path);
  24.                 return 2;
  25.             }
  26.             return 0;
  27.         }
  28.        
  29.         public function LoadPngString($String)
  30.         {
  31.             $InFile = tempnam(sys_get_temp_dir(), 'TmpImg_');
  32.             file_put_contents($InFile, $String);
  33.             $this->I = imagecreatefrompng($InFile);
  34.             unlink($InFile);
  35.         }
  36.        
  37.         public function W()
  38.         {
  39.             return imagesx($this->I);
  40.         }
  41.         public function H()
  42.         {
  43.             return imagesy($this->I);
  44.         }
  45.         public function Aspect()
  46.         {
  47.             return $this->W() / $this->H();
  48.         }
  49.        
  50.         public function MinSize($W, $H)
  51.         {
  52.             if($this->W() < $W || $this->H() < $H)
  53.                 return 0;
  54.             return 1;
  55.         }
  56.         public function MaxSize($W, $H)
  57.         {
  58.             if($this->W() > $W || $this->H() > $H)
  59.                 return 0;
  60.             return 1;
  61.         }
  62.        
  63.        
  64.        
  65.        
  66.         public function ResizeWPreserve($Wn)
  67.         {
  68.             $Wo = $this->W();
  69.             $Ho = $this->H();
  70.            
  71.             if($Wn == $Wo)
  72.                 return $this;
  73.            
  74.             $Hn = round($Wn * $Ho / $Wo);
  75.            
  76.             $N = imagecreatetruecolor($Wn, $Hn);
  77.             imagecopyresampled($N, $this->I, 0, 0, 0, 0, $Wn, $Hn, $Wo, $Ho);
  78.            
  79.             return new ImageProcessor($N);
  80.         }
  81.         public function ResizeHPreserve($Hn)
  82.         {
  83.             $Wo = $this->W();
  84.             $Ho = $this->H();
  85.            
  86.             if($Hn == $Ho)
  87.                 return $this;
  88.            
  89.             $Wn = round($Hn * $Wo / $Ho);
  90.            
  91.             $N = imagecreatetruecolor($Wn, $Hn);
  92.             imagecopyresampled($N, $this->I, 0, 0, 0, 0, $Wn, $Hn, $Wo, $Ho);
  93.            
  94.             return new ImageProcessor($N);
  95.         }
  96.        
  97.         public function ResizeCanvas($Wn, $Hn)
  98.         {
  99.             $Wo = $this->W();
  100.             $Ho = $this->H();
  101.            
  102.             //if($Wo < $Wn || $Ho < $Hn)
  103.             //  return 0; //Za małe
  104.            
  105.            
  106.             $N = imagecreatetruecolor($Wn, $Hn);
  107.             $BG = imagecolorallocate($N, 0, 0, 0);
  108.             imagefill($N, 0, 0, $BG);
  109.            
  110.             if(($Wo == $Wn && $Ho == $Hn) || $Wo / $Ho == $Wn / $Hn)
  111.             {
  112.                 imagecopyresampled($N, $this->I, 0, 0, 0, 0, $Wn, $Hn, $Wo, $Ho);
  113.             }
  114.             if(($Wo / $Ho) < ($Wn / $Hn))
  115.             {
  116.                 $R = $Wo / $Ho;
  117.                 $Ww = $R * $Hn; //Szerokosc wycinka
  118.                 $Wd = round(($Wn - $Ww) / 2);
  119.                
  120.                 imagecopyresampled($N, $this->I, $Wd, 0, 0, 0, $Ww, $Hn, $Wo, $Ho);
  121.             }
  122.             else
  123.             {
  124.                 //Paski gora i dol
  125.                 $R = $Wo / $Ho;
  126.                 $Hw = round($Wn / $R); // Wysokosc wycinka
  127.                 $Hd = round(($Hn - $Hw) / 2);
  128.                
  129.                 imagecopyresampled($N, $this->I, 0, $Hd, 0, 0, $Wn, $Hw, $Wo, $Ho);
  130.             }
  131.  
  132.             return new ImageProcessor($N);
  133.         }
  134.         public function Cut($W, $H, $X, $Y)
  135.         {
  136.             $N = imagecreatetruecolor($W, $H);
  137.             imagecopy($N, $this->I, 0, 0, $X, $Y, $this->W(), $this->H());
  138.             return new ImageProcessor($N);
  139.         }
  140.         public function Fetch()
  141.         {
  142.             $F = $this->Save();
  143.             $D = file_get_contents($F);
  144.             unlink($F);
  145.             return $D;
  146.         }
  147.         public function Save($OutFile = '')
  148.         {
  149.             if(!$OutFile)
  150.                 $OutFile = tempnam(sys_get_temp_dir(), 'TmpImg_');
  151.             imagepng($this->I, $OutFile);
  152.             return $OutFile;
  153.         }
  154.     }
  155. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement