PHP code example of daycode / stup-images

1. Go to this page and download the library: Download daycode/stup-images 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/ */

    

daycode / stup-images example snippets


$path = StupImage::from($request->file('avatar'))->cover(400, 400)->toWebp()->store()->path;

use Daycode\StupImage\Facades\StupImage;

public function update(Request $request)
{
    $request->validate(['avatar' => ['  ->cover(400, 400)
        ->toWebp(quality: 80)
        ->store();

    $request->user()->update(['avatar' => $image->path]);

    return back();
}

$image = StupImage::from($request->file('photo'))->store();

$image->path;      // images/01j9zk3v7c8e2x4m6n8p0q2r4s.jpg
$image->filename;  // 01j9zk3v7c8e2x4m6n8p0q2r4s.jpg
$image->disk;      // public
$image->mimeType;  // image/jpeg
$image->size;      // bytes
$image->width;     // pixels
$image->height;    // pixels

$image->url();
$image->temporaryUrl(now()->addMinutes(5)); // on disks that support it (e.g. S3)
$image->exists();
$image->delete();

(string) $image;   // the path
$image->toArray(); // also JSON serializable

StupImage::from($file)->cover(400, 400);   // crop and resize to exactly 400×400
StupImage::from($file)->scale(width: 1200); // resize, keeping the aspect ratio
StupImage::from($file)->contain(800, 800); // fit inside 800×800 and pad the rest
StupImage::from($file)->resize(800, 600);  // exactly 800×600, ignores the aspect ratio

StupImage::from($file)
    ->when($request->boolean('square'), fn ($upload) => $upload->cover(600, 600))
    ->store();

->toWebp(quality: 80)
->toAvif(quality: 60)   // needs a GD/Imagick build with AVIF support
->toJpeg(quality: 85)
->toPng()
->format('webp')        // same as toWebp()
->quality(75)

StupImage::from($file)
    ->disk('s3')                 // default: config('stup-image.disk') ?? the default filesystem disk
    ->directory('users/avatars') // default: config('stup-image.directory')
    ->name('john-doe')           // optional; replaces the naming strategy and overwrites an existing file
    ->visibility('public')       // optional; default: the disk's own setting
    ->store();

// Choosing the disk on the facade works too:
StupImage::disk('s3')->from($file)->store();

$image = StupImage::replace($user->avatar, $request->file('avatar'))
    ->directory('avatars')
    ->store();

$images = StupImage::many($request->file('gallery'))
    ->directory('gallery')
    ->scale(width: 1200)
    ->store(); // Collection<StoredImage>

$post->photos()->createMany($images->map(fn ($image) => ['path' => $image->path]));

StupImage::delete($user->avatar);             // default disk; also deletes its variants
StupImage::disk('s3')->delete($user->avatar);

StupImage::url($user->avatar);                // null when the path is null/empty
StupImage::url($user->avatar, 'thumbnail');   // URL of a variant
StupImage::exists($user->avatar);

'presets' => [
    'thumbnail' => ['cover' => [150, 150], 'format' => 'webp', 'quality' => 80],
    'medium'    => ['scale' => [800, null]],
],

StupImage::from($file)->preset('thumbnail')->store();

$image = StupImage::from($file)
    ->directory('avatars')
    ->variants(['thumbnail', 'medium'])
    ->store();

$image->path;                          // avatars/01j9zk....jpg
$image->variant('thumbnail')->path;    // avatars/01j9zk.../thumbnail.webp
$image->variant('medium')->url();

// Later, from the stored path only:
StupImage::url($user->avatar, 'thumbnail');
StupImage::variantPath($user->avatar, 'thumbnail');

StupImage::from($file)->variants(['thumbnail', 'medium'])->queue()->store();
StupImage::from($file)->variants(['thumbnail'])->queue('images', 'redis')->store(); // queue, connection

use Daycode\StupImage\Concerns\HasStupImages;

class User extends Model
{
    use HasStupImages;

    protected array $stupImages = [
        'avatar' => 'avatars', // the directory
        'cover'  => ['directory' => 'covers', 'disk' => 's3', 'preset' => 'medium', 'variants' => ['thumbnail']],
    ];
}

$user->update(['avatar' => $request->file('avatar')]); // stores the image, then deletes the old one after saving
$user->update(['avatar' => null]);                     // deletes the image after saving

$user->avatarUrl();             // or $user->stupImageUrl('avatar')
$user->coverUrl('thumbnail');   // URL of a variant

$image = $request->file('avatar')->storeImage('avatars');       // directory
$image = $request->file('avatar')->storeImage('avatars', 's3'); // directory, disk

try {
    $image = StupImage::from($request->file('avatar'))->store();
} catch (DisallowedMimeTypeException|ImageTooLargeException $e) {
    return back()->withErrors(['avatar' => $e->getMessage()]);
}

use Daycode\StupImage\Facades\StupImage;

it('updates the avatar', function () {
    $fake = StupImage::fake();
    $user = User::factory()->create(['avatar' => 'avatars/old.jpg']);

    $this->actingAs($user)->put('/profile', [
        'avatar' => UploadedFile::fake()->image('me.jpg', 800, 800),
    ]);

    $fake->assertStored($user->fresh()->avatar)
        ->assertStored(fn ($image) => $image->width === 400)
        ->assertStoredCount(1)
        ->assertDeleted('avatars/old.jpg');
});

return [
    'disk' => env('STUP_IMAGE_DISK'),               // null = filesystems.default
    'directory' => 'images',
    'driver' => env('STUP_IMAGE_DRIVER', 'gd'),     // gd | imagick
    'visibility' => env('STUP_IMAGE_VISIBILITY'),   // null = the disk's own setting
    'naming' => 'ulid',                             // ulid | uuid | hash | original | a Namer class
    'allowed_mime_types' => ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif'],
    'max_size' => 10 * 1024,                        // KB, null = no limit
    'max_width' => null,                            // px, null = no limit
    'max_height' => null,                           // px, null = no limit
    'default_format' => null,                       // null = keep the original format
    'quality' => 85,
    'presets' => [/* ... */],
];
bash
php artisan stup-image:install
bash
php artisan storage:link