php – 使用liipImagineBundle更新/删除记录时删除/更新缓存的图
发布时间:2020-12-13 18:06:22 所属栏目:PHP教程 来源:网络整理
导读:我是symfony2的新手. 我正在使用liipImagineBundle来管理图像缩略图. 我有一个产品实体类,它使用Lifecycle Callbacks来管理产品图像. Product.php ?phpnamespace SviplAdminBundleEntity;use DoctrineORMMapping as ORM;use GedmoMappingAnnotation as
我是symfony2的新手.
我正在使用liipImagineBundle来管理图像缩略图. 我有一个产品实体类,它使用Lifecycle Callbacks来管理产品图像. Product.php <?php namespace SviplAdminBundleEntity; use DoctrineORMMapping as ORM; use GedmoMappingAnnotation as GEDMO; use SymfonyComponentHttpFoundationFileUploadedFile; /** * SviplAdminBundleEntityProduct * @ORMEntity * @ORMTable(name="product") * @ORMEntity(repositoryClass="SviplAdminBundleEntityProductRepository") * @ORMHasLifecycleCallbacks */ class Product{ /** * @ORMColumn(type="integer") * @ORMId * @ORMGeneratedValue(strategy="AUTO") */ private $id; /** * @ORMColumn(type="string",length=25,unique=true) */ private $name; /** * @ORMColumn(type="text") */ private $description; /** * @ORMColumn(type="float",length=8) * @var unknown */ private $price; /** * @GEDMOTimestampable(on="update") * @ORMColumn(name="updated_at",type="datetime") */ private $updated_at; /** * @GEDMOTimestampable(on="create") * @ORMColumn(name="created_at",type="datetime") */ private $created_at; /** * @ORMManyToOne(targetEntity="Category",inversedBy="products") * @ORMJoinColumn(name="category_id",referencedColumnName="id") */ protected $category; /** * @ORMColumn(name="image",type="string",length=50) */ private $image; public function getAbsolutePath() { return null === $this->image ? null : $this->getUploadRootDir().'/'.$this->image; } public function getWebPath() { return null === $this->image ? null : $this->getUploadDir().'/'.$this->image; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__.'/../../../../web/'.$this->getUploadDir(); } protected function getUploadDir() { // get rid of the __DIR__ so it doesn't screw up // when displaying uploaded doc/image in the view. return 'uploads/product'; } private $file; /** * Get file. * * @return UploadedFile */ public function getFile() { return $this->file; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return Product */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set description * * @param string $description * @return Product */ public function setDescription($description) { $this->description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set price * * @param float $price * @return Product */ public function setPrice($price) { $this->price = $price; return $this; } /** * Get price * * @return float */ public function getPrice() { return $this->price; } /** * Set updated_at * * @param DateTime $updatedAt * @return Product */ public function setUpdatedAt($updatedAt) { $this->updated_at = $updatedAt; return $this; } /** * Get updated_at * * @return DateTime */ public function getUpdatedAt() { return $this->updated_at; } /** * Set created_at * * @param DateTime $createdAt * @return Product */ public function setCreatedAt($createdAt) { $this->created_at = $createdAt; return $this; } /** * Get created_at * * @return DateTime */ public function getCreatedAt() { return $this->created_at; } /** * Set category * * @param SviplAdminBundleEntityCategory $category * @return Product */ public function setCategory(SviplAdminBundleEntityCategory $category = null) { $this->category = $category; return $this; } /** * Get category * * @return SviplAdminBundleEntityCategory */ public function getCategory() { return $this->category; } /** * Set image * * @param string $image * @return Product */ public function setImage($image) { $this->image = $image; return $this; } /** * Get image * * @return string */ public function getImage() { return $this->image; } private $temp; /** * Sets file. * * @param UploadedFile $file */ public function setFile(UploadedFile $file = null) { $this->file = $file; // check if we have an old image path if (isset($this->image)) { // store the old name to delete after the update $this->temp = $this->image; $this->image = null; } else { $this->image = 'initial'; } } /** * @ORMPrePersist() * @ORMPreUpdate() */ public function preUpload() { if (null !== $this->getFile()) { // do whatever you want to generate a unique name $filename = sha1(uniqid(mt_rand(),true)); $this->image = $filename.'.'.$this->getFile()->guessExtension(); } } /** * @ORMPostPersist() * @ORMPostUpdate() */ public function upload() { if (null === $this->getFile()) { return; } // if there is an error when moving the file,an exception will // be automatically thrown by move(). This will properly prevent // the entity from being persisted to the database on error $this->getFile()->move($this->getUploadRootDir(),$this->image); // check if we have an old image if (isset($this->temp)) { // delete the old image unlink($this->getUploadRootDir().'/'.$this->temp); // clear the temp image path $this->temp = null; } $this->file = null; } /** * @ORMPostRemove() */ public function removeUpload() { if ($file = $this->getAbsolutePath()) { unlink($file); } } } config.yml ... liip_imagine: filter_sets: my_thumb: quality: 75 filters: thumbnail: { size: [120,90],mode: outbound } 缩略图生成代码 ... <img src="{{ asset('uploads/product/' ~ form_object.vars.value.image) | imagine_filter('my_thumb',true) }}" /> ... 缩略图生成正确. 但是,当原始图像更改或删除表单时,我无法找到更新/删除缓存图像的方法.
您应该注册preUpdate和preRemove
event listener/subscriber,注入所需的服务并删除其中的图像.
由于您无权访问服务容器(并且您不应将服务注入实体),因此无法使用Lifecycle Events查询LiipImagineBundle的服务以从实体内部获取缓存文件. 您可以注入服务liip_imagine.cache.manager并使用它的remove()方法从缓存中删除图像. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |