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.
 
 
 
 

266 lines
10 KiB

<?php
namespace App\Image;
use App\Http\Controllers\Traits\FileTrait;
use App\Image\Traits\ModulateTrait;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule as ValidationRule;
use Jcupitt\Vips\Image;
use Jcupitt\Vips\Interpretation;
class ImageProcessor
{
use ModulateTrait, FileTrait;
protected $defaultW = 2000;
protected $defaultH = 2000;
public function processToBuffer(string $filename, array $options = [], $saveOptions = ['Q' => 100])
{
if (array_key_exists('w', $options) && array_key_exists('h', $options) && array_key_exists('r', $options)) {
unset($options['r']);
}
if (array_key_exists('r', $options)) {
$options['r'] = explode(':', $options['r']);
if (count($options['r']) != 2) {
unset($options['r']);
}
$validator = Validator::make($options, [
'r' => ['nullable', 'array'],
'r.*' => ['nullable', 'numeric'],
]);
if ($validator->fails()) {
foreach ($validator->messages()->getMessages() as $field_name => $messages) {
unset($options[explode('.', $field_name)[0]]);
}
}
}
if (array_key_exists('r', $options)) {
if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$options['w'] = $options['w'] * $options['r'][0];
$options['h'] = $options['w'] * $options['r'][1];
}
if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
$options['w'] = $options['h'] * $options['r'][0];
$options['h'] = $options['h'] * $options['r'][1];
}
if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$options['w'] = getimagesize($filename)[0] * $options['r'][0];
$options['h'] = getimagesize($filename)[0] * $options['r'][1];
}
unset($options['r']);
}
$validator = Validator::make($options, [
'w' => ['numeric', 'between:1,2000'],
'h' => ['numeric', 'between:1,2000'],
'canv' => ['boolean'],
'brightness' => ['numeric', 'between:0,100'],
'saturation' => ['numeric', 'between:0,100'],
'hue' => ['numeric', 'between:0,100'],
'rotation' => ['numeric'],
'flip' => ['string', ValidationRule::in(['h', 'v', 'hv'])]
]);
if ($validator->fails()) {
foreach ($validator->messages()->getMessages() as $field_name => $messages) {
unset($options[$field_name]);
}
}
if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$image = Image::newFromFile($filename);
}
if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$image = Image::thumbnail($filename, $options['w'], ['height' => $this->defaultH]);
}
if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
$image = Image::thumbnail($filename, $this->defaultW, ['height' => $options['h']]);
}
if (array_key_exists('w', $options) && array_key_exists('h', $options)) {
if (array_key_exists('canv', $options) && ($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]
);
} else {
$image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
}
}
if (array_key_exists('brightness', $options) || array_key_exists('saturation', $options) || array_key_exists('hue', $options)) {
$image = $this->brightness($image, array_key_exists('brightness', $options) ? $options['brightness'] : 1.0, array_key_exists('saturation', $options) ? $options['saturation'] : 1.0, array_key_exists('hue', $options) ? $options['hue'] : 0.0);
}
if (array_key_exists('rotation', $options)) {
$image = $image->rotate($options['rotation']);
}
if (array_key_exists('flip', $options)) {
if ($options['flip'] == "h") {
$image = $image->fliphor();
}
if ($options['flip'] == "v") {
$image = $image->flipver();
}
if ($options['flip'] == "hv") {
$image = $image->fliphor();
$image = $image->flipver();
}
}
if (array_key_exists('q', $options)) {
$saveOptions['Q'] = $options['q'];
}
return $image->writeToBuffer("." . app()->file->ext, $saveOptions);
}
public function process(string $filename, string $target, array $options = [], $saveOptions = ['Q' => 100])
{
if (array_key_exists('w', $options) && array_key_exists('h', $options) && array_key_exists('r', $options)) {
unset($options['r']);
}
if (array_key_exists('r', $options)) {
$options['r'] = explode(':', $options['r']);
if (count($options['r']) != 2) {
unset($options['r']);
}
$validator = Validator::make($options, [
'r' => ['nullable', 'array'],
'r.*' => ['nullable', 'numeric'],
]);
if ($validator->fails()) {
foreach ($validator->messages()->getMessages() as $field_name => $messages) {
unset($options[explode('.', $field_name)[0]]);
}
}
}
if (array_key_exists('r', $options)) {
if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$options['w'] = $options['w'] * $options['r'][0];
$options['h'] = $options['w'] * $options['r'][1];
}
if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
$options['w'] = $options['h'] * $options['r'][0];
$options['h'] = $options['h'] * $options['r'][1];
}
if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$options['w'] = getimagesize($filename)[0] * $options['r'][0];
$options['h'] = getimagesize($filename)[0] * $options['r'][1];
}
unset($options['r']);
}
$validator = Validator::make($options, [
'w' => ['numeric', 'between:1,2000'],
'h' => ['numeric', 'between:1,2000'],
'canv' => ['boolean'],
'brightness' => ['numeric', 'between:0,100'],
'saturation' => ['numeric', 'between:0,100'],
'hue' => ['numeric', 'between:0,100'],
'rotation' => ['numeric'],
'flip' => ['string', ValidationRule::in(['h', 'v', 'hv'])]
]);
if ($validator->fails()) {
foreach ($validator->messages()->getMessages() as $field_name => $messages) {
unset($options[$field_name]);
}
}
if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$image = Image::newFromFile($filename);
}
if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
$image = Image::thumbnail($filename, $options['w'], ['height' => $this->defaultH]);
}
if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
$image = Image::thumbnail($filename, $this->defaultW, ['height' => $options['h']]);
}
if (array_key_exists('w', $options) && array_key_exists('h', $options)) {
if (array_key_exists('canv', $options) && ($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]
);
} else {
$image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
}
}
if (array_key_exists('brightness', $options) || array_key_exists('saturation', $options) || array_key_exists('hue', $options)) {
$image = $this->brightness($image, array_key_exists('brightness', $options) ? $options['brightness'] : 1.0, array_key_exists('saturation', $options) ? $options['saturation'] : 1.0, array_key_exists('hue', $options) ? $options['hue'] : 0.0);
}
if (array_key_exists('rotation', $options)) {
$image = $image->rotate($options['rotation']);
}
if (array_key_exists('flip', $options)) {
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 new \Illuminate\Http\File($target);
}
public function createFakeImage($stub, $path, $saveOptions = ['Q' => 100], $options = ["w" => null, "h" => null, "r" => null, "flip" => null, "canv" => null, "rotation" => null])
{
return $this->process($stub, $path, $options, $saveOptions);
}
public function convertImage(string $filePath, string $target, array $options = ['Q' => 100]): \Illuminate\Http\File
{
$tmpFile = \Jcupitt\Vips\Image::newFromFile($filePath);
$tmpFile->writeToFile($target, $options);
return new \Illuminate\Http\File($target);
}
}