介紹:
我使用的Bundle:
"vich/uploader-bundle": "^1.4";
"liip/imagine-bundle": "^1.7"
配置:
1)首先需使用composer加載這兩個Bundle,
2)註冊:
new Vich\UploaderBundle\VichUploaderBundle(),
new Liip\ImagineBundle\LiipImagineBundle()
3)配置路由
_liip_imagine:
resource: "@LiipImagineBundle/Resources/config/routing.xml"
4)配置config
配置VichUploaderBundle
vich_uploader:
db_driver: orm # or mongodb or propel or phpcr
mappings:
#實體中要配置的(mapping="dream_post");dream_post是個名稱而已須要對應;
dream_post:
#這裏是配置文件存儲目錄會存儲在web/attach/artice下
uri_prefix: /attach/article
upload_destination: "%kernel.root_dir%/../web/attach/article"
#vich_uploader裏的服務名稱
namer: vich_uploader.namer_uniqid
# 你本身寫的服務名稱app.vich.directory.namer是我寫的一個服務
directory_namer: app.vich.directory.namer
inject_on_load: false
delete_on_update: true
delete_on_remove: true
dream_attach:
uri_prefix: /attach/attach
upload_destination: "%kernel.root_dir%/../data/attach/attach"
namer: vich_uploader.namer_uniqid
directory_namer: app.vich.directory.namer
inject_on_load: false
delete_on_update: true
delete_on_remove: true
配置LiipImagineBundle
liip_imagine :
# configure resolvers
resolvers :
# setup the default resolver
default :
# use the default web path
web_path : ~
# your filter sets are defined here
filter_sets :
# use the default cache configuration
cache : ~
thumbnail_preview :
quality : 75
filters :
thumbnail : { size : [80, 50], mode : outbound }
5)寫上傳服務:
<?php
namespace AppBundle\Service;
use Vich\UploaderBundle\Naming\DirectoryNamerInterface;
use Vich\UploaderBundle\Mapping\PropertyMapping;
class VichDirectoryNamer implements DirectoryNamerInterface
{
/**
* Creates a directory name for the file being uploaded.
*
* @param object $object The object the upload is attached to.
* @param PropertyMapping $mapping The mapping to use to manipulate the given object.
*
* @return string The directory name.
*/
public function directoryName($object, PropertyMapping $mapping){
$name = $mapping->getFileName($object);
$directoryName = "";
for($i=0;$i<5;$i++){
$directoryName = $directoryName.substr($name,0,$i+1).'/';
}
return $directoryName;
}
}
配置服務
在本AppBundle裏配置
services:
app.vich.directory.namer:
class: AppBundle\Service\VichDirectoryNamer
其中app.vich.directory.namer:就是剛纔config中的本身寫的服務名稱
若是這裏看不明白能夠加我微信或者加SymfonyQQ羣詢問
微信號:WhenDreams 回覆消息"Symfony上傳圖片" ;QQ羣:182983780
6)寫實體屬性
/**
* @var string
*
* @ORM\Column(name="filename", type="string", length=255)
*/
private $filename;
/**
* @var string
*
* @Vich\UploadableField(mapping="dream_post", fileNameProperty="filename")
*/
private $attach;
public function setAttach($attach){
$this->attach = $attach;
if ($attach) {
$this->created = new \DateTime('now');
}
return $this;
}
public function getAttach(){
return $this->attach;
}
7)表單配置
->add('attach', FileType::class, [
'label' => '上傳',
'required' => $options['data']->getId() == null
]);
8)頁面顯示:
<th>Image</th>
<td>
<a href="{{ vich_uploader_asset(post,'attach')}}" target="_blank">
<img src="{{ vich_uploader_asset(post,'attach')|imagine_filter('thumbnail_preview')}}" >
</a>
</td>
=================================
若是須要所有代碼關注我微信號whenDreams回覆"Symfony上傳圖片",
或者加QQ羣:182983780
![](http://static.javashuo.com/static/loading.gif)
這是個人微信號php