deblan.tv/vendor/trinity/src/Trinity/Component/Utils/Image.php

175 lines
4.3 KiB
PHP

<?php
namespace Trinity\Component\Utils;
class Image
{
protected $tool;
protected $image;
public function __construct()
{
$this->tool = new \Imagick();
}
public function getTool()
{
return $this->tool;
}
public function setTool(\Imagick $tool)
{
$this->tool = $tool;
}
public function setCompressionQuality($compression_quality)
{
$this->getTool()->setCompressionQuality($compression_quality);
return $this;
}
public function setImage($image)
{
$this->image = $image;
return $this;
}
public function setImageColorSpace($colorSpace)
{
$this->getTool()->setimagecolorspace($colorSpace);
return $this;
}
public function contrastImage($times, $type)
{
for ($i = 1; $i <= $times; $i++) {
$this->getTool()->contrastimage($type);
}
return $this;
}
public function modulateImage($brightness, $saturation, $hue)
{
$this->getTool()->modulateimage($brightness, $saturation, $hue);
return $this;
}
public static function create($image = null)
{
$self = new self();
$self->setImage($image);
$class = get_class($self->getTool());
$self->getTool()->addImage(new $class($image));
return $self;
}
public function crop($width, $height, $x, $y)
{
$this->getTool()->cropImage($width, $height, $x, $y);
return $this;
}
public function thumb($max_width, $max_height, $filter = \Imagick::FILTER_LANCZOS, $blur = 1)
{
$sizes = $this->getTool()->getImageGeometry();
list($width, $height) = array($sizes['width'], $sizes['height']);
if ($width > $max_width) {
$new_width = $max_width;
$new_height = $height * $new_width / $width;
} elseif ($height > $max_height) {
$new_height = $max_height;
$new_width = $width * $new_height / $height;
} else {
return $this;
}
$this->getTool()->resizeImage($new_width, $new_height, $filter, $blur, true);
return $this;
}
public function resizeRatio($max_width, $max_height, $filter = \Imagick::FILTER_LANCZOS, $blur = 1)
{
$sizes = $this->getTool()->getImageGeometry();
list($width, $height) = array($sizes['width'], $sizes['height']);
$ratioImage = $width/$height;
$ratio = $max_width/$max_height;
if ($ratioImage < $ratio) {
$new_width = $max_width;
$new_height = $height * $new_width / $width;
} elseif ($ratioImage > $ratio) {
$new_height = $max_height;
$new_width = $width * $new_height / $height;
} else {
$new_width = $max_width;
$new_height = $max_height;
}
$this->getTool()->resizeImage($new_width, $new_height, $filter, $blur, true);
return $this;
}
public function resizeImage($max_width, $max_height, $filter = \Imagick::FILTER_LANCZOS, $blur = 1)
{
$this->getTool()->resizeImage($max_width, $max_height, $filter, $blur, true);
return $this;
}
public function autoRotate()
{
$orientation = $this->getTool()->getImageOrientation();
switch ($orientation) {
case \Imagick::ORIENTATION_BOTTOMRIGHT:
$this->getTool()->rotateimage("#000", 180); // rotate 180 degrees
break;
case \Imagick::ORIENTATION_RIGHTTOP:
$this->getTool()->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case \Imagick::ORIENTATION_LEFTBOTTOM:
$this->getTool()->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
$this->getTool()->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);
return $this;
}
public function save($output = null)
{
if (null === $output) {
$output = $this->image;
}
//$this->getTool()->setCompressionQuality($this->getCompressionQuality());
/**
* Should be $this->getTool()->writetImage($output);
* but it does not work (Undefined Method)
* Bug #61879
*/
$handle = fopen($output, 'w');
fwrite($handle, $this->getTool()->getImageBlob());
}
}