Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author [email protected]
- * @license (c) 2015
- */
- class Image
- {
- private $Img = null;
- private $File = "";
- private $Width = 0;
- private $Height = 0;
- /**
- * @param string $Source
- */
- public function __construct($Source)
- {
- $FileExt = file_ext($Source);
- switch($FileExt) {
- case "png": {
- $this->Img = $this->fromPNG($Source);
- } break;
- case "jpeg":
- case "jpg": {
- $this->Img = $this->fromJPEG($Source);
- } break;
- }
- $this->File = $Source;
- list($this->Width, $this->Height) = getimagesize($this->File);
- }
- /**
- * @param string $Source
- * @return resource
- */
- private function fromPNG($Source)
- {
- $Img = imagecreatefrompng($Source);
- imagesavealpha($Img, true);
- imagealphablending($Img, true);
- return $Img;
- }
- /**
- * @param string $Source
- * @return resource
- */
- private function fromJPEG($Source)
- {
- $Img = imagecreatefromjpeg($Source);
- imagesavealpha($Img, true);
- imagealphablending($Img, true);
- return $Img;
- }
- /**
- * @param int $Width
- * @param int $Height
- * @return resource
- */
- public static function createTransparent($Width, $Height)
- {
- $Img = imagecreatetruecolor($Width, $Height);
- imagesavealpha($Img, true);
- imagealphablending($Img, true);
- $Transparent = imagecolorallocatealpha($Img, 0, 0, 0, 127);
- imagefill($Img, 0, 0, $Transparent);
- return $Img;
- }
- public function Square()
- {
- $NewWidth = $this->Width;
- $NewHeight = $this->Height;
- $OffsetX = 0;
- $OffsetY = 0;
- if($this->Width > $this->Height) {
- $NewWidth = $this->Height;
- $OffsetX = $this->Width / 2 - $NewWidth / 2;
- }
- else if($this->Height > $this->Width) {
- $NewHeight = $this->Width;
- $OffsetY = $this->Height / 2 - $NewHeight / 2;
- }
- $Img = $this->createTransparent($NewWidth, $NewHeight);
- imagecopy($Img, $this->Img, 0, 0, $OffsetX, $OffsetY, $NewWidth, $NewHeight);
- $this->Img = $Img;
- $this->Width = $NewWidth;
- $this->Height = $NewHeight;
- return $this;
- }
- /**
- * @param int $MaxWidth
- * @param int $MaxHeight
- * @return Image
- */
- public function Resize($MaxWidth, $MaxHeight)
- {
- $Width = $this->Width;
- $Height = $this->Height;
- if ($Width > $MaxHeight) {
- $Width = ($MaxHeight / $Height) * $Width;
- $Height = $MaxHeight;
- }
- if ($Width > $MaxWidth) {
- $Height = ($MaxWidth / $Width) * $Height;
- $Width = $MaxWidth;
- }
- $image_p = imagecreatetruecolor($Width, $Height);
- imagecopyresampled($image_p, $this->Img, 0, 0, 0, 0, $Width, $Height, $this->Width, $this->Height);
- $this->Img = $image_p;
- return $this;
- }
- public function Display()
- {
- header("Content-type: image/png");
- imagepng($this->Img);
- return $this;
- }
- public function Save($Destination)
- {
- imagepng($this->Img, $Destination);
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment