You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

110 lines
4.0 KiB

<?php
namespace App\Image;
use Jcupitt\Vips\Image;
use Jcupitt\Vips\Interpretation;
class ImageProcessor
{
public function brightness($image, float $brightness = 1.0, float $saturation = 1.0, float $hue = 0.0)
{
$oldInterpretation = $image->interpretation;
$hue %= 360;
if ($hue < 0) {
$hue = 360 + $hue;
}
if ($image->hasAlpha()) {
$imageWithoutAlpha = $image->extract_band(0, ['n' => $image->bands - 1]);
$alpha = $image->extract_band($image->bands - 1, ['n' => 1]);
return $imageWithoutAlpha
->colourspace(Interpretation::LCH)
->linear([$brightness, $saturation, 1.0], [0.0, 0.0, $hue])
->colourspace($oldInterpretation)
->bandjoin($alpha);
}
return $image
->colourspace(Interpretation::LCH)
->linear([$brightness, $saturation, 1.0], [0.0, 0.0, $hue])
->colourspace($oldInterpretation);
}
public function process(string $filename, string $target, array $options = ["w" => null, "h" => null, "r" => null,"flip" => null , "canv" => null,"rotation" => null],$saveOptions = ['Q' => 100])
{
if (!isset($options['w']) && !isset($options['h'])) {
$image = Image::thumbnail($filename, getimagesize($filename)[0]);
}
if (isset($options['r'])) {
$rArray = explode(':', $options['r']);
if (isset($options['w']) && !isset($options['h'])) {
$options['h'] = $options['w'] * $rArray[1];
$options['w'] = $options['w'] * $rArray[0];
}
if (!isset($options['w']) && isset($options['h'])) {
$options['h'] = $options['h'] * $rArray[1];
$options['w'] = $options['h'] * $rArray[0];
}
if (!isset($options['w']) && !isset($options['h'])) {
$options['h'] = getimagesize($filename)[0] * $rArray[1];
$options['w'] = getimagesize($filename)[0] * $rArray[0];
}
}
if (isset($options['w']) && !isset($options['h'])) {
$image = Image::thumbnail($filename, $options['w'], ['height' => getimagesize($filename)[1]]);
}
if (!isset($options['w']) && isset($options['h'])) {
$image = Image::thumbnail($filename, getimagesize($filename)[0], ['height' => $options['h']]);
}
if (isset($options['w']) && isset($options['h']) && !($options['canv'] == true)) {
$image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
}
if (isset($options['w']) && isset($options['h']) && $options['canv'] == true) {
$image = Image::thumbnail($filename, $options['w'], ['height' => $options['h']]);
$widthH = ($options['h'] - $image->height) / 2;
$widthW = ($options['w'] - $image->width) / 2;
$image = $image->embed(
$widthW,
$widthH,
$options['w'],
$options['h'],
['extend' => 'background', 'background' => 1024]
);
}
if (isset($options['brightness']) || isset($options['saturation']) || isset($options['hue'])) {
$image = $this->brightness($image, isset($options['brightness']) ? $options['brightness'] : 1.0, isset($options['saturation']) ? $options['saturation'] : 1.0, isset($options['hue']) ? $options['hue'] : 0.0);
}
if (isset($options['rotation'])) {
$image = $image->rotate($options['rotation']);
}
if (isset($options['flip'])) {
if ($options['flip'] == "h") {
$image = $image->fliphor();
}
if ($options['flip'] == "v") {
$image = $image->flipver();
}
if ($options['flip'] == "hv") {
$image = $image->fliphor();
$image = $image->flipver();
}
}
$image->writeToFile($target,$saveOptions);
return $target;
}
}