Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Asset/AssetIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use craft\helpers\Assets as AssetsHelper;
use craft\helpers\Db as DbHelper;
use craft\helpers\FileHelper;
use craft\helpers\Image;
use CraftCms\Cms\Asset\Data\AssetIndexEntry;
use CraftCms\Cms\Asset\Data\IndexingSession;
use CraftCms\Cms\Asset\Data\Volume;
Expand All @@ -24,6 +23,7 @@
use CraftCms\Cms\Cms;
use CraftCms\Cms\Database\Table;
use CraftCms\Cms\Filesystem\Data\FsListing;
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\Image\ImageTransformHelper;
use CraftCms\Cms\Support\Json;
use CraftCms\Cms\Support\Str;
Expand Down Expand Up @@ -643,14 +643,14 @@ public function indexFileByEntry(

if ($isLocalFs) {
$transformSourcePath = $asset->getImageTransformSourcePath();
$dimensions = Image::imageSize($transformSourcePath);
$dimensions = ImageHelper::imageSize($transformSourcePath);
} else {
if (! $cacheImages) {
try {
$stream = $asset->getStream();

if (is_resource($stream)) {
$dimensions = Image::imageSizeByStream($stream);
$dimensions = ImageHelper::imageSizeByStream($stream);
fclose($stream);
}
} catch (VolumeException $e) {
Expand All @@ -661,7 +661,7 @@ public function indexFileByEntry(
if (! is_array($dimensions)) {
$tempPath = AssetsHelper::tempFilePath(pathinfo($filename, PATHINFO_EXTENSION));
AssetsHelper::downloadFile($volume->sourceDisk(), $indexEntry->uri, $tempPath);
$dimensions = Image::imageSize($tempPath);
$dimensions = ImageHelper::imageSize($tempPath);

$asset->setMimeType(FileHelper::getMimeType($tempPath));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Asset/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use craft\helpers\Assets as AssetsHelper;
use craft\helpers\DateTimeHelper;
use craft\helpers\FileHelper;
use craft\helpers\Image;
use CraftCms\Cms\Asset\Contracts\AssetPreviewHandlerInterface;
use CraftCms\Cms\Asset\Data\VolumeFolder;
use CraftCms\Cms\Asset\Elements\Asset;
Expand All @@ -29,6 +28,7 @@
use CraftCms\Cms\Filesystem\Filesystems\Temp;
use CraftCms\Cms\Image\Data\ImageTransform;
use CraftCms\Cms\Image\FallbackTransformer;
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\Support\Env;
use CraftCms\Cms\Support\Facades\Filesystems;
use CraftCms\Cms\Support\Str;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function getThumbUrl(Asset $asset, int $width, ?int $height = null, bool

$extension = $asset->getExtension();

if (! Image::canManipulateAsImage($extension)) {
if (! ImageHelper::canManipulateAsImage($extension)) {
return $iconFallback ? AssetsHelper::iconUrl($extension) : null;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public function getThumbUrl(Asset $asset, int $width, ?int $height = null, bool
*/
public function getImagePreviewUrl(Asset $asset, int $maxWidth, int $maxHeight): string
{
$isWebSafe = Image::isWebSafe($asset->getExtension());
$isWebSafe = ImageHelper::isWebSafe($asset->getExtension());
$originalWidth = (int) $asset->getWidth();
$originalHeight = (int) $asset->getHeight();
[$width, $height] = AssetsHelper::scaledDimensions((int) $asset->getWidth(), (int) $asset->getHeight(), $maxWidth, $maxHeight);
Expand Down
20 changes: 10 additions & 10 deletions src/Asset/Elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use craft\helpers\Cp;
use craft\helpers\ElementHelper;
use craft\helpers\FileHelper;
use craft\helpers\Image;
use craft\helpers\Template;
use craft\helpers\UrlHelper;
use craft\services\ElementSources;
Expand Down Expand Up @@ -59,6 +58,7 @@
use CraftCms\Cms\Filesystem\Exceptions\FilesystemException;
use CraftCms\Cms\Filesystem\Filesystems\Filesystem;
use CraftCms\Cms\Image\Data\ImageTransform;
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\Image\ImageTransformHelper;
use CraftCms\Cms\Image\ImageTransforms;
use CraftCms\Cms\Search\SearchQuery;
Expand Down Expand Up @@ -1757,7 +1757,7 @@ public function getUrlsBySize(array $sizes, mixed $transform = null): array

if (
($transform !== null || $this->_transform) &&
Image::canManipulateAsImage($this->getExtension())
ImageHelper::canManipulateAsImage($this->getExtension())
) {
$transform = ImageTransformHelper::normalizeTransform($transform ?? $this->_transform);
} else {
Expand Down Expand Up @@ -1981,7 +1981,7 @@ private function _url(mixed $transform = null, ?bool $immediately = null): ?stri
// otherwise - we can proceed and rely on the FallbackTransformer (e.g. for thumbs in the CP)
// see https://github.com/craftcms/cms/issues/13306 and https://github.com/craftcms/cms/issues/13624 for more info
(request()->isSiteRequest() && ! $this->allowTransforms()) ||
! Image::canManipulateAsImage(pathinfo($this->getFilename(), PATHINFO_EXTENSION))
! ImageHelper::canManipulateAsImage(pathinfo($this->getFilename(), PATHINFO_EXTENSION))
)
) {
$transform = null;
Expand Down Expand Up @@ -2069,7 +2069,7 @@ protected function thumbAlt(): ?string
}

$extension = $this->getExtension();
if (! Image::canManipulateAsImage($extension)) {
if (! ImageHelper::canManipulateAsImage($extension)) {
return $extension;
}

Expand Down Expand Up @@ -2213,7 +2213,7 @@ public function getFormat(mixed $transform = null): string
{
$ext = $this->getExtension();

if (! Image::canManipulateAsImage($ext)) {
if (! ImageHelper::canManipulateAsImage($ext)) {
return $ext;
}

Expand Down Expand Up @@ -2409,7 +2409,7 @@ public function getSupportsImageEditor(): bool
{
$ext = $this->getExtension();

return strcasecmp($ext, 'svg') !== 0 && Image::canManipulateAsImage($ext);
return strcasecmp($ext, 'svg') !== 0 && ImageHelper::canManipulateAsImage($ext);
}

/**
Expand Down Expand Up @@ -2952,7 +2952,7 @@ public function afterSave(bool $isNew): void
Cms::config()->sanitizeCpImageUploads
))
) {
Image::cleanImageByPath($this->tempFilePath);
ImageHelper::cleanImageByPath($this->tempFilePath);
}

// if we're creating or replacing and image, get the width or height via getimagesize
Expand Down Expand Up @@ -3190,13 +3190,13 @@ private function _dimensions(mixed $transform = null): array

$transform ??= $this->_transform;

if ($transform === null || ! Image::canManipulateAsImage($this->getExtension())) {
if ($transform === null || ! ImageHelper::canManipulateAsImage($this->getExtension())) {
return [$this->_width, $this->_height];
}

$transform = ImageTransformHelper::normalizeTransform($transform);

return Image::targetDimensions(
return ImageHelper::targetDimensions(
$this->_width,
$this->_height,
$transform->width,
Expand Down Expand Up @@ -3311,7 +3311,7 @@ private function _relocateFile(): void
$this->kind = Assets::getFileKindByExtension($filename);

if ($this->kind === self::KIND_IMAGE) {
[$this->_width, $this->_height] = Image::imageSize($tempPath);
[$this->_width, $this->_height] = ImageHelper::imageSize($tempPath);
} else {
$this->_width = null;
$this->_height = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Cp/Rebrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace CraftCms\Cms\Cp;

use craft\helpers\Image as ImageHelper;
use craft\services\Path;
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\Support\Str;
use Illuminate\Support\Facades\Storage;

Expand Down
4 changes: 3 additions & 1 deletion src/Dashboard/Widgets/CraftSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use craft\web\assets\craftsupport\CraftSupportAsset;
use CraftCms\Cms\Config\GeneralConfig;
use CraftCms\Cms\Edition;
use CraftCms\Cms\Image\Images;
use CraftCms\Cms\Plugin\Plugins;
use CraftCms\Cms\Support\Facades\HtmlStack;
use CraftCms\Cms\Support\PHP;
Expand All @@ -24,6 +25,7 @@ final class CraftSupport extends Widget
public function __construct(
private readonly GeneralConfig $generalConfig,
private readonly Plugins $plugins,
private readonly Images $images,
array $config = []
) {
parent::__construct($config);
Expand Down Expand Up @@ -85,7 +87,7 @@ public function getBodyHtml(): ?string
$db = $craft->getDb();
$dbDriver = $db->getDriverLabel();

$imagesService = $craft->getImages();
$imagesService = $this->images;
if ($imagesService->getIsGd()) {
$imageDriver = 'GD';
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Users/SaveUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Craft;
use craft\helpers\Assets;
use craft\helpers\FileHelper;
use craft\helpers\Image;
use craft\helpers\UrlHelper;
use craft\web\UploadedFile;
use CraftCms\Cms\Auth\Auth;
Expand All @@ -16,6 +15,7 @@
use CraftCms\Cms\Edition;
use CraftCms\Cms\Element\Element;
use CraftCms\Cms\Http\RespondsWithFlash;
use CraftCms\Cms\Image\ImageHelper;
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use CraftCms\Cms\Site\Sites;
use CraftCms\Cms\Support\Flash;
Expand Down Expand Up @@ -250,7 +250,7 @@ public function __invoke(Request $request): Response

$photo = UploadedFile::getInstanceByName('photo');

if ($photo && ! Image::canManipulateAsImage($photo->getExtension())) {
if ($photo && ! ImageHelper::canManipulateAsImage($photo->getExtension())) {
$user->errors()->add('photo', t('The user photo provided is not an image.'));
}

Expand Down
17 changes: 17 additions & 0 deletions src/Image/Enums/ExifOrientation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace CraftCms\Cms\Image\Enums;

enum ExifOrientation: int
{
case Rotate0 = 1;
case Rotate0Mirrored = 2;
case Rotate180 = 3;
case Rotate180Mirrored = 4;
case Rotate90Mirrored = 5;
case Rotate90 = 6;
case Rotate270Mirrored = 7;
case Rotate270 = 8;
}
11 changes: 11 additions & 0 deletions src/Image/Enums/ImageDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace CraftCms\Cms\Image\Enums;

enum ImageDriver: string
{
case Gd = 'gd';
case Imagick = 'imagick';
}
60 changes: 60 additions & 0 deletions src/Image/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace CraftCms\Cms\Image;

use CraftCms\Cms\Component\Component;

abstract class Image extends Component
{
private $heartBeatCallback;

abstract public function getWidth(): int;

abstract public function getHeight(): int;

abstract public function getExtension(): string;

abstract public function loadImage(string $path): self;

abstract public function crop(int $x1, int $x2, int $y1, int $y2): self;

abstract public function scaleToFit(?int $targetWidth, ?int $targetHeight, bool $scaleIfSmaller = true): self;

abstract public function scaleAndCrop(?int $targetWidth, ?int $targetHeight, bool $scaleIfSmaller = true, array|string $cropPosition = 'center-center'): self;

abstract public function resize(?int $targetWidth, ?int $targetHeight): self;

abstract public function saveAs(string $targetPath, bool $autoQuality = false): bool;

abstract public function getIsTransparent(): bool;

/**
* @param-out int|string $width
* @param-out int|string $height
*/
protected function normalizeDimensions(int|string|null &$width, int|string|null &$height): void
{
if (preg_match('/^([\d]+|AUTO)x([\d]+|AUTO)/', (string) $width, $matches)) {
$width = $matches[1] !== 'AUTO' ? (int) $matches[1] : '';
$height = $matches[2] !== 'AUTO' ? (int) $matches[2] : '';
}

if (! $height || ! $width) {
[$width, $height] = ImageHelper::calculateMissingDimension($width, $height, $this->getWidth(), $this->getHeight());
}
}

public function setHeartbeatCallback(?callable $method = null): void
{
$this->heartBeatCallback = $method;
}

public function heartbeat(): void
{
if (is_callable($this->heartBeatCallback)) {
call_user_func($this->heartBeatCallback);
}
}
}
Loading
Loading