‪TYPO3CMS  ‪main
ImageResource.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 use TYPO3\CMS\Core\Type\File\ImageInfo;
26 
32 class ImageResource
33 {
34  public function __construct(
35  protected int $width,
36  protected int $height,
37  protected string $extension,
38  protected string $fullPath,
39  protected ?string ‪$publicUrl = null,
40  protected ?File $originalFile = null,
41  protected ?ProcessedFile $processedFile = null
42  ) {}
43 
44  public static function createFromImageInfo(ImageInfo $imageInfo): self
45  {
46  return new self(
47  $imageInfo->getWidth(),
48  $imageInfo->getHeight(),
49  $imageInfo->getExtension(),
50  $imageInfo->getRealPath(),
51  ‪PathUtility::stripPathSitePrefix($imageInfo->getRealPath()),
52  );
53  }
54 
55  public static function createFromProcessedFile(ProcessedFile $processedFile): self
56  {
57  return new self(
58  (int)$processedFile->getProperty('width'),
59  (int)$processedFile->getProperty('height'),
60  $processedFile->getExtension(),
61  ‪Environment::getPublicPath() . '/' . $processedFile->getPublicUrl(),
62  $processedFile->getPublicUrl(),
63  $processedFile->getOriginalFile(),
64  $processedFile
65  );
66  }
67 
68  public function getWidth(): int
69  {
70  return $this->width;
71  }
72 
73  public function withWidth(int $width): self
74  {
75  $imageResource = clone $this;
76  $imageResource->width = $width;
77  return $imageResource;
78  }
79 
80  public function getHeight(): int
81  {
82  return $this->height;
83  }
84 
85  public function withHeight(int $height): self
86  {
87  $imageResource = clone $this;
88  $imageResource->height = $height;
89  return $imageResource;
90  }
91 
92  public function getExtension(): string
93  {
94  return $this->extension;
95  }
96 
97  public function withExtension(string $extension): self
98  {
99  $imageResource = clone $this;
100  $imageResource->extension = $extension;
101  return $imageResource;
102  }
103 
104  public function getFullPath(): string
105  {
106  return $this->fullPath;
107  }
108 
109  public function withFullPath(string $fullPath): self
110  {
111  $imageResource = clone $this;
112  $imageResource->fullPath = $fullPath;
113  return $imageResource;
114  }
115 
116  public function getPublicUrl(): ?string
117  {
118  return ‪$this->publicUrl;
119  }
120 
121  public function withPublicUrl(string ‪$publicUrl): self
122  {
123  $imageResource = clone $this;
124  $imageResource->publicUrl = ‪$publicUrl;
125  return $imageResource;
126  }
127 
128  public function getOriginalFile(): ?File
129  {
130  return $this->originalFile;
131  }
132 
133  public function withOriginalFile(?File $originalFile): self
134  {
135  $imageResource = clone $this;
136  $imageResource->originalFile = $originalFile;
137  return $imageResource;
138  }
139 
140  public function getProcessedFile(): ?ProcessedFile
141  {
142  return $this->processedFile;
143  }
144 
145  public function withProcessedFile(?ProcessedFile $processedFile): self
146  {
147  $imageResource = clone $this;
148  $imageResource->processedFile = $processedFile;
149  return $imageResource;
150  }
151 
157  public function getLegacyImageResourceInformation(): array
158  {
159  return [
160  0 => $this->width,
161  1 => $this->height,
162  2 => $this->extension,
163  3 => $this->fullPath,
164  'origFile' => ‪$this->publicUrl,
165  'origFile_mtime' => $this->originalFile?->getModificationTime(),
166  ];
167  }
168 }
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static stripPathSitePrefix(string $path)
Definition: PathUtility.php:429
‪TYPO3\CMS\Core\Imaging
Definition: Dimension.php:16
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Webhooks\Message\$publicUrl
‪identifier readonly string readonly string $publicUrl
Definition: FileUpdatedMessage.php:36
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:103