symfony-helper/media-bundle

此Bundle提供工具,以快速使用Symfony处理图片

安装: 38

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

开放问题: 1

类型:symfony-bundle

V0.1.0 2016-06-03 08:59 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:45 UTC


README

此Bundle提供工具,以快速使用Symfony处理图片

安装

将包添加到composer

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

在AppKernel中注册Bundle

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

配置

将配置添加到config.yml

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

预览部分不是必需的。参数smallthumbnail仅为示例。您可以在预览部分定义自定义子部分。

配置Doctrine ORM映射

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

用法

步骤

  • 上传一张图片,并在响应中获取图片ID。(非multipart类型)
  • 将此图片附加到您自定义的实体上
  • 享受并快乐

Media bundle将创建实体Image。您必须为此Image实体创建单向关系。

添加单向关系

/**
 * 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;
    ...
}

手动创建图片

将图片服务注入到您的服务中。

    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);
        }
    }