4

我正在使用dustin10/VichUploaderBundle 上传图片。

我正在使用 Gregwar/ImageBundle 来调整图像的大小。

dustin10/VichUploaderBundle 有一个 POST_UPLOAD 事件。如何触发事件。我已阅读文档,但没有说明如何触发事件。

https://github.com/dustin10/VichUploaderBundle/blob/master/Event/Events.php

计划是在上传后使用 ImageBundle 调整图像大小。

小号

4

1 回答 1

8

您不能“触发”该事件,它已在此处触发:

   /**
     * Checks for file to upload.
     *
     * @param object $obj       The object.
     * @param string $fieldName The name of the field containing the upload (has to be mapped).
     */
    public function upload($obj, $fieldName)
    {
        $mapping = $this->getMapping($obj, $fieldName);
        // nothing to upload
        if (!$this->hasUploadedFile($obj, $mapping)) {
            return;
        }
        $this->dispatch(Events::PRE_UPLOAD, new Event($obj, $mapping));
        $this->storage->upload($obj, $mapping);
        $this->injector->injectFile($obj, $mapping);
        $this->dispatch(Events::POST_UPLOAD, new Event($obj, $mapping));
    }

您可以做的是处理我认为您所指的事件。您可以通过创建一个如此处所述的侦听器来做到这一点。侦听器将POST_UPLOAD像这样侦听事件:

# app/config/services.yml
services:
    app_bundle.listener.uploaded_file_listener:
        class: AppBundle\EventListener\UploadedFileListener
        tags:
            - { name: kernel.event_listener, event: vich_uploader.post_upload, method: onPostUpload }

您的侦听器类将为 vich uploader 事件键入提示,如下所示:

// src/AppBundle/EventListener/AcmeRequestListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernel;
use Vich\UploaderBundle\Event\Event;

class UploadedFileListener
{
    public function onPostUpload(Event $event)
    {
        $uploadedFile = $event->getObject();
        // your custom logic here
    }
}
于 2015-10-13T14:16:37.283 回答