ozpavel

SimpleImage

Jun 28th, 2020
1,505
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SimpleImage {
  2.  
  3.    private $image;
  4.    private $image_type;
  5.  
  6.    public function load($filename)
  7.    {
  8.       $image_info = getimagesize($filename);
  9.       $this->image_type = $image_info[2];
  10.  
  11.       if ($this->image_type == IMAGETYPE_JPEG) {
  12.          $this->image = imagecreatefromjpeg($filename);
  13.       }
  14.       elseif ($this->image_type == IMAGETYPE_GIF) {
  15.          $this->image = imagecreatefromgif($filename);
  16.       }
  17.       elseif ($this->image_type == IMAGETYPE_PNG) {
  18.          $this->image = imagecreatefrompng($filename);
  19.       }
  20.    }
  21.  
  22.    public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null)
  23.    {
  24.       if ($image_type == IMAGETYPE_JPEG) {
  25.          imagejpeg($this->image,$filename,$compression);
  26.       }
  27.       elseif ($image_type == IMAGETYPE_GIF) {
  28.          imagegif($this->image,$filename);
  29.       }
  30.       elseif ($image_type == IMAGETYPE_PNG) {
  31.          imagepng($this->image,$filename);
  32.       }
  33.  
  34.       if( $permissions != null) {
  35.          chmod($filename,$permissions);
  36.       }
  37.    }
  38.  
  39.    public function output($image_type=IMAGETYPE_JPEG)
  40.    {
  41.       if ($image_type == IMAGETYPE_JPEG) {
  42.          imagejpeg($this->image);
  43.       }
  44.       elseif ($image_type == IMAGETYPE_GIF) {
  45.          imagegif($this->image);
  46.       }
  47.       elseif ($image_type == IMAGETYPE_PNG) {
  48.          imagepng($this->image);
  49.       }
  50.    }
  51.  
  52.    public function getWidth()
  53.    {
  54.       return imagesx($this->image);
  55.    }
  56.  
  57.    public function getHeight()
  58.    {
  59.       return imagesy($this->image);
  60.    }
  61.  
  62.    public function resizeToHeight($height)
  63.    {
  64.       $ratio = $height / $this->getHeight();
  65.       $width = $this->getWidth() * $ratio;
  66.       $this->resize($width,$height);
  67.    }
  68.  
  69.    public function resizeToWidth($width)
  70.    {
  71.       $ratio = $width / $this->getWidth();
  72.       $height = $this->getheight() * $ratio;
  73.       $this->resize($width,$height);
  74.    }
  75.  
  76.    public function scale($scale)
  77.    {
  78.       $width = $this->getWidth() * $scale/100;
  79.       $height = $this->getheight() * $scale/100;
  80.       $this->resize($width,$height);
  81.    }
  82.  
  83.    public function resize($width,$height)
  84.    {
  85.       $new_image = imagecreatetruecolor($width, $height);
  86.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  87.       $this->image = $new_image;
  88.    }
  89. }
RAW Paste Data