Guest User

Untitled

a guest
Aug 21st, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2.  
  3. class SimpleImage {
  4.  
  5.    function __construct($filename) {
  6.       $this->image       = new Imagick($filename);
  7.       $this->filename    = $filename;
  8.       $this->compression = 85;
  9.    }
  10.  
  11.    function setCompression($compression) {
  12.       if(!is_numeric($compression) || $compression < 0 || $compression > 100) {
  13.          throw new Exception("Invalid compression value!");
  14.       }
  15.  
  16.       $this->compression = $compression;
  17.    }
  18.  
  19.    function save($newFileName) {
  20.       $this->image->setCompression($this->compression);
  21.  
  22.       if(is_string($newFileName)) {
  23.          $this->image->writeImage($newFileName);
  24.       }
  25.       else {
  26.          $this->image->writeImage($this->filename);
  27.       }
  28.      
  29.    }
  30.  
  31.    function output() {
  32.       echo file_get_contents($this->filename);
  33.    }
  34.  
  35.    function getWidth() {
  36.       return $this->image->getImageWidth();
  37.    }
  38.  
  39.    function getHeight() {
  40.       return $this->image->getImageHeight();
  41.    }
  42.  
  43.    function resizeToHeight($height) {
  44.       $ratio = $height / $this->getHeight();
  45.       $width = $this->getWidth() * $ratio;
  46.       $this->resize($width, $height);
  47.    }
  48.  
  49.    function resizeToWidth($width) {
  50.       $ratio = $width / $this->getWidth();
  51.       $height = $this->getheight() * $ratio;
  52.       $this->resize($width, $height);
  53.    }
  54.  
  55.    function scale($scale) {
  56.       $width = $this->getWidth() * $scale / 100;
  57.       $height = $this->getheight() * $scale / 100;
  58.       $this->resize($width,$height);
  59.    }
  60.  
  61.    function resize($width, $height) {
  62.       return $this->image->scaleImage($width, $height);
  63.    }  
  64.  
  65.    function rotate($angle = 90) {
  66.       $this->image->rotateImage(new ImagickPixel(), 90);
  67.    }
  68.  
  69.    function crop($width, $height, $x, $y) {
  70.       $this->image->cropImage($width, $height, $x, $y);
  71.  
  72.    }
  73. }
  74.  
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment