Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove/update cached image while update/delete record with liipImagineBundle
    primarykey
    data
    text
    <p>I am new to symfony2. I am using liipImagineBundle to manage image thumbnail. I have a product entity class which uses Lifecycle Callbacks to manage product image.</p> <p>Product.php</p> <pre><code>&lt;?php namespace Svipl\AdminBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as GEDMO; use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Svipl\AdminBundle\Entity\Product * @ORM\Entity * @ORM\Table(name="product") * @ORM\Entity(repositoryClass="Svipl\AdminBundle\Entity\ProductRepository") * @ORM\HasLifecycleCallbacks */ class Product{ /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(type="string", length=25, unique=true) */ private $name; /** * @ORM\Column(type="text") */ private $description; /** * @ORM\Column(type="float", length=8) * @var unknown */ private $price; /** * @GEDMO\Timestampable(on="update") * @ORM\Column(name="updated_at", type="datetime") */ private $updated_at; /** * @GEDMO\Timestampable(on="create") * @ORM\Column(name="created_at", type="datetime") */ private $created_at; /** * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ protected $category; /** * @ORM\Column(name="image", type="string", length=50) */ private $image; public function getAbsolutePath() { return null === $this-&gt;image ? null : $this-&gt;getUploadRootDir().'/'.$this-&gt;image; } public function getWebPath() { return null === $this-&gt;image ? null : $this-&gt;getUploadDir().'/'.$this-&gt;image; } protected function getUploadRootDir() { // the absolute directory path where uploaded // documents should be saved return __DIR__.'/../../../../web/'.$this-&gt;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-&gt;file; } /** * Get id * * @return integer */ public function getId() { return $this-&gt;id; } /** * Set name * * @param string $name * @return Product */ public function setName($name) { $this-&gt;name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this-&gt;name; } /** * Set description * * @param string $description * @return Product */ public function setDescription($description) { $this-&gt;description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this-&gt;description; } /** * Set price * * @param float $price * @return Product */ public function setPrice($price) { $this-&gt;price = $price; return $this; } /** * Get price * * @return float */ public function getPrice() { return $this-&gt;price; } /** * Set updated_at * * @param \DateTime $updatedAt * @return Product */ public function setUpdatedAt($updatedAt) { $this-&gt;updated_at = $updatedAt; return $this; } /** * Get updated_at * * @return \DateTime */ public function getUpdatedAt() { return $this-&gt;updated_at; } /** * Set created_at * * @param \DateTime $createdAt * @return Product */ public function setCreatedAt($createdAt) { $this-&gt;created_at = $createdAt; return $this; } /** * Get created_at * * @return \DateTime */ public function getCreatedAt() { return $this-&gt;created_at; } /** * Set category * * @param \Svipl\AdminBundle\Entity\Category $category * @return Product */ public function setCategory(\Svipl\AdminBundle\Entity\Category $category = null) { $this-&gt;category = $category; return $this; } /** * Get category * * @return \Svipl\AdminBundle\Entity\Category */ public function getCategory() { return $this-&gt;category; } /** * Set image * * @param string $image * @return Product */ public function setImage($image) { $this-&gt;image = $image; return $this; } /** * Get image * * @return string */ public function getImage() { return $this-&gt;image; } private $temp; /** * Sets file. * * @param UploadedFile $file */ public function setFile(UploadedFile $file = null) { $this-&gt;file = $file; // check if we have an old image path if (isset($this-&gt;image)) { // store the old name to delete after the update $this-&gt;temp = $this-&gt;image; $this-&gt;image = null; } else { $this-&gt;image = 'initial'; } } /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { if (null !== $this-&gt;getFile()) { // do whatever you want to generate a unique name $filename = sha1(uniqid(mt_rand(), true)); $this-&gt;image = $filename.'.'.$this-&gt;getFile()-&gt;guessExtension(); } } /** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if (null === $this-&gt;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-&gt;getFile()-&gt;move($this-&gt;getUploadRootDir(), $this-&gt;image); // check if we have an old image if (isset($this-&gt;temp)) { // delete the old image unlink($this-&gt;getUploadRootDir().'/'.$this-&gt;temp); // clear the temp image path $this-&gt;temp = null; } $this-&gt;file = null; } /** * @ORM\PostRemove() */ public function removeUpload() { if ($file = $this-&gt;getAbsolutePath()) { unlink($file); } } } </code></pre> <p>config.yml</p> <pre><code>... liip_imagine: filter_sets: my_thumb: quality: 75 filters: thumbnail: { size: [120, 90], mode: outbound } </code></pre> <p>Thumbnail generation code</p> <pre><code>... &lt;img src="{{ asset('uploads/product/' ~ form_object.vars.value.image) | imagine_filter('my_thumb', true) }}" /&gt; ... </code></pre> <p>Thumbnail generating correctly.</p> <p>But I am not able to find the way to update/remove cached image while original image change or delete with form.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload