PHP code example of think.studio / laravel-simple-image-manager

1. Go to this page and download the library: Download think.studio/laravel-simple-image-manager 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/ */

    

think.studio / laravel-simple-image-manager example snippets


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();

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

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

class User //...
{
    //...
    use HasAvatar;
    // ...

    protected string $avatarManagerDriver = 'avatars';
}

if ($request->hasFile('avatar')) {
    $user->avatar = $user->avatarUpload(
        $request->file('avatar'), 
        null /* or some name */
    );
}
$user->save();

$url = $user->avatarUrl();

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

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

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()),
bash
php artisan vendor:publish --provider="SimpleImageManager\ServiceProvider" --tag="config"