0

我有一个产品表单,用户可以同时上传 5 张图片。一切正常,但我正在尝试实现 sfThumbnail 插件,以便它同时创建每张图片的缩略图。下面的代码适用于 Picture1,但 Picture2、3、4 和 5 不适用。我试过弄乱代码,我不能让它单独处理每个文件。它仅以图片 2 的上传文件为例,并将其命名为与图片 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());
    }
4

2 回答 2

0

首先删除您的功能:

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[]数组包含您需要的所有文件夹,具有 ri 尺寸,因此您需要的所有拇指 - 您在创建之前需要destination_folderweb/uploads 目录中创建文件夹之前

还记得调用命令

php symfony project:permissions

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

于 2012-07-12T10:34:28.913 回答
0

这对我有用:

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
    }
于 2012-07-12T09:44:51.173 回答