Stup Image is a powerful Laravel package for storing and updating images with clean, readable code. Integrated with the Intervention Image library, it provides an elegant solution for handling file uploads with advanced features like automatic resizing, configurable filename hashing, and flexible storage options.
- π Simple and intuitive API for file uploads
- πΌοΈ Automatic image resizing on upload
- π Configurable filename hashing for security
- π Flexible storage disk configuration (local, S3, etc.)
- π Sync upload (replace old file automatically)
- π¦ Multiple file uploads support
- ποΈ Easy file deletion
- βοΈ Customizable configuration
- β File extension validation
composer require daycode/stup-imagesphp artisan vendor:publish --tag=stup-imageThis will create a config/stup-image.php file where you can customize the package behavior.
Make sure your config/filesystems.php has the desired disk configured. By default, the package uses the FILESYSTEM_DISK from your .env file.
FILESYSTEM_DISK=publicphp artisan storage:linkphp artisan optimize:clearAfter publishing the config file, you can customize these options in config/stup-image.php:
return [
// Hash filenames using MD5 + timestamp for security
'hash_filename' => true,
// Allowed file extensions. Use ['*'] for all, or specify: ['jpg', 'png', 'gif']
'allowed_extensions' => ['*'],
];Add the Stupable trait to your controller or any class:
use Daycode\StupImage\Stupable;
class UserController extends Controller
{
use Stupable;
// Your methods here...
}Upload a single file to storage:
public function store(Request $request)
{
$filename = $this->uploadFile(
file: $request->file('avatar'),
path: 'images/avatars'
);
User::create([
'name' => $request->name,
'avatar' => $filename,
]);
return redirect()->back()->with('success', 'User created successfully!');
}Automatically resize images during upload:
public function store(Request $request)
{
// Resize to 800x600 pixels
$filename = $this->uploadFile(
file: $request->file('thumbnail'),
path: 'images/thumbnails',
resize: [800, 600]
);
Post::create([
'title' => $request->title,
'thumbnail' => $filename,
]);
}Upload a new file and automatically delete the old one:
public function update(Request $request, User $user)
{
$filename = $user->avatar;
if ($request->hasFile('avatar')) {
$filename = $this->syncUploadFile(
file: $request->file('avatar'),
oldFileName: $user->avatar,
path: 'images/avatars'
);
}
$user->update([
'name' => $request->name,
'avatar' => $filename,
]);
return redirect()->back()->with('success', 'User updated successfully!');
}Upload multiple files at once:
public function store(Request $request)
{
$filenames = $this->uploadMultipleFiles(
files: $request->file('gallery'),
path: 'images/gallery'
);
foreach ($filenames as $filename) {
Gallery::create([
'image' => $filename,
]);
}
return redirect()->back()->with('success', 'Gallery uploaded successfully!');
}Delete a file from storage:
public function destroy(User $user)
{
$this->deleteFile(
fileName: $user->avatar,
path: 'images/avatars'
);
$user->delete();
return redirect()->back()->with('success', 'User deleted successfully!');
}Upload a single file to storage.
Parameters:
$file- The uploaded file instance$path- Storage path (relative to your configured disk)$resize- Optional. Array with[width, height]for automatic resizing
Returns: Filename (string) or throws UploadException
Upload a new file and delete the old one.
Parameters:
$file- The new uploaded file$oldFileName- The old filename to delete (nullable)$path- Storage path$resize- Optional. Array with[width, height]for automatic resizing
Returns: New filename (string)
Upload multiple files at once.
Parameters:
$files- Array of uploaded files$path- Storage path$resize- Optional. Array with[width, height]for automatic resizing
Returns: Array of filenames or throws UploadException
Delete a file from storage.
Parameters:
$fileName- The filename to delete$path- Storage path
The package respects your FILESYSTEM_DISK environment variable. To use different disks:
Local Storage:
FILESYSTEM_DISK=publicAmazon S3:
FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-bucketDigitalOcean Spaces, Cloudinary, etc.:
Configure your disk in config/filesystems.php and set FILESYSTEM_DISK accordingly.
Edit config/stup-image.php:
'allowed_extensions' => ['jpg', 'jpeg', 'png', 'gif', 'webp'],If you want to keep original filenames:
'hash_filename' => false,- Filenames are hashed by default using MD5 + timestamp to prevent conflicts and enhance security
- File extension validation prevents unauthorized file types
- Uses Laravel's Storage facade for secure file handling
use Daycode\StupImage\Stupable;
class ProductController extends Controller
{
use Stupable;
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'image' => 'required|image|max:2048',
]);
$filename = $this->uploadFile(
file: $request->file('image'),
path: 'products',
resize: [1200, 800]
);
Product::create([
'name' => $request->name,
'image' => $filename,
]);
return redirect()->route('products.index');
}
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'image' => 'nullable|image|max:2048',
]);
$filename = $product->image;
if ($request->hasFile('image')) {
$filename = $this->syncUploadFile(
file: $request->file('image'),
oldFileName: $product->image,
path: 'products'
);
}
$product->update([
'name' => $request->name,
'image' => $filename,
]);
return redirect()->route('products.index');
}
public function destroy(Product $product)
{
$this->deleteFile($product->image, 'products');
$product->delete();
return redirect()->route('products.index');
}
}Contributions are welcome! Please feel free to submit a Pull Request.
This package is open-sourced software licensed under the MIT license.
- Author: Wirandra Alaya (dayCod)
- Intervention Image: Intervention
If you find this package helpful, please consider giving it a β on GitHub and Packagist!
For issues or questions, please use the GitHub Issues page.