MIMEType = $header; $this->Data = urldecode($parts[1]); return; } // Data is URL-encoded; set MIME type and decode the data $this->MIMEType = explode(";", $header, 2)[0]; $this->Data = base64_decode($parts[1]); } /** * Gets the raw data extracted from the data URL. * * @return string|null The raw data or null if parsing failed. */ public function GetData(): ?string { return $this->Data; } /** * Gets the size of the raw data in bytes. * * @return int The size of the data, or 0 if data is null. */ public function GetSize(): int { return $this->Data ? strlen($this->Data) : 0; } /** * Gets the MIME type of the data URL. * * @return string|null The MIME type or null if parsing failed. */ public function GetMimeType(): ?string { return $this->MIMEType; } /** * Converts the object back to a valid data URL string. * * @return string The reconstructed data URL. */ public function __toString() { // If data or MIME type is missing, return an empty string if (!$this->Data || !$this->MIMEType) return ''; // Encode based on the MIME type if (in_array($this->MIMEType, self::$EncodedMIMETypes)) return "data:{$this->MIMEType}," . urlencode($this->Data); // Base64-encode all other data return "data:{$this->MIMEType};base64," . base64_encode($this->Data); } }