Skip to content

Commit

Permalink
Various tweaks and improvements, seed two filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Bradie Tilley committed Jun 17, 2019
1 parent 16db1e9 commit 3c90b89
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 29 deletions.
27 changes: 18 additions & 9 deletions classes/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function getDefaultImage()

// If the image still doesn't exist, use the provided Image Not Found image
if (!$image || !file_exists($image)) {
$image = base_path('plugins/abwebdevelopers/imageresize/assets/image-not-found.png');
$image = base_path(Settings::DEFAULT_IMAGE_NOT_FOUND);
}

// Use the default Image Not Found background, mode and quality
Expand Down Expand Up @@ -188,6 +188,7 @@ private function initOptions(iterable $options = null)
// Check to see if a filter is being used for this image
if (!empty($options['filter'])) {
// If options were passed then use them to override any filters used
$options = array_filter($options);
$this->override = array_merge($this->override, $options);

// Now find it
Expand Down Expand Up @@ -231,7 +232,7 @@ private function initOptions(iterable $options = null)
/**
* Get the physical path of the image
*
* @return void
* @return string
*/
public function getPath()
{
Expand All @@ -241,7 +242,7 @@ public function getPath()
/**
* Get the storage path - used for public access and for physical path generation
*
* @return void
* @return string
*/
private function storagePath()
{
Expand All @@ -257,6 +258,11 @@ private function storagePath()
*/
public function getCache()
{
// If explicitly told to not cache, don't cache it then
if (isset($this->options['cache']) && !$this->options['cache']) {
return false;
}

$path = $this->storagePath();

if (file_exists(storage_path($path))) {
Expand Down Expand Up @@ -310,6 +316,9 @@ public function resize(int $width = null, int $height = null, iterable $options
return $cached;
}

$width = $this->options['width'];
$height = $this->options['height'];

$hasMinMaxConstraint = array_key_exists('min_height', $this->options) ||
array_key_exists('max_height', $this->options) ||
array_key_exists('min_width', $this->options) ||
Expand All @@ -319,7 +328,7 @@ public function resize(int $width = null, int $height = null, iterable $options
$this->initResource();

// If width or height is set, resize the image to it
if ($width !== null || $height !== null || $hasMinMaxConstraint) {
if (($width !== null) || ($height !== null) || $hasMinMaxConstraint) {
$oheight = $this->original->height();
$owidth = $this->original->width();
$oratio = $owidth / $oheight;
Expand Down Expand Up @@ -352,7 +361,7 @@ public function resize(int $width = null, int $height = null, iterable $options

$height = (int) ($width / $oratio);
} else {
if ($width === null && $height === null) {
if (($width === null) && ($height === null)) {
// Neither dimension was given, so pretend they were
$height = $oheight;
$width = $owidth;
Expand Down Expand Up @@ -388,7 +397,7 @@ public function resize(int $width = null, int $height = null, iterable $options
$fit = false;

// Allow upsizing of the image? (default: false)
$allowUpsizing = !empty($this->options['upsize']) && (bool) $this->options['upsize'];
$allowUpsizing = (!empty($this->options['upsize']) && (bool) $this->options['upsize']);

// Should the canvas be resized to the dimensions specified?
$resizeCanvas = false;
Expand Down Expand Up @@ -446,7 +455,7 @@ public function resize(int $width = null, int $height = null, iterable $options
[$mime, $format] = $this->detectFormat(true);

// If it's exporting to a flat image and no background is set, and it was transparent to start off with..
if ($format !== 'png' && $format !== 'webp' && empty($this->options['background']) && $this->detectAlpha()) {
if (($format !== 'png') && ($format !== 'webp') && empty($this->options['background']) && $this->detectAlpha()) {
// Then fill in the background - Would be nice to guess the color but that's not my job
$this->options['background'] = '#fff';
}
Expand All @@ -472,7 +481,7 @@ private function detectFormat(bool $useNewFormat = false)
}

// Determine whether or not to use the new format in
if ($useNewFormat && !empty($this->options['format']) && $this->options['format'] !== 'auto') {
if ($useNewFormat && !empty($this->options['format']) && ($this->options['format'] !== 'auto')) {
$format = $this->options['format'];
} else {
// Get the image resource entity if not already loaded
Expand Down Expand Up @@ -647,7 +656,7 @@ public function modify()

/**
* Render the image in the desired output format, exiting immediately after
*
*
* @return void
*/
public function render() {
Expand Down
8 changes: 8 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
'stretch' => 'Stretch',
],
],
'quality' => [
'label' => 'Default Output Quality',
'comment' => 'Default output quality for images (1-100)'
],
'image_not_found' => [
'label' => '404 Image Source',
'comment' => 'Select a different image to be displayed when images are not found',
Expand All @@ -47,6 +51,10 @@
'label' => '404 Image Resize Mode',
'comment' => 'Resizing mode for image above',
],
'image_not_found_format' => [
'label' => '404 Image Format',
'comment' => 'Output format for image above',
],
'image_not_found_quality' => [
'label' => '404 Image Quality',
'comment' => 'Default output quality for the image above',
Expand Down
28 changes: 17 additions & 11 deletions models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class Settings extends Model

use Validation;

const DEFAULT_IMAGE_NOT_FOUND = '/plugins/abwebdevelopers/imageresize/assets/image-not-found.png';

/**
* Implement settings model
*
*
* @var array
*/
public $implement = [
Expand All @@ -22,36 +24,40 @@ class Settings extends Model

/**
* Define settings code
*
*
* @var string
*/
public $settingsCode = 'abwebdevelopers_imageresize';

/**
* Define settings fields
*
*
* @var string
*/
public $settingsFields = 'fields.yaml';

/**
* Define validation rules for settings
*
*
* @var array
*/
public $rules = [
'driver' => 'required|string|in:gd,imagick',
'background' => 'required|string|regex:/^#([a-f0-9]{3}){1,2}$/i',
'mode' => 'required|string|in:auto,cover,contain,stretch',
'image_not_found' => 'nullable|string',
'quality' => 'required|min:1|max:100',
'format' => 'required|string|in:auto,jpg,png,bmp,gif,ico,webp',
'background' => 'required|string|regex:/^#([a-f0-9]{3}){1,2}$/i',
'filters.*.modifier' => 'in:width,height,min_width,min_height,max_width,max_height,blur,sharpen,brightness,contrast,pixelate,greyscale,invert,opacity,rotate,flip,background,colorize,format,quality,mode',
'filters.*.value' => 'string|min:0|max:10'
'filters.*.value' => 'string|min:0|max:10',
'image_not_found' => 'nullable',
'image_not_found_background' => 'required|regex:/^#([a-f0-9]{3}){1,2}$/i',
'image_not_found_mode' => 'required|in:auto,cover,contain,stretch',
'image_not_found_quality' => 'required|min:1|max:100',
];

/**
* Define cast types for each modifier type
*
*
* @var array
*/
protected $castModifiers = [
Expand Down Expand Up @@ -80,7 +86,7 @@ class Settings extends Model

/**
* Define validation rules for each modifier type
*
*
* @var array
*/
public $modifierRules = [
Expand Down Expand Up @@ -109,7 +115,7 @@ class Settings extends Model

/**
* Before validating, cast modifier values to their respective type
*
*
* @return void
*/
public function beforeValidate() {
Expand Down Expand Up @@ -145,7 +151,7 @@ public function beforeValidate() {
/**
* Before saving, validate the modifiers which have their own validation logic. Throw an
* exception on fail.
*
*
* @return void
*/
public function beforeSave() {
Expand Down
38 changes: 30 additions & 8 deletions models/settings/fields.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ tabs:
gd: 'abwebdevelopers.imageresize::lang.settings.fields.driver.options.gd'
imagick: 'abwebdevelopers.imageresize::lang.settings.fields.driver.options.imagick'
default: gd
background:
label: 'abwebdevelopers.imageresize::lang.settings.fields.background.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.background.comment'
tab: 'abwebdevelopers.imageresize::lang.settings.tabs.main'
span: right
type: colorpicker
default: '#ffffff'
mode:
label: 'abwebdevelopers.imageresize::lang.settings.fields.mode.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.mode.comment'
tab: 'abwebdevelopers.imageresize::lang.settings.tabs.main'
span: right
span: left
required: 1
type: dropdown
options:
Expand All @@ -28,7 +35,7 @@ tabs:
label: 'abwebdevelopers.imageresize::lang.settings.fields.format.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.format.comment'
tab: 'abwebdevelopers.imageresize::lang.settings.tabs.main'
span: right
span: left
required: 1
type: dropdown
options:
Expand All @@ -40,13 +47,13 @@ tabs:
gif: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.gif'
ico: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.ico'
default: auto
background:
label: 'abwebdevelopers.imageresize::lang.settings.fields.background.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.background.comment'
quality:
label: 'abwebdevelopers.imageresize::lang.settings.fields.quality.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.quality.comment'
tab: 'abwebdevelopers.imageresize::lang.settings.tabs.main'
span: left
type: colorpicker
default: '#ffffff'
type: text
default: 65
_404:
type: section
label: 'abwebdevelopers.imageresize::lang.settings.sections._404.label'
Expand Down Expand Up @@ -77,11 +84,26 @@ tabs:
cover: 'abwebdevelopers.imageresize::lang.settings.fields.mode.options.cover'
stretch: 'abwebdevelopers.imageresize::lang.settings.fields.mode.options.stretch'
default: contain
image_not_found_format:
label: 'abwebdevelopers.imageresize::lang.settings.fields.image_not_found_format.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.image_not_found_format.comment'
tab: 'abwebdevelopers.imageresize::lang.settings.tabs.main'
span: left
type: dropdown
options:
auto: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.auto'
jpg: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.jpg'
png: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.png'
webp: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.webp'
bmp: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.bmp'
gif: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.gif'
ico: 'abwebdevelopers.imageresize::lang.settings.fields.format.options.ico'
default: contain
image_not_found_quality:
label: 'abwebdevelopers.imageresize::lang.settings.fields.image_not_found_quality.label'
comment: 'abwebdevelopers.imageresize::lang.settings.fields.image_not_found_quality.comment'
tab: 'abwebdevelopers.imageresize::lang.settings.tabs.main'
span: right
span: left
type: text
default: 65
filters:
Expand Down
99 changes: 99 additions & 0 deletions updates/seed_filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php namespace ABWebDevelopers\Portfolio\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;
use ABWebDevelopers\ImageResize\Models\Settings;

class SeedFilters extends Migration
{
public function up()
{
$filters = Settings::get('filters');

$hasThumbnail = false;
$hasHero = false;

foreach ($filters as $key => $value) {
if ($value['code'] == 'thumbnail') {
$hasThumbnail = true;
} elseif ($value['code'] == 'hero') {
$hasHero = true;
}
}

if (!$hasThumbnail) {
$filters[] = [
"code" => "thumbnail",
"description" => "Basic thumbnail filter",
"rules" => [
[
"modifier" => "max_width",
"value" => "500",
],
[
"modifier" => "max_height",
"value" => "500",
],
[
"modifier" => "brightness",
"value" => "50",
],
[
"modifier" => "background",
"value" => "#fff",
],
[
"modifier" => "greyscale",
"value" => "true",
],
[
"modifier" => "quality",
"value" => "60",
],
[
"modifier" => "format",
"value" => "jpg",
],
[
"modifier" => "mode",
"value" => "cover",
],
],
];
}

if (!$hasHero) {
$filters[] = [
"code" => "hero",
"description" => "Standard hero filter",
"rules" => [
[
"modifier" => "width",
"value" => "1920",
],
[
"modifier" => "height",
"value" => "500",
],
[
"modifier" => "mode",
"value" => "cover",
],
[
"modifier" => "quality",
"value" => "80",
],
[
"modifier" => "format",
"value" => "jpg",
],
],
];
}
}

public function down()
{

}
}
Loading

0 comments on commit 3c90b89

Please sign in to comment.