Installation
composer require c975l/services-bundle
Ensure your Laravel project is using Laravel 5.4+ (as the bundle is Symfony-based, but Laravel 5.4+ supports Symfony bundles via illuminate/foundation).
Register the Bundle
Add the bundle to config/app.php under the providers array:
'providers' => [
// ...
c975L\ServicesBundle\c975LServicesBundle::class,
],
First Use Case: Image Resizing
Inject ServiceImageInterface into a controller or service:
use c975L\ServicesBundle\Service\ServiceImageInterface;
public function resizeImage(ServiceImageInterface $imageService) {
$imageService->resize(
$file, // Uploaded file (e.g., from `request()->file('image')`)
'uploads', // Target folder
'resized.jpg', // Output filename
'jpg', // Format
400, // Final height
75, // Compression (0-100)
false, // Square crop?
null // Optional stamp image
);
}
src/Service/ – Core service implementations (e.g., ServiceImage, ServiceTranslation).Resources/translations/ – Predefined translations (if leveraging the bundle’s i18n features).ServiceImageInterface) for testability and flexibility.
public function __construct(ServiceImageInterface $imageService) {
$this->imageService = $imageService;
}
AppServiceProvider if extending functionality:
$this->app->bind(ServiceImageInterface::class, function ($app) {
return new CustomImageService(); // Your wrapper
});
Image Processing
resize() for dynamic thumbnails.$stamp parameter.isValidImage() before processing.Translations
.po/.mo files to Resources/translations/ and clear Laravel’s cache:
php artisan view:clear
php artisan config:clear
Shell Scripts
.sh scripts (e.g., for deployment). Use them via Laravel’s Artisan::call():
Artisan::call('vendor:bin/services-bundle/script.sh', ['arg1' => 'value']);
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->put('path/resized.jpg', $imageData);
dispatch(new ResizeImageJob($file, $params));
$request->validate(['image' => 'required|image|mimes:jpeg,png']);
Symfony vs. Laravel Compatibility
Kernel and Container. In Laravel, ensure:
illuminate/foundation is up-to-date.Container calls; use Laravel’s DI instead.Translation Overrides
.po files. Solution:
services::key).trans() helper with the services namespace:
trans('services::message.key');
File Path Handling
$folder in resize(). Tip:
storage_path() or public_path():
$folder = storage_path('app/uploads');
Image Format Limitations
resize() method defaults to jpg. For lossless formats (e.g., webp), ensure:
imagick PHP extension is installed.$format (e.g., 'webp').config/app.php and dependencies are installed..po file exists in Resources/translations/ and the locale is loaded.php -m | grep -E 'gd|imagick'
Custom Services
ServiceImageInterface) and bind them in AppServiceProvider:
$this->app->extend(ServiceImageInterface::class, function ($service) {
return new CustomImageService($service);
});
Adding Translations
.po files in Resources/translations/ and update the bundle’s Resources/config/translations.php to include them:
return [
'en' => 'path/to/en.po',
'fr' => 'path/to/fr.po',
];
Shell Script Integration
Process facade:
use Symfony\Component\Process\Process;
$process = new Process(['bash', 'vendor/bin/services-bundle/script.sh']);
$process->run();
app('translator')->setFallback(false);
collect() to process multiple images:
$files->each(function ($file) use ($imageService) {
$imageService->resize($file, 'uploads', $file->hashName());
});
How can I help you explore Laravel packages today?