- How do I install and use 1tomany/data-uri in a Laravel project?
- Install via Composer with `composer require 1tomany/data-uri`. Use `DataDecoder::decode()` for data URIs, URLs, or files, or `decodeBase64()`/`decodeText()` for specific formats. Example: `$file = (new DataDecoder())->decode('data:text/plain;base64,SGVsbG8=');` The result is an immutable object with auto-deleting temp files.
- Does this package work with Laravel’s Storage facade or Filesystem contracts?
- No, it lacks native Laravel integration. You’ll need to wrap the `DataUriInterface` in a custom `FilesystemAdapter` to use it with Laravel’s `Storage::put()` or `Filesystem` contracts. The package is framework-agnostic but works seamlessly with Laravel’s PHP 8.1+ stack.
- What Laravel versions and PHP versions are supported?
- The package supports PHP 8.1+ and is compatible with Laravel 10+. It has no framework-specific dependencies, so it works in any Laravel 10+ project without conflicts. Tested with Laravel’s latest LTS releases.
- How does performance compare to Laravel’s native Filesystem::put() for large files?
- For large files, this package streams data via `stream_get_contents()`, which is efficient but may hit PHP’s `memory_limit` for extremely large payloads. Laravel’s `Filesystem::put()` is optimized for disk operations, while this package excels at ephemeral, in-memory processing. Benchmark with your specific use case.
- Can I manually control when the temporary file is deleted instead of auto-deletion?
- No, the package auto-deletes files on object destruction or garbage collection. If you need manual control, consider extending the class or using a wrapper to expose a `delete()` method. The current design prioritizes safety by preventing orphaned files.
- How does MIME type detection work, and can I override it?
- MIME types are auto-detected using `mime_content_type()` by default. You can override this by passing a `string|Type|null` to `decode()`. For example, force `text/markdown` instead of `text/plain` for Markdown files. Explicit types are recommended for consistency.
- What happens if the data URI or URL is invalid or unreachable?
- The package throws exceptions for invalid data URIs or unreachable URLs. For remote URLs, consider adding validation (e.g., Guzzle middleware) before passing data to `DataDecoder`. Integrate with Laravel’s exception handlers for graceful error handling.
- Are there alternatives to this package for Laravel?
- For Laravel-specific solutions, consider `spatie/array-to-xml` (for XML) or `league/flysystem` (for filesystem abstraction). However, for RFC 2397-compliant data URIs, this package is lightweight and dependency-free. If you need deeper Laravel integration, a custom `FilesystemAdapter` is the best approach.
- How do I test this package in a Laravel application?
- Test edge cases like malformed URIs, non-existent URLs, and large files using PHPUnit. Mock `fopen()` and `stream_get_contents()` to simulate remote files. The package includes basic tests, but extend them for Laravel-specific scenarios (e.g., custom adapters).
- Can I use this package to process files from S3 or other cloud storage?
- Yes, the package streams data, so it works with S3 streams via Laravel’s `Filesystem` contracts. Pass the stream URL to `DataDecoder::decode()`, and it will handle the rest. For direct S3 integration, wrap the result in a `FilesystemAdapter` to use with `Storage::disk('s3')`.