【问题标题】:Symfony sfThumbnail Plugin with 5 Simultaneous UploadsSymfony sfThumbnail 插件,具有 5 个同时上传
【发布时间】:2012-07-12 00:47:38
【问题描述】:

我有一个产品表单,用户可以同时上传 5 张图片。一切正常,但我正在尝试实现 sfThumbnail 插件,以便它同时创建每张图片的缩略图。下面的代码适用于 Picture1,但 Picture2、3、4 和 5 不适用。我试过弄乱代码,我不能让它单独处理每个文件。它仅以 Picture2 的上传文件为例,并将其命名为与图片 1 相同的名称。因此,当它运行时,我最终得到了 5 张上传的图片,但只有 1 个带有第一张图片文件名的缩略图。

我尝试复制代码 5 次并为每张图片自定义它,但我得到了相同的结果。

如果有更有效的方法,请告诉我。这正是我从插件文档中得到的。谢谢

if($this->isModified())
    {
      $uploadDir = sfConfig::get('sf_upload_dir');
      $thumbnail = new sfThumbnail(150, 150);
      $thumbnail->loadFile($uploadDir.'/car/'.$this->getPicture1());
      $thumbnail->save($uploadDir.'/car/thumbnail/'. $this->getPicture1());
    }

【问题讨论】:

    标签: image symfony1 upload thumbnails


    【解决方案1】:

    首先删除你的函数:

    if($this->isModified())
        {
          $uploadDir = sfConfig::get('sf_upload_dir');
          $thumbnail = new sfThumbnail(150, 150);
          $thumbnail->loadFile($uploadDir.'/car/'.$this->getPicture1());
          $thumbnail->save($uploadDir.'/car/thumbnail/'. $this->getPicture1());
        }
    

    在你的表单类中插入这个函数:

    protected function processUploadedFile($field, $filename = null, $values = null) {
    
              // prendo il file che è stato caricato
              $fn = parent::processUploadedFile( $field, $filename, $values );
    
              // se il file esiste ed
              if ( $fn != "" ) {
    
                   try {
    
                      // array multidimensionale di tutte le tipologie di thumb esistenti
                      $thumbnails[] = array( 'dir' => 'ico',           'width' => 75,      'height' => 75 );
                      $thumbnails[] = array( 'dir' => 'small',      'width' => 150, 'height' => 150 );
                      $thumbnails[] = array( 'dir' => 'medium',      'width' => 400, 'height' => 400 );
                      $thumbnails[] = array( 'dir' => 'big',           'width' => 800, 'height' => 800 );
    
                      foreach ( $thumbnails as $thumbParam )
                      {
                        $currentFile = sfConfig::get( 'sf_upload_dir' ).'/destination_folder/'.$thumbParam[ 'dir' ].'/'. $fn;
                        if ( is_file( $currentFile ) ) unlink( $currentFile );
                      }
    
                      foreach ( $thumbnails as $thumbParam )
                      {
                          $thumbnail = new sfThumbnail( $thumbParam[ 'width' ], $thumbParam[ 'height' ], true, false );
                          $thumbnail->loadFile( sfConfig::get( 'sf_upload_dir' )."/destination_folder/original/".$fn );
                          $thumbnail->save( sfConfig::get( 'sf_upload_dir' ).'/destination_folder/'.$thumbParam[ 'dir' ].'/'.$fn, 'image/jpeg' );
                      }
    
                   } catch( Exception $e ) {
    
                   }
              }
    
              return $fn;
         }
    

    地点:
    - $thumbnails[] 数组包含您需要的所有文件夹,具有不同的尺寸,因此您需要的所有拇指 - 您需要先在 web/uploads 目录中创建 destination_folder 文件夹

    还要记得调用命令

    php symfony project:permissions
    

    此函数获取表单中的所有文件,并根据需要生成尽可能多的拇指

    【讨论】:

    • 此代码源中的snippet 根本无法回答问题...(除非您对其进行自定义)
    【解决方案2】:

    这对我有用:

    if($this->isModified())
        {
          $uploadDir = sfConfig::get('sf_upload_dir');
          $thumbnail = new sfThumbnail(150, 150);
          if($this->getPicture1())
          {
            $thumbnail->loadFile($uploadDir.'/car/'.$this->getPicture1());
            $thumbnail->save($uploadDir.'/car/thumbnail/'. $this->getPicture1());
          }
    
          if($this->getPicture2())
          {
            $thumbnail->loadFile($uploadDir.'/car/'.$this->getPicture2());
            $thumbnail->save($uploadDir.'/car/thumbnail/'. $this->getPicture2());
          }
    
    .. the rest of my the pictures
        }
    

    【讨论】:

    • 你可以把每张图片的名字(array('picture1', 'picture2', ...))放在一个数组里面循环。
    • 嘿j0k。在有人尝试仅上传少于 5 张图片之前,此代码似乎可以正常工作。每张图片都是以空错误出的。我尝试做一个 if 语句“if($this->getPicture1() 'NULL')”,但它仍然抛出错误。有任何想法吗?谢谢!
    • 您不应该检查它是否为空,但可能有点空,例如:if(strlen($this->getPicture1())) 或直接if($this->getPicture1())
    • 一如既往,您是对的。我已经用我更新的代码更新了我的答案。再次感谢。 :)
    猜你喜欢
    • 2021-10-19
    • 2013-05-14
    • 2021-07-19
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多