think.studio/laravel-simple-image-manager

Simple package to save multiple images formats when upload an image.

3.4.0 2023-07-15 11:15 UTC

This package is auto-updated.

Last update: 2024-11-15 14:07:26 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Simple package to save multiple images formats when upload an image. For example:

  • myimage.png (original)
  • myimage-small.png
  • myimage-thumb.png
  • myimage-100x100.png

Package works as wrapper of spatie/image package. So when you create formats you can use all methods of this package. All configuration options description you can find in config file

Installation

Install the package via composer:

composer require think.studio/laravel-simple-image-manager

You can publish the config file with:

php artisan vendor:publish --provider="SimpleImageManager\ServiceProvider" --tag="config"

Usage

Upload file example:

if ($request->hasFile('avatar')) {
    $user->avatar = SimpleImageManager::driver('avatars')
                                      ->upload(
                                          $request->file('avatar'), 
                                          null /* or some name */,
                                          $user->avatar /* old file name to replace it */
                                       );
}
$user->save();

Other methods:

// Get file url
$url = SimpleImageManager::driver('avatars')->url((string) $user->avatar, $format);
// Get file path
$path = SimpleImageManager::driver('avatars')->path((string) $user->avatar, $format);
// Delete specific format
$isDeleted = SimpleImageManager::driver('avatars')->deleteSingle((string) $user->avatar, $format);
// Delete all files
if($isDeletedAll = SimpleImageManager::driver('avatars')->delete((string) $user->avatar)) {
    $user->avatar = null;
}

Advanced usage

Create specific trait

  1. Create trait
// app/Models/Traits/HasAvatar.php
namespace App\Models\Traits;

use Illuminate\Http\UploadedFile;
use SimpleImageManager\Facades\SimpleImageManager;

trait HasAvatar {
    
    /**
     * Driver name.
     *
     * @return string|null
     */
    public function avatarManagerDriver(): ?string {
        if ( property_exists( $this, 'avatarManagerDriver' ) ) {
            return $this->avatarManagerDriver;
        }

        return null;
    }

    /**
     * Field key name.
     *
     * @return string
     */
    public function avatarKey(): string {
        if ( property_exists( $this, 'avatarKey' ) ) {
            return $this->avatarKey;
        }

        return 'avatar';
    }

    /**
     * Image manager.
     *
     * @return \SimpleImageManager\Contracts\ImageManagerInterface
     */
    public function avatarManager(): \SimpleImageManager\Contracts\ImageManagerInterface {
        return SimpleImageManager::driver( $this->avatarManagerDriver() );
    }

    /**
     * Upload file to storage.
     *
     * @param UploadedFile $image
     * @param string|null $filename
     *
     * @return string|null Storage file name.
     */
    public function avatarUpload( UploadedFile $image, ?string $filename = null ): ?string {
        return $this->avatarManager()
                    ->upload( $image, $filename, $this->{$this->avatarKey()} );
    }

    /**
     * Delete file from storage.
     *
     * @return bool Storage file name.
     */
    public function avatarDelete( bool $updateField = true, bool $persist = false ): bool {
        $result = $this->avatarManager()->delete( $this->{$this->avatarKey()} );
        if ( $result && $updateField ) {
            $this->{$this->avatarKey()} = null;
            if ( $persist ) {
                $this->save();
            }
        }

        return $result;
    }

    /**
     * Full path to file.
     *
     * @param string|null $format
     *
     * @return string|null
     */
    public function avatarPath( ?string $format = null ): ?string {
        return $this->avatarManager()->path( (string) $this->{$this->avatarKey()}, $format );
    }

    /**
     * File url.
     *
     * @param string|null $format
     *
     * @return string|null
     */
    public function avatarUrl( ?string $format = null ): ?string {
        return $this->avatarManager()->url( (string) $this->{$this->avatarKey()}, $format );
    }
}
  1. Use trail
class User //...
{
    //...
    use HasAvatar;
    // ...

    protected string $avatarManagerDriver = 'avatars';
}
  1. Manipulate using trait
if ($request->hasFile('avatar')) {
    $user->avatar = $user->avatarUpload(
        $request->file('avatar'), 
        null /* or some name */
    );
}
$user->save();

$url = $user->avatarUrl();

Use internal trait

use Illuminate\Database\Eloquent\Model;
use \SimpleImageManager\Eloquent\HasThinkImage;
use \SimpleImageManager\Eloquent\ThinkImage;

class Author extends Model
{
    use HasThinkImage;

    /** @inerhitDoc  */
    public function thinkImagesMap(): array {
        return [
            'avatar' => (new ThinkImage('avatars', $this->avatar))
                ->setDefaultUrl(url('/img/default.svg')),
            'image' => 'feature-images',
        ];
    }

    public function avatarImage(): ThinkImage {
        return $this->thinkImage('avatar');
    }

    public function featureImage(): ThinkImage {
        return $this->thinkImage('image');
    }
}

Then you can use it:

/** @var Author Author */
$author->featureImage()->img();
$author->avatarImage()->url();
$author->thinkImage('avatar')->path();

Usage with laravel-nova

Avatar::make('Avatar', 'avatar')
    ->store(
      fn ($request, Model $model, $attribute, $requestAttribute, $storageDisk, $storageDir) => fn () => $model->fill([
          $attribute => $model->avatarUpload($request->file($requestAttribute), $model->uuid),
      ])->save()
    )->maxWidth(100)
    ->preview(fn ($value, $storageDisk, $model) => $model->avatarUrl('small'))
    ->thumbnail(fn ($value, $storageDisk, $model) => $model->avatarUrl('small'))
    ->delete(fn ($request, $model, $storageDisk, $storagePath) => $model->avatarDelete()),

Credits

  • Think Studio