symfony-helper / media-bundle
此Bundle提供工具,以快速使用Symfony处理图片
V0.1.0
2016-06-03 08:59 UTC
Requires
- php: >=7.0.1
- ext-imagick: *
- doctrine/doctrine-bundle: ^1.6
- doctrine/doctrine-cache-bundle: ^1.2
- doctrine/orm: ^2.5
- friendsofsymfony/rest-bundle: ~1.7.8
- jms/di-extra-bundle: ~1.7
- jms/serializer-bundle: ~1.1.0
- symfony/symfony: 3.0.*
- willdurand/hateoas-bundle: ~1.1.1
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
预览部分不是必需的。参数small
和thumbnail
仅为示例。您可以在预览部分定义自定义子部分。
配置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); } }