2017 © Pedro Peláez
 

symfony-bundle media-bundle

This Bundle provides tools to rapidly process image with Symfony

image

symfony-helper/media-bundle

This Bundle provides tools to rapidly process image with Symfony

  • Friday, June 3, 2016
  • by itcreator
  • Repository
  • 1 Watchers
  • 0 Stars
  • 37 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Symfony media bundle

This Bundle provides tools to rapidly process image with Symfony, (*1)

Instalation

add package to composer

    "symfony-helper/media-bundle": "@dev",

Register bundle in AppKernel

    public function registerBundles()
        {
            $bundles = [
                ...
                new SHelper\MediaBundle\SHelperMediaBundle(),
                ...
            ];

            ...

            return $bundles;
        }

Configuration

Add configuration config to config.yml

s_helper_media:
    original_resolution:
        width: 1920
        height: 1080
    previews:
        small:
            width: 32
            height: 32
        thumbnail:
            width: 640
            height: 480
        ... other resolutions

Preview section is not required. Parameters small and thumbnail are only example. You can define custom subsections in preview section., (*2)

Configure Doctrine ORM mapping

doctrine:
    orm:
        mappings:
            AppBundle:      # this is only example section 
                type: annotation
                prefix: App\Entity
            SHelperMediaBundle:     # Media bundle mapping
                type: annotation
                dir: "Model/Entity"
                prefix: SHelper\MediaBundle\Model\Entity

Usage

Steps: - Upload an image and get image ID in response. (Not multipart) - Attach this image to you custom entity - Enjoy and be happy, (*3)

Media bundle will create entity Image. You must make a unidirectional relation to this Image entity., (*4)

Add an unidirectional relation

/**
 * User
 *
 * @ORM\Table(name="`user`")
 * @ORM\Entity
 */
class User implements UserInterface
{
    ...
    /**
     * @var Image
     *
     * @ORM\OneToOne(targetEntity="SHelper\MediaBundle\Model\Entity\Image")
     * @ORM\JoinColumn(name="avatar_id", referencedColumnName="id", nullable=true)
     */
    private $avatar;
    ...
}

Create image manually

Inject image service to your service., (*5)

    class YourService
    {
        /** @var IImageService */
        private $imageService;

        public function __construct(IImageService $imageService)
        {
            $this->imageService = $imageService;
        }
        ...

        public function doSomething()
        {
            $imageBlob = file_get_contents('filename.jpg');
            $image = $this->imageService->createImageFromBlob($imageBlob);

            ...

            $user->setAvatar($image);
            //OR
            $article->setAvatar($image);
            //OR
            $someThingElse->setAvatar($image);
        }
    }

The Versions