(符号4)您如何使用Liip / Imagine生成(缩略图)文件对象并存储到Amazon S3?

时间:2019-02-06 05:23:09

标签: symfony amazon-s3

我有一个表格,可以让您上传许多文件,因此对于我要上传的每个文件:

  • 创建上传文件的缩略图
  • 将所有文件(上传的文件及其生成的缩略图)存储到Amazon S3。

原始上传的文件可以很好地保存到Amazon S3,但是我不知道如何以正确的格式生成Liip / Imagine缩略图,并且可以以与原始上传文件相同的方式/格式保存到S3。

这是控制器中的代码:

if ($form->isSubmitted())
{
    if ($form->isValid())
    {
        $postRepository->save($post);
        $postId = $post->getId();

        $files = $form['file']->getData();
        foreach ($files as $f) {
            $fileArray = array();

            // create the thumbnails
            $pathName = $f->getPathName();
            $imagineFilter = $this->get('liip_imagine.service.filter');
            $fileThumbSquare = $imagineFilter->[what is the function to call here?]($pathName, 'thumb_square');
            $fileThumbRectangleMd = $imagineFilter->[what is the function to call here?]($pathName, 'thumb_rectangle_md'); 
            $fileThumbThumbHd = $imagineFilter->[what is the function to call here?]($pathName, 'thumb_hd');

            $fileArray['file'] = $f;  // the original uploaded file, this is fine and uploads to S3 with no problems
            $fileArray['fileThumbSquare'] = $fileThumbSquare;
            $fileArray['fileThumbRectangleMd'] = $fileThumbRectangleMd;
            $fileArray['fileThumbThumbHd'] = $fileThumbThumbHd;

            $postRepository->addFilesForPost($fileArray, $post);
        }

这是存储库函数的外观,该函数接收上载文件的数组及其缩略图:

所以addFilesForPost函数:

public function addFilesForPost(array $fileArray, Post $post)
{
    $fileName = $fileArray['file']->getClientOriginalName();
    $extension = pathinfo($fileName, PATHINFO_EXTENSION);

    if(in_array($extension, Photo::FILE_TYPE_ARRAY))
    {
        $fObj = new Photo();
        // this works fine 
        $fObj->setFile($fileArray['file']);

        // but I don't know how what function of Liip/Imagine to call to put these in the correct format as the $fileArray['file'] file.
        $fObj->setThumbSquare($fileArray['fileThumbSquare']);
        $fObj->setThumbRectangle($fileArray['fileThumbRectangleMd']);
        $fObj->setThumbHd($fileArray['fileThumbThumbHd']);

        $fObj->setUser($post->getUser());
        $fObj->setPost($post);
        $this->photoRepository->save($fObj);
    }
    else
    {
        return;
    }

}

photoRepository->save函数的外观如下: 同样,这会将我的原始上传文件上传到S3,但是对于缩略图,我不知道要发送什么。

/**
 * @param Photo $photo
 */
public function save(Photo $photo)
{
    if(!$photo->getUser() instanceof User)
        throw new \LogicException('Photo requires User to be set!');

    $em = $this->getEntityManager();

    $filename = $photo->getUser()->getId().'_'.sha1(uniqid(mt_rand(), true));
    $extension = $photo->getFile()->getClientOriginalExtension();
    if($extension == 'php') $extension = 'txt'; // rename php files to txt files

    $key = sprintf('%s.%s', $filename, $extension);

    // so eventually I'll call this for the thumbnails too.
    $this->s3Client->putObject([
        'Key' => $key,
        'Bucket' => $this->params->get('aws.bucket'),
        'SourceFile' => $photo->getFile()  // will eventually have $photo->getThumbSquare(), etc...
    ]);
    $photo->setFile($key);

    // persist to db
    $em->persist($photo);
    $em->flush($photo);

    // fire photo added event
    $event = new UserEvent($photo->getUser());
    $this->eventDispatcher->dispatch($event::PHOTO_ADDED, $event);
}

0 个答案:

没有答案