Php 通过使用Symfony 3.0 config来调整图像大小,需要制作或使用哪种库

Php 通过使用Symfony 3.0 config来调整图像大小,需要制作或使用哪种库,php,doctrine-orm,bundle,symfony,Php,Doctrine Orm,Bundle,Symfony,您好,我在我的asympfony 3.0项目中想创建一个库,当构建该库以从config.yml读取以下参数时: 最大宽度 最大高度 库将读取它们,并将图像大小调整为config.yml提供的这些维度,或以任何其他方式读取config 为了更清楚,我有以下实体: <?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints a

您好,我在我的asympfony 3.0项目中想创建一个库,当构建该库以从config.yml读取以下参数时:

  • 最大宽度
  • 最大高度
库将读取它们,并将图像大小调整为config.yml提供的这些维度,或以任何其他方式读取config

为了更清楚,我有以下实体:

<?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;
    }
}
这将从config.yml读取最大宽度和最大高度,并执行图像大小调整。ImageProcessor是一个库,可以进行任何类型的图像处理,如裁剪、调整大小等

问题不在于如何调整图像大小,而在于如何通过config.yml使此库可重新设置并易于配置。我找不到一本好的教程或手册来做这件事。

希望这能有所帮助:

很快,您将需要实现

  • Configuration.php将在config.yml中描述捆绑包配置树
  • 在bundle扩展中捕获值并以您选择的方式存储它们(在全局参数中或使用DI容器将其传递给库)

我对LiipImagineBundle有很好的经验:-自动创建和缓存各种大小的缩略图非常容易。我只想获得infp voia Ajac呼叫,这就是为什么此扩展没有帮助。
new ImageProcessor($image_path,$extention)->to_thumb($filename)