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