你好,我的渐近 3.0 项目我想创建一个库,当构建时从 config.yml 读取这些参数:
- max_width
- max_height
该库将读取它们,并将执行图像大小调整为 config.yml 或任何其他读取配置方式提供的这些尺寸。
为了更清楚,我有这个实体:
<?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;
}
}
如您所见,上面的实体具有 Doctrine 触发器。内部函数上传我想调用:
new ImageProcessor($image_path,$extention)->to_thumb($filename)
这将从config.yml读取max_width和max_height,并将执行图像大小调整。图像处理器将是进行任何类型的图像处理(如裁剪,调整大小等)的库。
问题不在于如何调整图像大小,而在于如何使这个库可重新订阅并通过 config.yml 轻松配置。我找不到一个好的教程或手册来做到这一点。
希望这会有所帮助:http://symfony.com/doc/current/cookbook/bundles/configuration.html
很快你需要实现
- 配置.php将在 config.yml 中描述捆绑包配置树 在
- 捆绑包扩展中捕获值并以您选择的方式存储它们(在全局参数中或使用 DI 容器将其传递给库)。