Advertisement
Guest User

SimpleImagick: simple PHP wrapper for ImageMagick

a guest
May 28th, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2. class SimpleImagick {
  3.     public $image;
  4.  
  5.     public function __construct($filename)
  6.     {
  7.         $this->load($filename);
  8.     }
  9.     public function load($filename)
  10.     {
  11.         if (!class_exists('Imagick')) throw new Exception('Imagick not found');
  12.         $this->image = new Imagick();
  13.         $ping_result = $this->image->pingImage($filename);
  14.         $this->image->readImage($filename);
  15.     }
  16.     public function save(
  17.         $filename,
  18.         $image_type = IMAGETYPE_JPEG,
  19.         $compression = 75,
  20.         $permissions = null
  21.     ) {
  22.         $this->image->setCompressionQuality($compression);
  23.         $this->image->setFormat($image_type);
  24.         $this->image->writeImage($filename);
  25.         if ($permissions != null) {
  26.             chmod($filename, $permissions);
  27.         }
  28.     }
  29.     public function output($image_type = IMAGETYPE_JPEG, $compression = 90)
  30.     {
  31.         $this->image->setCompressionQuality($compression);
  32.         $format = $this->image->setFormat($image_type);
  33.         header('Content-type: image/'.$format);
  34.         echo $this->image;
  35.     }
  36.     public function getWidth()
  37.     {
  38.         return $this->image->getImageWidth();
  39.     }
  40.     public function getHeight()
  41.     {
  42.         return $this->image->getImageHeight();
  43.     }
  44.     public function resizeToHeight($height)
  45.     {
  46.         $this->image->thumbnailImage(null, $height);
  47.     }
  48.     public function resizeToWidth($width)
  49.     {
  50.         $this->image->thumbnailImage($width, null);
  51.     }
  52.     public function scale($scale)
  53.     {
  54.         $width = $this->getWidth() * $scale / 100;
  55.         $height = $this->getheight() * $scale / 100;
  56.         $this->image->scaleImage(round($width), round($height));
  57.     }
  58.     public function resize($width, $height)
  59.     {
  60.         $this->image->scaleImage($width, $height);
  61.     }
  62.     public function crop($width, $height)
  63.     {
  64.         $src_x = round(($this->getWidth()  - $width)  / 2);
  65.         $src_y = round(($this->getHeight() - $height) / 2);
  66.         $this->image->cropImage($width, $height, $src_x, $src_y);
  67.     }
  68.     private function setFormat($image_type)
  69.     {
  70.         switch ($image_type) {
  71.             case IMAGETYPE_JPEG: $format = 'jpeg'; break;
  72.             case IMAGETYPE_PNG:  $format = 'png';  break;
  73.             case IMAGETYPE_GIF:  $format = 'gif';  break;
  74.             default:             $format = 'jpeg';
  75.         }
  76.         $this->image->setImageFormat($format);
  77.         return $format;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement