Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ImageProcessor
- {
- protected $I;
- function __construct($Gd = null)
- {
- if($Gd)
- $this->I = $Gd;
- }
- public function LoadFile($Path)
- {
- $Data = @getimagesize($Path);
- if(!$Data)
- return 0;
- if($Data[2] == IMAGETYPE_JPEG)
- {
- $this->I = imagecreatefromjpeg($Path);
- return 1;
- }
- if($Data[2] == IMAGETYPE_PNG)
- {
- $this->I = imagecreatefrompng($Path);
- return 2;
- }
- return 0;
- }
- public function LoadPngString($String)
- {
- $InFile = tempnam(sys_get_temp_dir(), 'TmpImg_');
- file_put_contents($InFile, $String);
- $this->I = imagecreatefrompng($InFile);
- unlink($InFile);
- }
- public function W()
- {
- return imagesx($this->I);
- }
- public function H()
- {
- return imagesy($this->I);
- }
- public function Aspect()
- {
- return $this->W() / $this->H();
- }
- public function MinSize($W, $H)
- {
- if($this->W() < $W || $this->H() < $H)
- return 0;
- return 1;
- }
- public function MaxSize($W, $H)
- {
- if($this->W() > $W || $this->H() > $H)
- return 0;
- return 1;
- }
- public function ResizeWPreserve($Wn)
- {
- $Wo = $this->W();
- $Ho = $this->H();
- if($Wn == $Wo)
- return $this;
- $Hn = round($Wn * $Ho / $Wo);
- $N = imagecreatetruecolor($Wn, $Hn);
- imagecopyresampled($N, $this->I, 0, 0, 0, 0, $Wn, $Hn, $Wo, $Ho);
- return new ImageProcessor($N);
- }
- public function ResizeHPreserve($Hn)
- {
- $Wo = $this->W();
- $Ho = $this->H();
- if($Hn == $Ho)
- return $this;
- $Wn = round($Hn * $Wo / $Ho);
- $N = imagecreatetruecolor($Wn, $Hn);
- imagecopyresampled($N, $this->I, 0, 0, 0, 0, $Wn, $Hn, $Wo, $Ho);
- return new ImageProcessor($N);
- }
- public function ResizeCanvas($Wn, $Hn)
- {
- $Wo = $this->W();
- $Ho = $this->H();
- //if($Wo < $Wn || $Ho < $Hn)
- // return 0; //Za małe
- $N = imagecreatetruecolor($Wn, $Hn);
- $BG = imagecolorallocate($N, 0, 0, 0);
- imagefill($N, 0, 0, $BG);
- if(($Wo == $Wn && $Ho == $Hn) || $Wo / $Ho == $Wn / $Hn)
- {
- imagecopyresampled($N, $this->I, 0, 0, 0, 0, $Wn, $Hn, $Wo, $Ho);
- }
- if(($Wo / $Ho) < ($Wn / $Hn))
- {
- $R = $Wo / $Ho;
- $Ww = $R * $Hn; //Szerokosc wycinka
- $Wd = round(($Wn - $Ww) / 2);
- imagecopyresampled($N, $this->I, $Wd, 0, 0, 0, $Ww, $Hn, $Wo, $Ho);
- }
- else
- {
- //Paski gora i dol
- $R = $Wo / $Ho;
- $Hw = round($Wn / $R); // Wysokosc wycinka
- $Hd = round(($Hn - $Hw) / 2);
- imagecopyresampled($N, $this->I, 0, $Hd, 0, 0, $Wn, $Hw, $Wo, $Ho);
- }
- return new ImageProcessor($N);
- }
- public function Cut($W, $H, $X, $Y)
- {
- $N = imagecreatetruecolor($W, $H);
- imagecopy($N, $this->I, 0, 0, $X, $Y, $this->W(), $this->H());
- return new ImageProcessor($N);
- }
- public function Fetch()
- {
- $F = $this->Save();
- $D = file_get_contents($F);
- unlink($F);
- return $D;
- }
- public function Save($OutFile = '')
- {
- if(!$OutFile)
- $OutFile = tempnam(sys_get_temp_dir(), 'TmpImg_');
- imagepng($this->I, $OutFile);
- return $OutFile;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement