我有一个产品表单,用户可以同时上传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());
}
答案 0 :(得分: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
}
答案 1 :(得分: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[]
数组包含您需要的所有文件夹,包含尺寸,因此您需要所有的拇指
- 您之前需要在web / uploads目录中创建destination_folder
文件夹
还记得调用命令
php symfony project:permissions
此功能获取表单中的所有文件,并根据需要生成任意数量的鼠标