Symfony | liipimaginebundle : display or convert a heic file

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

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *