mirror of
https://github.com/flextype/flextype.git
synced 2025-08-10 15:14:20 +02:00
feat(media): use image
helper function #564
This commit is contained in:
@@ -100,7 +100,7 @@ class Media extends Entries
|
||||
filesystem()->file($uploadFolder . '/' . $result->name)->move($mediaFile);
|
||||
|
||||
if (getimagesize($mediaFile)) {
|
||||
$this->processImage($mediaFile, $settings);
|
||||
image($mediaFile, $settings['process']['image']);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$result->clear();
|
||||
@@ -112,136 +112,4 @@ class Media extends Entries
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process image
|
||||
*
|
||||
* @param string $mediaFile Media file.
|
||||
* @param array $settings Media file settings.
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
private function processImage(string $mediaFile, array $settings): void
|
||||
{
|
||||
$image = Image::make($mediaFile);
|
||||
|
||||
if (isset($settings['process']['image']['blur'])) {
|
||||
$image->blur($settings['process']['image']['blur']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['brightness'])) {
|
||||
$image->brightness($settings['process']['image']['brightness']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['colorize']) &&
|
||||
isset($settings['process']['image']['colorize']['red']) &&
|
||||
isset($settings['process']['image']['colorize']['green']) &&
|
||||
isset($settings['process']['image']['colorize']['blue'])) {
|
||||
$image->colorize($settings['process']['image']['colorize']['red'],
|
||||
$settings['image']['process']['colorize']['green'],
|
||||
$settings['image']['process']['colorize']['blue']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['contrast'])) {
|
||||
$image->contrast($settings['process']['image']['contrast']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['flip'])) {
|
||||
$image->flip($settings['process']['image']['flip']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['gamma'])) {
|
||||
$image->gamma($settings['process']['image']['gamma']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['rotate'])) {
|
||||
$image->rotate($settings['process']['image']['rotate']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['pixelate'])) {
|
||||
$image->pixelate($settings['process']['image']['pixelate']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['heighten'])) {
|
||||
$image->heighten($settings['process']['image']['heighten']['height'],
|
||||
function ($constraint) use ($settings) {
|
||||
if (isset($settings['process']['image']['heighten']['constraint']) &&
|
||||
is_array($settings['process']['image']['heighten']['constraint'])) {
|
||||
foreach ($settings['process']['image']['heighten']['constraint'] as $method) {
|
||||
if (in_array($method, ['upsize'])) {
|
||||
$constraint->{$method}();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['widen'])) {
|
||||
$image->heighten($settings['process']['image']['widen']['width'],
|
||||
function ($constraint) use ($settings) {
|
||||
if (isset($settings['process']['image']['widen']['constraint']) &&
|
||||
is_array($settings['process']['image']['widen']['constraint'])) {
|
||||
foreach ($settings['process']['image']['widen']['constraint'] as $method) {
|
||||
if (in_array($method, ['upsize'])) {
|
||||
$constraint->{$method}();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['fit']) &&
|
||||
isset($settings['process']['image']['fit']['width'])) {
|
||||
$image->fit($settings['process']['image']['fit']['width'],
|
||||
$settings['process']['image']['fit']['height'] ?? null,
|
||||
function ($constraint) use ($settings) {
|
||||
if (isset($settings['process']['image']['fit']['constraint']) &&
|
||||
is_array($settings['process']['image']['fit']['constraint'])) {
|
||||
foreach ($settings['process']['image']['fit']['constraint'] as $method) {
|
||||
if (in_array($method, ['upsize'])) {
|
||||
$constraint->{$method}();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
$settings['process']['image']['fit']['position'] ?? 'center');
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['crop']) &&
|
||||
isset($settings['process']['image']['crop']['width']) &&
|
||||
isset($settings['process']['image']['crop']['height'])) {
|
||||
$image->crop($settings['process']['image']['crop']['width'],
|
||||
$settings['process']['image']['crop']['height'],
|
||||
$settings['process']['image']['crop']['x'] ?? null,
|
||||
$settings['process']['image']['crop']['y'] ?? null);
|
||||
}
|
||||
|
||||
|
||||
if (isset($settings['process']['image']['invert']) &&
|
||||
$settings['process']['image']['invert'] == true) {
|
||||
$image->invert();
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['sharpen'])) {
|
||||
$image->sharpen($settings['process']['image']['sharpen']);
|
||||
}
|
||||
|
||||
if (isset($settings['process']['image']['resize'])) {
|
||||
$image->resize($settings['process']['image']['resize']['width'] ?? null,
|
||||
$settings['process']['image']['resize']['height'] ?? null,
|
||||
function ($constraint) use ($settings) {
|
||||
if (isset($settings['process']['image']['resize']['constraint']) &&
|
||||
is_array($settings['process']['image']['resize']['constraint'])) {
|
||||
foreach ($settings['process']['image']['resize']['constraint'] as $method) {
|
||||
if (in_array($method, ['aspectRatio', 'upsize'])) {
|
||||
$constraint->{$method}();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$image->save($mediaFile, $settings['process']['image']['quality'] ?? 70);
|
||||
$image->destroy();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user