From: Cyperghost Date: Tue, 5 Nov 2024 10:54:21 +0000 (+0100) Subject: Add image cropper configuration X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=29927c0486bf0ee5593a3c2a6011b51df62352d9;p=GitHub%2FWoltLab%2FWCF.git Add image cropper configuration --- diff --git a/wcfsetup/install/files/lib/system/file/processor/AbstractFileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/AbstractFileProcessor.class.php index 750edfeb35..21634bd5a0 100644 --- a/wcfsetup/install/files/lib/system/file/processor/AbstractFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/AbstractFileProcessor.class.php @@ -90,4 +90,11 @@ abstract class AbstractFileProcessor implements IFileProcessor { // Do not track downloads. } + + #[\Override] + public function getImageCropperConfiguration(): ?ImageCropperConfiguration + { + // Do not crop images. + return null; + } } diff --git a/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php index 701d22ce1a..2682d30570 100644 --- a/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php @@ -156,4 +156,9 @@ interface IFileProcessor * file types that are served by the web server itself. */ public function trackDownload(File $file): void; + + /** + * Returns the image cropper configuration for this file processor. + */ + public function getImageCropperConfiguration(): ?ImageCropperConfiguration; } diff --git a/wcfsetup/install/files/lib/system/file/processor/ImageCropSize.class.php b/wcfsetup/install/files/lib/system/file/processor/ImageCropSize.class.php new file mode 100644 index 0000000000..993fb20213 --- /dev/null +++ b/wcfsetup/install/files/lib/system/file/processor/ImageCropSize.class.php @@ -0,0 +1,20 @@ +width / $this->height; + } +} diff --git a/wcfsetup/install/files/lib/system/file/processor/ImageCropperConfiguration.class.php b/wcfsetup/install/files/lib/system/file/processor/ImageCropperConfiguration.class.php new file mode 100644 index 0000000000..4929f6d75d --- /dev/null +++ b/wcfsetup/install/files/lib/system/file/processor/ImageCropperConfiguration.class.php @@ -0,0 +1,72 @@ +aspectRatio = $size->aspectRatio(); + + foreach ($sizes as $size) { + if ($size->aspectRatio() !== $this->aspectRatio) { + throw new \InvalidArgumentException('All sizes must have the same aspect ratio.'); + } + } + + \usort($sizes, function (ImageCropSize $a, ImageCropSize $b) { + if ($a->width > $a->height) { + return $a->width <=> $b->width; + } else { + return $a->height <=> $b->height; + } + }); + $this->sizes = $sizes; + } + + /** + * Creates an image cropper with minimum and maximum size with the same aspect ratio. + * The user can freely select, move and scale. + * However, the cropping area is limited to `$min` and `$max`. + */ + public static function createMinMax(ImageCropSize $min, ImageCropSize $max): self + { + return new self(ImageCropperType::MinMax, $min, $max); + } + + /** + * Creates an image cropper that reduces the image to a specific size + * and only allows the user to move the cropping area. + * The size is determined by `$sizes` and corresponds to the smallest side of the image that is the next smaller + * or equal size of `$sizes`. The aspect ratio of the uploaded image is retained. + * + * Example: + * `$sizes` is [128x128, 256x256] + * - Image is 100x200 + * - Image is rejected + * - Image is 200x150 + * - Image is resized to 170x128 + * - Image is 150x200 + * - Image is resized to 128x170 + * - Image is 300x300 + * - Image is resized to 256x256 + */ + public static function createExact(ImageCropSize ...$sizes): self + { + return new self(ImageCropperType::Exact, ...$sizes); + } +} diff --git a/wcfsetup/install/files/lib/system/file/processor/ImageCropperType.class.php b/wcfsetup/install/files/lib/system/file/processor/ImageCropperType.class.php new file mode 100644 index 0000000000..54330f3bed --- /dev/null +++ b/wcfsetup/install/files/lib/system/file/processor/ImageCropperType.class.php @@ -0,0 +1,9 @@ +