нарисовал класс с тем же функционалом, плюс стандартные (300+) меттоды доступны
сам класс (еще буду допиливать) (+/-)
<?php
class Kimagick {
private $image, $width, $height = false;
const UR = 1;
const UL = 2;
const DL = 3;
const DR = 4;
public function check() {
if (!extension_loaded('imagick')) {
throw new ErrorException('Imagick is not installed');
}
}
public function __construct($file) {
$this->check();
$this->tmp($file);
}
public function save($filename, $quality = 100) {
$this->image->setImageCompressionQuality($quality);
$this->image->writeImage($filename);
}
public function tmp($image) {
$this->image = new Imagick;
if (file_exists($image)) {
$image = file_get_contents($image);
} elseif (preg_match('#^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$#', $file)) {
$image = base64_decode($image);
} elseif (substr($image, 0, 4) == 'http') {
if (list(, , $type) = getimagesize($image)) {
if ($type) {
$upload = new SplFileObject($image, 'rb');
$image = '';
while (!$upload->eof()) {
$image .= $upload->fgets();
}
} else {
throw new Exception('Unsupported image type');
return false;
}
}
}
if ($this->image->readImageBlob($image)) {
if ($this->image->getImageAlphaChannel() !== Imagick::ALPHACHANNEL_ACTIVATE) {
$this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET); // 8
#$this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE); // 6
}
}
$background = new Imagick;
$background->newImage($this->width, $this->height, new ImagickPixel('transparent'));
$background->setImageBackgroundColor(new ImagickPixel('transparent'));
$this->image->compositeImage($background, imagick::COMPOSITE_OVER, 0, 0); //Imagick::COMPOSITE_DISSOLVE
$this->image->setFormat('png');
$this->setSizes();
}
public function watermark($file, $corner = self::UL, $opacity = 0.5) {
$watermark = new Imagick(realpath($file));
$watermark->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity, Imagick::CHANNEL_ALPHA);
$x = $this->widht();
$y = $this->height();
#$watermark->scaleImage($x / 8, 0);
$watermark->scaleImage(0, $y / 6);
$wx = $watermark->getImageWidth();
$wy = $watermark->getImageHeight();
$offsetw = 10;
$offseth = 10;
$resx = $offsetw;
$resy = $offseth;
switch($corner) {
case self::UL: // 2 верхний левый
default:
break;
case self::UR: // 1 верхний правый
$resx = $x - $wx - $offsetw;
break;
case self:L: // 3 нижний левый
$resy = $y - $wy - $offseth;
break;
case self:R: // 4 нижний правый
$resx = $x - $wx - $offsetw;
$resy = $y - $wy - $offseth;
break;
}
$this->image->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $resx, $resy);
return $this;
}
/*
public function copy_watermark(self $watermark, $offset_x, $offset_y, $opacity = 0.5) {
$watermark->image()->evaluateImage(Imagick::EVALUATE_MULTIPLY, $opacity, Imagick::CHANNEL_ALPHA);
return $this->image->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y);
}
*/
/**
* Изменение размера в процентном соотношении
*
* @param integer $scale
* @return Kimagick
*/
public function scale($scale) {
$width = $this->widht() * ($scale / 100);
$height = $this->height() * ($scale / 100);
$this->resize($width, $height);
}
public function resize($width, $height) {
$this->image->scaleImage($width, $height, 1);
$this->setSizes();
return $this;
}
public function crop($width, $height, $x, $y) {
$this->image->cropImage($width, $height, $x, $y);
$this->setSizes();
return $this;
}
public function reHeight($height) {
$this->resize($this->widht() * ($height / $this->height()), $height);
}
public function reWidth($width) {
$this->resize($width, $this->height() * ($width / $this->widht()));
}
public function rotate($angle = 90) {
$this->image->rotateImage(new ImagickPixel('#00000000'), $angle);
#$this->image->rotateImage(new ImagickPixel('transparent'), $angle);
$this->setSizes();
return $this;
}
public function roundCorners() {
$this->image->roundCorners(20, 20);
$this->setSizes();
return $this;
}
/**
* Создает вертикальное зеркало изображения
*
*/
public function flip() {
$this->image->flipImage();
return $this;
}
/**
* Создает горизонтальное зеркало изображения
*
*/
public function flop() {
$this->image->flopImage();
return $this;
}
public function widht() {
return $this->width;
}
public function height() {
return $this->height;
}
public function setSizes() {
$args = $this->image->getImageGeometry();
$this->width = $args['width'];
$this->height = $args['height'];
return $this;
}
public function image() {
return $this->image;
}
public function base64() {
return 'data:image/png;base64,' . base64_encode($this->image->getImageBlob());
}
// echo '<img src="' . $this->base64 . '" alt="base64_image" style="background-color: rgb(90, 187, 200);" />';
}
тут тесты
код (+/-)
$obj = new Kimagick('fazliddin.jpg');
#$obj->crop(300, 300, 100, 100);
#$obj->rotate(90);
#$obj->resize(800, 400);
$obj->watermark('wat2.png', 4, 1);
$obj->roundCorners();
#$obj->rotate(45);
$obj->scale(150);
#$obj->scale(50);
echo '<img src="' . $obj->base64() . '" alt="base64_image" style="background-color: rgb(150, 50, 50);" />'; // задний фон для теста прозрачности, красненький)
$obj->save(dirname(__FILE__) . '/test.png');
exit;