Skip to content

Latest commit

 

History

History
102 lines (70 loc) · 2.41 KB

uploading.md

File metadata and controls

102 lines (70 loc) · 2.41 KB

Uploading

For uploading images we use StorableImageInterface and any class implementing UploaderInterface.

Currently, we have 2 implementation of UploaderInterface:

FilePathUploader

use Contributte\Imagist\Entity\StorableImage;
use Contributte\Imagist\ImageStorageInterface;
use Contributte\Imagist\Uploader\FilePathUploader;

/** @var ImageStorageInterface $imageStorage */
$imageStorage;

$image = $imageStorage->persist(new StorableImage(
    new FilePathUploader('path/to/image.png'),
    'nameOfImage.png',
));

$image->getId(); // now image is PersistentImageInterface that has a unique ID

StringUploader

use Contributte\Imagist\Entity\StorableImage;
use Contributte\Imagist\ImageStorageInterface;
use Contributte\Imagist\Uploader\StringUploader;

/** @var ImageStorageInterface $imageStorage */
$imageStorage;

$image = $imageStorage->persist(new StorableImage(
    new StringUploader(file_get_contents('path/to/image.png')),
    'nameOfImage.png',
));

$image->getId(); // now image is PersistentImageInterface that has a unique ID

FileUploadUploader

use Contributte\Imagist\Bridge\Nette\Uploader\FileUploadUploader;
use Contributte\Imagist\Entity\StorableImage;
use Contributte\Imagist\ImageStorageInterface;
use Nette\Http\FileUpload;

/** @var ImageStorageInterface $imageStorage */
$imageStorage;

$image = $imageStorage->persist(new StorableImage(
    new FileUploadUploader(new FileUpload(/* ... */)),
    'nameOfImage.png',
));

$image->getId(); // now image is PersistentImageInterface that has a unique ID

UploadedFileUploader

TODO

Custom Uploader

use Contributte\Imagist\Uploader\UploaderInterface;

class ResourceUploader implements UploaderInterface
{

    public function __construct(
        private resource $resource,
    ) {}

    public function getContent(): string
    {
        assert(($contents = stream_get_contents($this->resource)) !== false);

        return $contents;
    }

}

$image->getId(); // now image is PersistentImageInterface that has a unique ID

Next Steps

Scopes
Persisting and retrieving (database)