Download the PHP package xaduken/image-symfony-support without Composer

On this page you can find all versions of the php package xaduken/image-symfony-support. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package image-symfony-support

Image manager for symfony

Description:

Component used for make one way for persist information about image to database and store them to filesystem.

Installation

composer require xaduken/image-symfony-support

Usage:

Suppose you have to entity User. This entity need to have avatar. Then you create for example Image entity and make it implements Imageable interface. In this way you guarantee Image table in database will have needle columns.

Image entity may be something like this:

<?php

namespace App\Entity;

use App\Repository\ImageRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Xaduken\ImageSupport\EntityInterface\Imageable;

#[ORM\Entity(repositoryClass: ImageRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Image implements Imageable
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $related_entity = null;

    #[ORM\Column(length: 400, nullable: true)]
    private ?string $path = null;

    #[ORM\Column(length: 255)]
    private ?string $fileName = null;

    #[ORM\Column(length: 255)]
    private ?string $mimeType = null;

    #[ORM\Column]
    private ?DateTimeImmutable $createdAt = null;

    #[ORM\Column]
    private ?DateTimeImmutable $updatedAt = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getRelatedEntity(): ?string
    {
        return $this->related_entity;
    }

    public function setRelatedEntity(string $related_entity): self
    {
        $this->related_entity = $related_entity;

        return $this;
    }

    public function getPath(): ?string
    {
        return $this->path;
    }

    public function setPath(?string $path): self
    {
        $this->path = $path;

        return $this;
    }

    public function getFileName(): ?string
    {
        return $this->fileName;
    }

    public function setFileName(string $fileName): self
    {
        $this->fileName = $fileName;

        return $this;
    }

    public function getMimeType(): ?string
    {
        return $this->mimeType;
    }

    public function setMimeType(string $mimeType): self
    {
        $this->mimeType = $mimeType;

        return $this;
    }

    public function getCreatedAt(): ?DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getUpdatedAt(): ?DateTimeImmutable
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(DateTimeImmutable $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    #[ORM\PreUpdate]
    public function onUpdate(): void
    {
        $this->updatedAt = new DateTimeImmutable();
    }

    #[ORM\PrePersist]
    public function onCreate(): void
    {
        $this->createdAt = new DateTimeImmutable();
        $this->updatedAt = new DateTimeImmutable();
    }
}

As you can see, Image entity impelemts Imageable interface. It`s required.

After that you need to create UserImageManager and extend it by AbstractImageManager.

UserImageManager:

<?php

namespace App\Image;

use App\Entity\Image;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Xaduken\ImageSupport\Factory\ImageManagerFactoryInterface;
use Xaduken\ImageSupport\Factory\SimpleImageManagerFactory;
use Xaduken\ImageSupport\Service\AbstractImageManager;

class UserImageManager extends AbstractImageManager
{
    private EntityManagerInterface $entityManager;

    private string $targetDir;

    public function __construct(EntityManagerInterface $entityManager, string $targetDir)
    {
        $this->entityManager = $entityManager;
        $this->targetDir = $targetDir;
    }

    protected function getTargetDirectory(): string
    {
        return $this->targetDir;
    }

    protected function getRelatedClass(): string
    {
        return User::class;
    }

    protected function getImageManagerFactory(): ImageManagerFactoryInterface
    {
        return new SimpleImageManagerFactory($this->getEntityManager());
    }

    protected function getEntityManager(): EntityManagerInterface
    {
        return $this->entityManager;
    }

    protected function getImageEntityClass(): string
    {
        return Image::class;
    }
}

Let`s look more detail on methods.

getTargetDirectory()

Used for define path, where images for user should be stored. In this case we push it to constructor and transfer it via property. You may use any others way to do it.

getRelatedClass()

Push here full class name which should be stored in related_class column in table references to defined entity in getImageEntityClass method.

getImageManagerFactory()

Push here class instance of ImageManagerFactoryInterface. See full list of available factories on end of this file.

getEntityManager()

Push here doctrine entityManager. In this example we get it from constructor. In usage example we get it from dependency injection.

getImageEntityClass()

Push fully class name of entity implementation Imageable interface. In this case is Image Entity.

Testing:

After create UserImageManager we ready to store our files in database and filesystem used one simple class!

See example:

<?php

namespace App\Controller\Api;

use App\Image\UserImageManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestController extends AbstractController
{
    #[Route(path: '/api/test-image', name: 'test-image')]
    public function testImage(Request $request, UserImageManager $userImageManager): Response
    {
        $image = $request->files->get('image');

        $result = $userImageManager->save($image);

        dd(result);
    }
}

We get UserImageManager via dependency injection and use just one method for save image to database, filesystem and manage his relations!

But maybe you ask how UserImageManager class get $targetDirectory property? It`s very simple. just use symfony manually injection.

/config/services.yaml

parameters:
    ...
    user_image_directory: '%kernel.project_dir%/public/uploads/user'
services:
    ...
    App\Image\UserImageManager:
        arguments:
            $targetDir: '%user_image_directory%'

And that's it! Now if you need to save image related to User entity. Just get UserEntityManager by dependency injection and save your image.

Factories

This is all cool, but that if you need to resize image before save it to filesystem? All some another things? To answer this questions, I use abstract factory pattern.

If you need to define costume way for this, create your own factory, implemented by ImageManagerFactoryInterface and push your implementation to UserImageManger.getImageManagerFactory() method.

ImageManagerFactoryInterface have very simple signature, lets look

ImageManagerFactoryInterface:

<?php

namespace Xaduken\ImageSupport\Factory;

use Xaduken\ImageSupport\Database\DatabaseImageUploaderInterface;
use Xaduken\ImageSupport\Filesystem\FilesystemUploaderInterface;

interface ImageManagerFactoryInterface
{
    public function getDatabaseUploader(): DatabaseImageUploaderInterface;

    public function getFilesystemUploader(): FilesystemUploaderInterface;
}

Let`s see more detail on methods

getDatabaseUploader()

Should return class instance of implementation DatabaseImageUploaderInterface. You should create this implementation. This class should define way, how persist Imageable entity to database. For simple persisting use DatabaseImageUploader class instance.

We discuss how to create implementation of DatabaseUploaderInterface later.

getFilesystemUploader()

Should return class instance of implementation FilesystemUploaderInterface. You also should to make implementation by self or use StandardFilesystemUploader if you need simple save.

Just like with DatabaseImageUploaderInterface we discuss how to make implementation later.

DatabaseImageUploaderInterface

they signature so simple:

<?php

namespace Xaduken\ImageSupport\Database;

use Xaduken\ImageSupport\DTO\ImageInfo;
use Xaduken\ImageSupport\EntityInterface\Imageable;

interface DatabaseImageUploaderInterface
{
    public function upload(ImageInfo $imageInfo, string $relatedEntity, string $imageClass): Imageable;
}

method upload should save Image to database.

FilesystemUploaderInterface

they signature very familiar with DatabaseImageUploaderInterface

<?php

namespace Xaduken\ImageSupport\Filesystem;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Xaduken\ImageSupport\DTO\ImageInfo;

interface FilesystemUploaderInterface
{
    public function upload(UploadedFile $uploadedFile, string $targetDirectory): ImageInfo;
}

method upload should save image to filesystem.

Method should return FileInfo DTO object, which will be used for storing into database.

And maybe you find needle realization of factory in this list:

not so much right now ;)


All versions of image-symfony-support with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
symfony/http-foundation Version ^v6.0.0
doctrine/orm Version ^2.12
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package xaduken/image-symfony-support contains the following files

Loading the files please wait ...