Symfony 4][LiipImagine]在控制器中使用liipImagine。

问题描述 投票:0回答:1

(在寻求帮助之前,我想告诉你我的英语很烂,真的很抱歉......) 我是一个真正的初学者,在php和symfony所以这就是为什么我在这里...)

我的问题是:

我试图在一个有图片库的网站上使用LiipImagine,因为我需要在上传照片时创建一些缩略图,我在文档中看到我可以在我的控制器中使用LiipImagine和我的上传方法,所以,在做任何事情之前,我试图复制粘贴文档中的代码,但没有成功,试图在互联网上找到,但我总是得到错误的信息 "未找到服务 "liip_imagine.cache.mnagaer"

我知道我需要在我的控制器中注入LiipImagine,但我有点困惑,我不知道如何正确地做它,我不知道如何正确地使用我的过滤器后。

我的上传方法是:

/**
 * @Route("/new", name="photo_new", methods={"GET","POST"})
 */
public function new(CategorieRepository  $cat, Request $request): Response
{
    $photo = new Photo();
    $form = $this->createForm(PhotoType::class, $photo);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $entityManager = $this->getDoctrine()->getManager();

        // Je récupère les informations du fichier uploadé
        $photoUploade = $form->get("nom_photo")->getData();

        // Je récupère le nom du fichier uploadé
        $nomPhoto = pathinfo($photoUploade->getClientOriginalName(), PATHINFO_FILENAME);

        // Je remplace les espaces dans le nom du fichier
        $nomPhoto = str_replace(" ", "_", $nomPhoto);

        // Je rajoute un string unique (pour éviter les fichiers doublons) et l'extension du fichier téléchargé
        $nomPhoto .= uniqid() . "." . $photoUploade->guessExtension();

        // J'enregistre le fichier uploadé sur mon serveur, dans le dossier public/images
        $photoUploade->move("images", $nomPhoto);

        // Pour enregistrer l'information en BDD
        $photo->setnomPhoto($nomPhoto);
        $entityManager->persist($photo);
        $entityManager->flush();

        return $this->redirectToRoute('photo_index');
    }

如果有人能帮助我理解它,我将非常感激... ...

dependency-injection controller symfony4 liipimaginebundle
1个回答
0
投票

明白了...

需要这个类

use Liip\ImagineBundle\Service\FilterService;

然后在方法中注入它,然后

$minPhoto = $FilterService->getUrlOfFilteredImage("images/".$nomPhoto, 'mini');

它使我的缩略图与我的 "迷你 "过滤器。

© www.soinside.com 2019 - 2024. All rights reserved.