我有一个产品形式,用户可以同时上传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());
}发布于 2012-07-12 17:44:51
这是对我有效的方法:
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-12 18:34:29
首先删除你的函数:
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[]目录中的destination_folder文件夹还要记住调用该命令
php symfony project:permissions此函数获取表单中的所有文件,并根据需要生成任意数量的拇指
https://stackoverflow.com/questions/11448951
复制相似问题