Guest User

PHP Image Class

a guest
Feb 26th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author      [email protected]
  5.  * @license     (c) 2015
  6.  */
  7.  
  8. class Image
  9. {
  10.  
  11.     private $Img = null;
  12.     private $File = "";
  13.     private $Width = 0;
  14.     private $Height = 0;
  15.  
  16.     /**
  17.      * @param string $Source
  18.      */
  19.     public function __construct($Source)
  20.     {
  21.         $FileExt = file_ext($Source);
  22.  
  23.         switch($FileExt) {
  24.             case "png": {
  25.                 $this->Img = $this->fromPNG($Source);
  26.             } break;
  27.             case "jpeg":
  28.             case "jpg": {
  29.                 $this->Img = $this->fromJPEG($Source);
  30.             } break;
  31.         }
  32.  
  33.         $this->File = $Source;
  34.         list($this->Width, $this->Height) = getimagesize($this->File);
  35.     }
  36.  
  37.     /**
  38.      * @param string $Source
  39.      * @return resource
  40.      */
  41.     private function fromPNG($Source)
  42.     {
  43.         $Img = imagecreatefrompng($Source);
  44.         imagesavealpha($Img, true);
  45.         imagealphablending($Img, true);
  46.  
  47.         return $Img;
  48.     }
  49.  
  50.     /**
  51.      * @param string $Source
  52.      * @return resource
  53.      */
  54.     private function fromJPEG($Source)
  55.     {
  56.         $Img = imagecreatefromjpeg($Source);
  57.         imagesavealpha($Img, true);
  58.         imagealphablending($Img, true);
  59.  
  60.         return $Img;
  61.     }
  62.  
  63.     /**
  64.      * @param int $Width
  65.      * @param int $Height
  66.      * @return resource
  67.      */
  68.     public static function createTransparent($Width, $Height)
  69.     {
  70.         $Img = imagecreatetruecolor($Width, $Height);
  71.         imagesavealpha($Img, true);
  72.         imagealphablending($Img, true);
  73.         $Transparent = imagecolorallocatealpha($Img, 0, 0, 0, 127);
  74.         imagefill($Img, 0, 0, $Transparent);
  75.  
  76.         return $Img;
  77.     }
  78.  
  79.     public function Square()
  80.     {
  81.         $NewWidth = $this->Width;
  82.         $NewHeight = $this->Height;
  83.         $OffsetX = 0;
  84.         $OffsetY = 0;
  85.  
  86.         if($this->Width > $this->Height) {
  87.             $NewWidth = $this->Height;
  88.             $OffsetX = $this->Width / 2 - $NewWidth / 2;
  89.         }
  90.         else if($this->Height > $this->Width) {
  91.             $NewHeight = $this->Width;
  92.             $OffsetY = $this->Height / 2 - $NewHeight / 2;
  93.         }
  94.  
  95.         $Img = $this->createTransparent($NewWidth, $NewHeight);
  96.  
  97.         imagecopy($Img, $this->Img, 0, 0, $OffsetX, $OffsetY, $NewWidth, $NewHeight);
  98.  
  99.         $this->Img = $Img;
  100.         $this->Width = $NewWidth;
  101.         $this->Height = $NewHeight;
  102.  
  103.         return $this;
  104.     }
  105.  
  106.     /**
  107.      * @param int $MaxWidth
  108.      * @param int $MaxHeight
  109.      * @return Image
  110.      */
  111.     public function Resize($MaxWidth, $MaxHeight)
  112.     {
  113.         $Width = $this->Width;
  114.         $Height = $this->Height;
  115.  
  116.         if ($Width > $MaxHeight) {
  117.             $Width = ($MaxHeight / $Height) * $Width;
  118.             $Height = $MaxHeight;
  119.         }
  120.  
  121.         if ($Width > $MaxWidth) {
  122.             $Height = ($MaxWidth / $Width) * $Height;
  123.             $Width = $MaxWidth;
  124.         }
  125.  
  126.         $image_p = imagecreatetruecolor($Width, $Height);
  127.  
  128.         imagecopyresampled($image_p, $this->Img, 0, 0, 0, 0, $Width, $Height, $this->Width, $this->Height);
  129.  
  130.         $this->Img = $image_p;
  131.  
  132.         return $this;
  133.     }
  134.  
  135.     public function Display()
  136.     {
  137.         header("Content-type: image/png");
  138.         imagepng($this->Img);
  139.  
  140.         return $this;
  141.     }
  142.  
  143.     public function Save($Destination)
  144.     {
  145.         imagepng($this->Img, $Destination);
  146.  
  147.         return $this;
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment