addType('id', Types::STRING); $this->addType('fileId', Types::BIGINT); $this->addType('storageId', Types::BIGINT); $this->addType('oldFileId', Types::BIGINT); $this->addType('locationId', Types::STRING); $this->addType('width', Types::INTEGER); $this->addType('height', Types::INTEGER); $this->addType('mimetypeId', Types::INTEGER); $this->addType('sourceMimetypeId', Types::INTEGER); $this->addType('mtime', Types::INTEGER); $this->addType('size', Types::INTEGER); $this->addType('max', Types::BOOLEAN); $this->addType('cropped', Types::BOOLEAN); $this->addType('encrypted', Types::BOOLEAN); $this->addType('etag', Types::STRING); $this->addType('versionId', Types::STRING); } public static function fromPath(string $path, IMimeTypeDetector $mimeTypeDetector): Preview|false { $preview = new self(); $preview->setFileId((int)basename(dirname($path))); $fileName = pathinfo($path, PATHINFO_FILENAME) . '.' . pathinfo($path, PATHINFO_EXTENSION); $ok = preg_match('/(([A-Za-z0-9\+\/]+)-)?([0-9]+)-([0-9]+)(-(max))?(-(crop))?\.([a-z]{3,4})/', $fileName, $matches); if ($ok !== 1) { return false; } [ 2 => $version, 3 => $width, 4 => $height, 6 => $max, 8 => $crop, ] = $matches; $preview->setMimeType($mimeTypeDetector->detectPath($fileName)); $preview->setWidth((int)$width); $preview->setHeight((int)$height); $preview->setCropped($crop === 'crop'); $preview->setMax($max === 'max'); if (!empty($version)) { $preview->setVersion($version); } return $preview; } public function getName(): string { $path = ($this->getVersion() > -1 ? $this->getVersion() . '-' : '') . $this->getWidth() . '-' . $this->getHeight(); if ($this->isCropped()) { $path .= '-crop'; } if ($this->isMax()) { $path .= '-max'; } $ext = $this->getExtension(); $path .= '.' . $ext; return $path; } public function getExtension(): string { return match ($this->getMimeType()) { 'image/png' => 'png', 'image/gif' => 'gif', 'image/jpeg' => 'jpg', 'image/webp' => 'webp', default => 'png', }; } public function setBucketName(string $bucketName): void { $this->bucketName = $bucketName; } public function setObjectStoreName(string $objectStoreName): void { $this->objectStoreName = $objectStoreName; } public function setVersion(?string $version): void { $this->version = $version; } public function getMimeType(): string { return $this->mimetype; } public function setMimeType(string $mimeType): void { $this->mimetype = $mimeType; } public function getSourceMimeType(): string { return $this->sourceMimetype; } public function setSourceMimeType(string $mimeType): void { $this->sourceMimetype = $mimeType; } }