首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony _ liipimaginebundle :显示或转换heic文件

Symfony _ liipimaginebundle :显示或转换heic文件
EN

Stack Overflow用户
提问于 2020-01-15 08:50:45
回答 2查看 743关注 0票数 2

技术上下文: Symfony 4.4,liipimagine。

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

基本上,我的上传很好。我唯一的问题是当人们想从iphone上传图片时:我不能呈现.heic文件,因为这是新的格式。

我希望我能转换heic文件,或者如果有方法显示它。

以下是我现有的代码:

控制器:

代码语言:javascript
复制
/** @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');
}

类UploadHelper:

代码语言:javascript
复制
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:

代码语言:javascript
复制
liip_imagine:
    # valid drivers options include "gd" or "gmagick" or "imagick"
    driver: "imagick"
    filter_sets:
        squared_thumbnail_small:
            filters:
                thumbnail:
                    size: [200, 200]

视图中的显示:

代码语言:javascript
复制
{% if currentUser.imageFilename %}

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

{% endif %}
EN

回答 2

Stack Overflow用户

发布于 2020-01-16 12:03:50

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

代码语言:javascript
复制
/**
 * @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断言与VichUploader一起使用?

希望它能帮到你。

票数 0
EN

Stack Overflow用户

发布于 2021-10-31 09:54:32

看来帮助已经在路上了。

想象现在支座转换HEIC。这个特性使正朝着它的方向前进进入了想象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59747804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档