首先删除您的功能:
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_folder
web/uploads 目录中创建文件夹之前
还记得调用命令
php symfony project:permissions
此函数获取表单中的所有文件,并根据需要生成尽可能多的拇指