PHP code example of santeacademie / super-uploader-bundle

1. Go to this page and download the library: Download santeacademie/super-uploader-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

santeacademie / super-uploader-bundle example snippets




namespace App\Foo\Entity;

use Santeacademie\SuperUploaderBundle\Asset\AbstractAsset;
use Santeacademie\SuperUploaderBundle\Interface\UploadableInterface;
use Santeacademie\SuperUploaderBundle\Trait\UploadableTrait;
use Santeacademie\SuperUploaderBundle\Annotation\UploadableField;
use Santeacademie\SuperUploaderBundle\Annotation\UploadableKey;

class User implements UploadableInterface
{
    use UploadableTrait;

    /**
     * @UploadableKey
     */
    private string $id;
    private string $firstname;
    private string $lastname;
    private string $email;
    
    /**
     * @UploadableField(name="profile_picture", asset="App\Foo\Asset\ProfilePictureAsset")
     */
    public ?AbstractAsset $profilePicture = null;
    
    public function getEmail(): string 
    {
        return $this->email;
    }



namespace App\Foo\Asset;

use Santeacademie\SuperUploaderBundle\Form\VariantType\ImagickCropVariantType;
use Santeacademie\SuperUploaderBundle\Select\SelectUploadMediaType;
use Santeacademie\SuperUploaderBundle\Asset\AbstractAsset;
use Santeacademie\SuperUploaderBundle\Asset\Variant\PictureVariant;

class ProfilePictureAsset extends AbstractAsset
{

    const VARIANT_PORTRAIT = 'portrait';
    const VARIANT_LANDSCAPE = 'landscape';

    public function getLabel(): string
    {
        return 'Profile picture';
    }

    public function supportedVariants(): array
    {
        return [
            new PictureVariant(
                variantTypeClass: ImagickCropVariantType::class,
                



namespace App\Foo\Form;

use Santeacademie\SuperUploaderBundle\Form\AssetType\AssetType;
use App\Foo\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('firstname', TextType::class)
            ->add('lastname', TextType::class)
            ->add('email', EmailType::class)
            ->add('profile_picture', AssetType::class, [
                'uploadable_entity' => $builder->getData()
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(['data_class' => User::class ]);
    }
}



namespace App\Foo\Controller;

use App\Foo\Entity\User;
use App\Foo\Asset\ProfilePictureAsset;
use Santeacademie\SuperUploaderBundle\Bridge\UploadableEntityBridge;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;

class FooController extends AbstractController 
{

      /**
     * @Route("/foo/{id}", name="foo", methods={"POST"})
     */
    public function foo(Request $request, User $user, UploadableEntityBridge $uploadableEntityBridge) 
    {
        $variantUser = $user->profilePicture->getVariant(ProfilePictureAsset::VARIANT_LANDSCAPE);
        
        $uploadableEntityBridge->manualUpload(
            entity: $user, 
            variant: $variantUser,
            fileOrBinary: $request->files->get('profile_picture')
        );

        return new Response(sprintf('Profile picture uploaded for user %s !', $user->getEmail()));
    }

}

bash
php bin/console assets:install
bash
php bin/console santeacademie:super-uploader:generate:fallbacks
bash
php bin/console santeacademie:super-uploader:generate:dbmap
html
<img src="{{ asset(user.profilePicture.variant('landscape').variantFile) }}" />