VichUploader 和 LiipImagine :调整大小并保存上传的图像
VichUploader and LiipImagine : resize and save the uploaded image
我正在做一个 symfony 项目,我正在使用 vichuploader 和 liipImagine,所以我想调整上传图片的大小并将它(调整后的图片)保存在上传目录中,以便用户上传图片最大 10Mb,然后调整大小以保持 100 KB 的大小。
到目前为止,我发现可以使用 imagine_filter 调整上传图片的大小,但在这种情况下,它会保留原始图片,并在缓存中创建调整大小的图片。
所以我试图做这样的事情:
在我的控制器中
public function edit(Post $post,Request $request)
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
// And then resize all uploaded images...
// All i know is to get the filename of each image stored in public/media/posts.
// In loop because we allow multiple upload
// foreach ($post->getPictures() as $picture) {
// $picture->getFilename();
// }
$this->addFlash('success', 'ELement successfully modified');
return $this->redirectToRoute('admin.post.index');
}
return $this->render('admin/post/edit.html.twig', [
'post' => $post,
'form' => $form->createView()
]);
}
我的文章类型
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('category', EntityType::class, [
'class' => Category::class,
'required' => true,
'choice_label' => 'title'
])
->add('pictureFiles', FileType::class, [
'required' => false,
'multiple' => true
])
->add('content')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
感谢您的帮助!
由于找不到任何建议,我决定使用 InterventionImage。我知道这不是最好的做法,但是当你得不到你需要的东西时,你就会满足于你所拥有的。
正如我上面所说,目标是在服务器上保留最小的图像并允许用户上传大图像。
对于那些可能感兴趣的人,我添加了这个
public function edit(Post $post,Request $request)
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$pics = [];
foreach ($post->getPictures() as $picture) {
// this path of the image
$targetPath = 'media/posts/' . $picture->getFileName();
// and then resize it
$this->resizeImage($targetPath);
}
dump($pics);
$this->addFlash('success', 'ELement successfully modified');
return $this->redirectToRoute('admin.post.index');
}
return $this->render('admin/post/edit.html.twig', [
'post' => $post,
'form' => $form->createView()
]);
}
private function resizeImage($targetPath)
{
$manager = new ImageManager(['driver' => 'gd']);
$manager->make($targetPath)->widen(768, function ($constraint) {
$constraint->upsize();
})->save($targetPath);
}
如果有其他改进的想法,我会注意的,
谢谢:)
我正在做一个 symfony 项目,我正在使用 vichuploader 和 liipImagine,所以我想调整上传图片的大小并将它(调整后的图片)保存在上传目录中,以便用户上传图片最大 10Mb,然后调整大小以保持 100 KB 的大小。
到目前为止,我发现可以使用 imagine_filter 调整上传图片的大小,但在这种情况下,它会保留原始图片,并在缓存中创建调整大小的图片。
所以我试图做这样的事情: 在我的控制器中
public function edit(Post $post,Request $request)
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
// And then resize all uploaded images...
// All i know is to get the filename of each image stored in public/media/posts.
// In loop because we allow multiple upload
// foreach ($post->getPictures() as $picture) {
// $picture->getFilename();
// }
$this->addFlash('success', 'ELement successfully modified');
return $this->redirectToRoute('admin.post.index');
}
return $this->render('admin/post/edit.html.twig', [
'post' => $post,
'form' => $form->createView()
]);
}
我的文章类型
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('category', EntityType::class, [
'class' => Category::class,
'required' => true,
'choice_label' => 'title'
])
->add('pictureFiles', FileType::class, [
'required' => false,
'multiple' => true
])
->add('content')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
感谢您的帮助!
由于找不到任何建议,我决定使用 InterventionImage。我知道这不是最好的做法,但是当你得不到你需要的东西时,你就会满足于你所拥有的。 正如我上面所说,目标是在服务器上保留最小的图像并允许用户上传大图像。 对于那些可能感兴趣的人,我添加了这个
public function edit(Post $post,Request $request)
{
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$pics = [];
foreach ($post->getPictures() as $picture) {
// this path of the image
$targetPath = 'media/posts/' . $picture->getFileName();
// and then resize it
$this->resizeImage($targetPath);
}
dump($pics);
$this->addFlash('success', 'ELement successfully modified');
return $this->redirectToRoute('admin.post.index');
}
return $this->render('admin/post/edit.html.twig', [
'post' => $post,
'form' => $form->createView()
]);
}
private function resizeImage($targetPath)
{
$manager = new ImageManager(['driver' => 'gd']);
$manager->make($targetPath)->widen(768, function ($constraint) {
$constraint->upsize();
})->save($targetPath);
}
如果有其他改进的想法,我会注意的, 谢谢:)