2

技术背景: Symfony 4.4,liipimagine 包。

我正在开发一个网络应用程序,实际上主要用于智能手机。在我的应用程序中,用户可以为他的个人资料上传图片。

基本上,我的上传工作正常。我唯一的问题是当人们想从他们的 iphone 上传图片时:我无法渲染 .heic 文件,因为它是新格式。

我希望我可以转换 heic 文件,或者如果有办法显示它..

这是我现有的代码:

控制器:

/** @var FormInterface $form */
$form = $this->createForm(UsersType::class, $currentUser);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

    $uploadedFile = $form['imageFilename']->getData();

    if ($uploadedFile) {

        $newFilename = $uploaderHelper->uploadUserImage($uploadedFile, $currentUser);
        $currentUser->setImageFilename($newFilename);

        $entityManager->persist($currentUser);
        $entityManager->flush();
    }

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

类上传助手:

class UploaderHelper
{
    const USER_IMAGE = 'user_image';

    /** @var string $uploadsPath */
    private $uploadsPath;

    /**
     * @var RequestStackContext
     */
    private $requestStackContext;

    /**
     * UploaderHelper constructor.
     * @param string $uploadsPath
     * @param RequestStackContext $requestStackContext
     */
    public function __construct(string $uploadsPath, RequestStackContext $requestStackContext)
    {
        $this->uploadsPath          = $uploadsPath;
        $this->requestStackContext  = $requestStackContext;
    }

    /**
     * @param UploadedFile $uploadedFile
     * @param Users $user
     * @return string
     */
    public function uploadUserImage(UploadedFile $uploadedFile, Users $user): string
    {
        /** @var string $destination */
        $destination =  $this->uploadsPath . '/' . self::USER_IMAGE;

        /** @var string $originalFilename */
        $originalFilename = pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_FILENAME);

        if ($uploadedFile->guessExtension() == 'heic') {

            // We need to convert the image

        }

        /** @var string $newFilename */
        $newFilename = strtolower($user->getName()).'_'.strtolower($user->getSurname()).'-'.uniqid().'.'.$uploadedFile->guessExtension();

        $uploadedFile->move($destination, $newFilename);
//        $this->correctImageOrientation($destination);

        return $newFilename;
    }

    /**
     * @param string $path
     * @return string
     */
    public function getPublicPath(string $path): string
    {
        return $this->requestStackContext->getBasePath() . '/uploads/' . $path;
    }

配置liip:

liip_imagine:
    # valid drivers options include "gd" or "gmagick" or "imagick"
    driver: "imagick"
    filter_sets:
        squared_thumbnail_small:
            filters:
                thumbnail:
                    size: [200, 200]

在视图中显示:

{% if currentUser.imageFilename %}

<img src="{{ uploaded_asset(currentUser.imagePath)|imagine_filter('squared_thumbnail_small') }}" alt="{{ currentUser.name }} {{ currentUser.surname }}">#}

{% endif %}
4

2 回答 2

0

似乎您可以在不转换文件用于VichUploaderBundle上传文件的情况下处理此问题https://symfony.com/doc/current/bundles/EasyAdminBundle/integration/vichuploaderbundle.html并且提到您可以验证链接中提到的文件这个问题在你的实体类中。

/**
 * @param ExecutionContextInterface $context
 */
public function validate(ExecutionContextInterface $context)
{
    if (! in_array($this->file->getMimeType(), array(
        'image/jpeg',
        'image/gif',
        'image/png',
        'image/heif',
        'image/heic',
        'image/heif-sequence',
        'image/heic-sequence',
    ))) {
        $context
            ->buildViolation('Wrong file type (mp4,mov,avi)')
            ->atPath('fileName')
            ->addViolation()
        ;
    }
} 

如何将 mimeType Assert 与 VichUploader 一起使用?

希望它可以帮助你。

于 2020-01-16T12:03:50.883 回答
0

似乎帮助正在路上。

Imagick 现在支持转换 HEIC。这个功能正在进入想象中。

于 2021-10-31T09:54:32.350 回答