From 1e6573cc621188dd218ff58b681a1ddbab21ea6b Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 21 Aug 2020 14:07:23 -0600 Subject: [PATCH] Cleaned up inline docs for ImageResizer, fixed tests --- modules/system/classes/ImageResizer.php | 71 ++----------------- .../unit/system/classes/ImageResizerTest.php | 51 +++---------- 2 files changed, 14 insertions(+), 108 deletions(-) diff --git a/modules/system/classes/ImageResizer.php b/modules/system/classes/ImageResizer.php index 67ef4edff..7cd04b324 100644 --- a/modules/system/classes/ImageResizer.php +++ b/modules/system/classes/ImageResizer.php @@ -13,68 +13,6 @@ use System\Models\File as SystemFileModel; use October\Rain\Database\Attach\File as FileModel; use October\Rain\Database\Attach\Resizer as DefaultResizer; -/** - * $width = numeric, 'auto' | false | null - * $height = numeric, 'auto' | false | null - * $options = null | array [ - * 'mode' => [ - * 'auto', // automatically choose between portrait and landscape based on the image's orientation - * 'exact', // resize to the exact dimensions given, without preserving aspect ratio - * 'portrait', // resize to the given height and adapt the width to preserve aspect ratio - * 'landscape', // resize to the given width and adapt the height to preserve aspect ratio - * 'crop', // crop to the given dimensions after fitting as much of the image as possible inside those - * 'fit', // fit the image inside the given maximal dimensions, keeping the aspect ratio - * ], - * 'quality' => numeric, 1 - 100 - * 'interlace' => boolean (default false), - * 'extension' => ['auto', 'png', 'gif', 'jpg', 'jpeg', 'webp', 'bmp', 'ico'], - * 'offset' => [x, y] Offset to crop the image from - * 'sharpen' => numeric, 1 - 100 - * - * // Options that could be processed by an addon - * - * 'blur' => numeric, 1 - 100 - * 'brightness'=> numeric, -100 - 100 - * 'contrast' => numeric, -100 - 100 - * 'pixelate' => numeric, 1 - 5000 - * 'greyscale' => boolean - * 'invert' => boolean - * 'opacity' => numeric, 0 - 100 - * 'rotate' => numeric, 1 - 360 - * 'flip' => [h, v] - * 'background' | 'fill' => string, hex value - * 'colourize' => string, RGB value - * ] - * - * Event::fire('system.resizer.afterResize') - * Event::fire('system.resizer.beforeResize') - * Event::fire('system.resizer.processResize') - * Event::fire('system.resizer.getAvailableSources', [&$sourcesArray]) - * - * - */ - -/** - * DRAFT RESIZER DESIGN - * This is a rough draft, very WIP, of what the Image resizer UX / API will look like in October. - * - * Notes: - * - Clearing the application cache should not invalidate any existing resized images - * - Invalid images should not result in a valid "image not found" image existing, it should result in a 404 or more specific error - * - Provide a new backend list column type "thumb" that will pass it through the resizer - * - * Configurations to support - * - * - Developer can provide a image (in a wide range of various formats so long as the application actually has access to the provided image - * and can understand how to access it) to the `| resize(width, height, options)` Twig filter. That filter will output either a link to the - * final generated image as requested or a link to the resizer route that will actually handle resizing the image. - * - User should be able to extend the image resizing to provide pre or post processing of the images before / after being resized - * also to include the ability to swap out the image resizer itself. The core workflow logic should remain the same though. - * Examples: - * - Post processing of resized images with TinyPNG to optimize filesize further - * - Replacement processing of resizing with Intervention Image (using GD or ImageMagick) - */ - /** * Image Resizing class used for resizing any image resources accessible * to the application. @@ -91,11 +29,14 @@ use October\Rain\Database\Attach\Resizer as DefaultResizer; * * The functionality of this class is controlled by these config items: * - * - cms.resized.disk - - * - cms.resized.folder - - * - cms.resized.path - + * - cms.resized.disk - The disk to store resized images on + * - cms.resized.folder - The folder on the disk to store resized images in + * - cms.resized.path - The public path to the resized images as returned + * by the storage disk's URL method, used to identify + * already resized images * * @see System\Classes\SystemController System controller + * @see System\Twig\Extension Twig filters for this class defined * @package october\system * @author Luke Towers */ diff --git a/tests/unit/system/classes/ImageResizerTest.php b/tests/unit/system/classes/ImageResizerTest.php index e9969c90d..04f4f70be 100644 --- a/tests/unit/system/classes/ImageResizerTest.php +++ b/tests/unit/system/classes/ImageResizerTest.php @@ -179,7 +179,7 @@ class ImageResizerTest extends PluginTestCase $this->copyMedia(); $imageResizer = new ImageResizer( - 'themes/test/assets/images/october.png', + '/themes/test/assets/images/october.png', 100, 100 ); @@ -220,7 +220,7 @@ class ImageResizerTest extends PluginTestCase // Plugin URL (relative URL) $imageResizer = new ImageResizer( - 'plugins/database/tester/assets/images/avatar.png', + '/plugins/database/tester/assets/images/avatar.png', 100, 100 ); @@ -236,7 +236,7 @@ class ImageResizerTest extends PluginTestCase // Module URL (relative URL) $imageResizer = new ImageResizer( - 'modules/backend/assets/images/favicon.png', + '/modules/backend/assets/images/favicon.png', 100, 100 ); @@ -251,6 +251,10 @@ class ImageResizerTest extends PluginTestCase $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); // URL for a FileModel instance (absolute URL) + $fileModel = new FileModel(); + $fileModel->fromFile(base_path('tests/fixtures/plugins/database/tester/assets/images/avatar.png')); + $fileModel->save(); + $imageResizer = new ImageResizer( FileModel::first()->getPath(), 100, @@ -267,52 +271,13 @@ class ImageResizerTest extends PluginTestCase $fileModel->save(); $imageResizer = new ImageResizer( - str_replace(base_path() . '/', '', FileModel::first()->getLocalPath()), + str_replace(url('') . '/', '/', FileModel::first()->getPath()), 100, 100 ); $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); } - /** - * Tests paths for sources that can be accessed via paths. - * - * @return void - */ - public function testPathSources() - { - // Plugin path (relative) - $imageResizer = new ImageResizer( - '/plugins/database/tester/assets/images/avatar.png', - 100, - 100 - ); - $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); - - // Plugin path (absolute) - $imageResizer = new ImageResizer( - base_path('tests/fixtures/plugins/database/tester/assets/images/avatar.png'), - 100, - 100 - ); - $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); - - // Path of a FileModel instance (absolute) - $fileModel = new FileModel(); - $fileModel->fromFile(base_path('tests/fixtures/plugins/database/tester/assets/images/avatar.png')); - $fileModel->save(); - - $imageResizer = new ImageResizer( - FileModel::first()->getLocalPath(), - 100, - 100 - ); - $this->assertEquals('png', $imageResizer->getConfig()['options']['extension']); - - // Remove FileModel instance - $fileModel->delete(); - } - public function testDirectSources() { // FileModel instance itself