Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SimpleImagick {
- public $image;
- public function __construct($filename)
- {
- $this->load($filename);
- }
- public function load($filename)
- {
- if (!class_exists('Imagick')) throw new Exception('Imagick not found');
- $this->image = new Imagick();
- $ping_result = $this->image->pingImage($filename);
- $this->image->readImage($filename);
- }
- public function save(
- $filename,
- $image_type = IMAGETYPE_JPEG,
- $compression = 75,
- $permissions = null
- ) {
- $this->image->setCompressionQuality($compression);
- $this->image->setFormat($image_type);
- $this->image->writeImage($filename);
- if ($permissions != null) {
- chmod($filename, $permissions);
- }
- }
- public function output($image_type = IMAGETYPE_JPEG, $compression = 90)
- {
- $this->image->setCompressionQuality($compression);
- $format = $this->image->setFormat($image_type);
- header('Content-type: image/'.$format);
- echo $this->image;
- }
- public function getWidth()
- {
- return $this->image->getImageWidth();
- }
- public function getHeight()
- {
- return $this->image->getImageHeight();
- }
- public function resizeToHeight($height)
- {
- $this->image->thumbnailImage(null, $height);
- }
- public function resizeToWidth($width)
- {
- $this->image->thumbnailImage($width, null);
- }
- public function scale($scale)
- {
- $width = $this->getWidth() * $scale / 100;
- $height = $this->getheight() * $scale / 100;
- $this->image->scaleImage(round($width), round($height));
- }
- public function resize($width, $height)
- {
- $this->image->scaleImage($width, $height);
- }
- public function crop($width, $height)
- {
- $src_x = round(($this->getWidth() - $width) / 2);
- $src_y = round(($this->getHeight() - $height) / 2);
- $this->image->cropImage($width, $height, $src_x, $src_y);
- }
- private function setFormat($image_type)
- {
- switch ($image_type) {
- case IMAGETYPE_JPEG: $format = 'jpeg'; break;
- case IMAGETYPE_PNG: $format = 'png'; break;
- case IMAGETYPE_GIF: $format = 'gif'; break;
- default: $format = 'jpeg';
- }
- $this->image->setImageFormat($format);
- return $format;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement