2

Technical context: Symfony 4.4, liipimagine bundle.

I'm working on the development of a web-app, mostly used with a smartphone indeed. In my application, the user is able to upload a picture for his profile.

Basically, my upload is woking perfectly fine. My only issue is when people want to upload a picture from their iphone: I can't render a .heic file, since it's the new format.

I wish I could convert the heic file, or if there is a way to display it..

Here is my existing code:

Controller:

/** @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 UploadHelper:

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;
    }

Configuration liip:

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

Display in the view :

{% if currentUser.imageFilename %}

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

{% endif %}
Chris_1985
  • 71
  • 9

2 Answers2

0

It seems that you can handle this without converting your files use VichUploaderBundle to upload files https://symfony.com/doc/current/bundles/EasyAdminBundle/integration/vichuploaderbundle.html and it's mentioned that you can validate you files mentioned in the link of this question inside your entity class.

/**
 * @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()
        ;
    }
} 

How to use mimeType Assert with VichUploader?

Hope it helps you.

Kamel Mili
  • 1,374
  • 3
  • 19
  • 43
0

Seems like help is on the way.

Imagick now supports converting HEIC. This feature is making its way into imagine.

Baishu
  • 1,448
  • 12
  • 12