Hello fellows I on my asympfony 3.0 project I want to make a Library that when is constructed to read from config.yml these parameters:
And the library will read them and will perfom a image resize to these dimentions provided by config.yml or any other way to read config.
In order to be more clear I have this Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Filesystem\Filesystem;
/**
* @ORM\Entity
* @ORM\Table(name="images")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ImagesRepository")
* @ORM\HasLifecycleCallbacks
*/
class Images
{
/**
* @ORM\Column(type="string", length=60)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="AppBundle\Doctrine\AutoIdGenerate")
*/
private $id;
/**
* Filename of the Image
* @ORM\Column(type="string", length=100)
*/
private $name;
/**
* Filename of the Thumbnail
* @ORM\Column(type="string", length=100)
*/
private $name_small;
/**
* ImageGroup og the Image
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\ImageGroups", inversedBy="images")
*/
private $group;
/**
* @Assert\File(maxSize="20000000")
*/
private $file;
private $upload_dir='images';
private $temp;
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file,$upload_dir)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->name))
{
// store the old name to delete after the update
$this->temp = $this->name;
$this->name = null;
}
else
{
$this->name = sha1(uniqid(mt_rand(), true)).'.'.$file->guessExtension();
}
$this->name_small="small_".$this->name;
$this->upload_dir=$upload_dir;
return $this;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile())
{
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->name = $filename.'.'.$this->getFile()->guessExtension();
$this->name_small='small_'.$this->name;
}
}
/**
*Getting the directory that will upload the file
*/
public function getUploadRootDir()
{
$dir= __DIR__.'/../../../web/'.$this->upload_dir;
return $dir;
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
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
//-- Make the Thumbnail Here --
$dir=$this->getUploadRootDir();
$fs = new Filesystem();
if(!$fs->exists($dir))
{
$fs->mkdir($dir,0777,true);
}
$file=$this->getFile();
$file->move($dir, $this->name);
// 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;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
$file = $this->getUploadRootDir();
error_reporting(E_ERROR | E_PARSE); //If the file does not exist just ingore the warning
if (file_exists($file.'/'.$this->name)===true)
{
unlink($file.'/'.$this->name);
}
if(file_exists($file.'/'.$this->name_small)===true);
{
unlink($file.'/'.$this->name_small);
}
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);//But we want warnings back
}
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get nameSmall
*
* @return string
*/
public function getNameSmall()
{
return $this->name_small;
}
/**
* Set group
*
* @param \AppBundle\Entity\ImageGroups $group
*
* @return Images
*/
public function setGroup(\AppBundle\Entity\ImageGroups $group = null)
{
$this->group = $group;
return $this;
}
/**
* Get group
*
* @return \AppBundle\Entity\ImageGroups
*/
public function getGroup()
{
return $this->group;
}
/**
* Set name
*
* @param string $name
*
* @return Images
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Set nameSmall
*
* @param string $nameSmall
*
* @return Images
*/
public function setNameSmall($nameSmall)
{
$this->name_small = $nameSmall;
return $this;
}
}
As you can see the entity above has Doctrine triggers. Inside function upload I want to call:
new ImageProcessor($image_path,$extention)->to_thumb($filename)
And this will read from config.yml the max_width and max_height and will perform the image resize. The ImageProcessor will be the library that will do any kind of image processings like crop, resize, etc etc.
The problem is NOT how to resize the image but on how can I make this Library resubable and easily configurable by config.yml. I cannot find a good tutorial or manual to do that.
Hope this will help: http://symfony.com/doc/current/cookbook/bundles/configuration.html
Shortly you'll need to implement