Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Avatar Bundle Laravel Package

dwr/avatar-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require dwr/avatar-bundle
    

    Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):

    // Symfony 4+
    Dwr\AvatarBundle\DwrAvatarBundle::class => ['all' => true],
    
  2. Configuration Add to config/packages/dwr_avatar.yaml (Symfony 4+) or app/config/config.yml (Symfony 2/3):

    dwr_avatar:
        default_avatar: '%kernel.project_dir%/path/to/default.png'
        upload_directory: '%kernel.project_dir%/var/avatars'
        allowed_extensions: ['jpg', 'jpeg', 'png', 'gif']
        max_width: 200
        max_height: 200
    
  3. First Use Case Generate an avatar from a user-uploaded file in a controller:

    use Dwr\AvatarBundle\Generator\AvatarGenerator;
    
    public function uploadAvatar(Request $request)
    {
        $file = $request->files->get('avatar');
        $generator = $this->get('dwr_avatar.generator');
        $avatarPath = $generator->generate($file, 'user_' . $this->getUser()->getId());
    
        return new Response('Avatar saved to: ' . $avatarPath);
    }
    
  4. Twig Integration Use the avatar Twig filter in templates:

    <img src="{{ avatar(user.avatarPath, 50, 50) }}" alt="User Avatar">
    

Implementation Patterns

Core Workflows

  1. Avatar Generation

    • From Uploaded Files:
      $generator = $this->get('dwr_avatar.generator');
      $path = $generator->generate($uploadedFile, 'filename_prefix');
      
    • From URLs: Use the Dwr\AvatarBundle\Generator\UrlAvatarGenerator service:
      $urlGenerator = $this->get('dwr_avatar.url_generator');
      $path = $urlGenerator->generate('https://example.com/image.jpg', 'filename_prefix');
      
  2. Dynamic Resizing Override default dimensions per request:

    $path = $generator->generate($file, 'filename', 100, 100);
    
  3. Fallback Avatars Configure a default avatar in config and handle missing files gracefully:

    <img src="{{ avatar(user.avatarPath, 50, 50, 'default') }}">
    
  4. Batch Processing Process multiple avatars in a loop (e.g., for bulk user imports):

    foreach ($users as $user) {
        $file = $user->getAvatarFile();
        $generator->generate($file, 'user_' . $user->getId());
    }
    

Integration Tips

  1. Entity Integration Add a avatarPath property to your User entity and update it post-upload:

    public function setAvatarFile($file)
    {
        $generator = $this->get('dwr_avatar.generator');
        $this->avatarPath = $generator->generate($file, 'user_' . $this->id);
    }
    
  2. Form Handling Use Symfony’s File type in forms:

    $builder->add('avatar', FileType::class, [
        'label' => 'Profile Picture',
        'mapped' => false,
    ]);
    
  3. Cache Control Append a query string to avatar URLs to bypass cache:

    <img src="{{ avatar(user.avatarPath, 50, 50) }}?v={{ user.updatedAt|date('U') }}">
    
  4. Security Restrict avatar uploads to trusted users and validate file types:

    $generator->setAllowedExtensions(['jpg', 'png']); // Override config per request
    

Gotchas and Tips

Pitfalls

  1. GD Extension Requirement Ensure php-gd is installed and enabled. Test with:

    php -m | grep gd
    

    If missing, install via:

    sudo apt-get install php-gd  # Debian/Ubuntu
    sudo yum install php-gd      # CentOS/RHEL
    
  2. File Permissions The upload_directory must be writable by the web server (e.g., www-data or nginx):

    chmod -R 775 var/avatars
    chown -R www-data:www-data var/avatars
    
  3. Image Corruption

    • Cause: Large files or unsupported formats may corrupt avatars.
    • Fix: Validate file sizes and extensions before processing:
      if (!$file->isValid() || !in_array($file->guessExtension(), $allowedExtensions)) {
          throw new \RuntimeException('Invalid file');
      }
      
  4. Symfony 4+ Autowiring The bundle predates Symfony 4’s autowiring. Explicitly type-hint services:

    public function __construct(
        private AvatarGenerator $avatarGenerator,
        private UrlAvatarGenerator $urlAvatarGenerator
    ) {}
    

Debugging

  1. Log Generation Errors Enable debug mode and check logs for GD errors:

    # config/packages/monolog.yaml
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
    
  2. Verify Generated Paths Log the output path to confirm files are saved:

    $path = $generator->generate($file, 'test');
    \Log::debug('Avatar saved to:', ['path' => $path]);
    
  3. Check File Existence Debug missing avatars with:

    if (!file_exists($avatarPath)) {
        throw new \RuntimeException("Avatar not found at: $avatarPath");
    }
    

Extension Points

  1. Custom Avatar Styles Extend the Dwr\AvatarBundle\Generator\AvatarGenerator to add filters (e.g., grayscale):

    use GdImage;
    
    public function generate($file, $filename, $width = null, $height = null)
    {
        $image = $this->createImage($file);
        imagefilter($image, IMG_FILTER_GRAYSCALE); // Custom filter
        return $this->saveImage($image, $filename, $width, $height);
    }
    
  2. Dynamic Default Avatars Override the default avatar per user (e.g., based on gender):

    public function getDefaultAvatarPath($user)
    {
        return $user->isMale()
            ? $this->container->getParameter('male_default_avatar')
            : $this->container->getParameter('female_default_avatar');
    }
    
  3. Cloud Storage Integration Replace local file saving with AWS S3 or similar:

    public function saveImage($image, $filename, $width, $height)
    {
        $path = $this->generateRemotePath($filename);
        $this->s3Client->putObject([
            'Bucket' => 'avatars-bucket',
            'Key'    => $path,
            'Body'   => $this->imageToBytes($image),
        ]);
        return $path;
    }
    

Configuration Quirks

  1. Relative vs. Absolute Paths Use %kernel.project_dir% for portability:

    upload_directory: '%kernel.project_dir%/var/avatars'
    

    Avoid hardcoding paths like /var/www/avatars.

  2. Case Sensitivity allowed_extensions is case-sensitive. Use lowercase:

    allowed_extensions: ['jpg', 'png']  # Not ['JPG', 'PNG']
    
  3. Legacy Symfony 2 Compatibility For Symfony 2, ensure the bundle is loaded after the FrameworkBundle in AppKernel.php:

    new Dwr\AvatarBundle\DwrAvatarBundle(),
    
  4. Environment-Specific Configs Override settings per environment (e.g., disable uploads in test):

    # config/packages/dwr_avatar_test.yaml
    dwr_avatar:
        upload_directory: null  # Disable uploads in tests
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium