migunov / laravel-services
Little Useful Services for Laravel
Installs: 503
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/migunov/laravel-services
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- illuminate/filesystem: ^12.0
- illuminate/http: ^12.0
- illuminate/support: ^12.0
- intervention/image: ^3.11
Requires (Dev)
- laravel/pint: ^1.24.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^10.6
- peckphp/peck: ^0.1.3
- pestphp/pest: ^4.1.0
- pestphp/pest-plugin-type-coverage: ^4.0.2
- phpstan/phpstan: ^2.1.26
- rector/rector: ^2.1.7
- symfony/var-dumper: ^7.3.3
README
Little Useful Services for Laravel
A package of useful utilities and services for Laravel applications, including image processing, HTTP requests, meta tags extraction, and standardized API responses.
Requirements
- PHP 8.3+
- Laravel 11.0+ or 12.0+
- Intervention Image 3.11+
Installation
composer require migunov/services
Overview
The package provides the following main components:
- Helper - universal utilities for working with data, files, and HTTP
- MetaTagsService - extracting metadata from HTML pages
- ResponseService - standardized JSON responses for APIs
- ImageService - comprehensive image processing
- Traits - convenient static methods for common operations
Quick Start
HTTP Requests
use Migunov\Services\Helper;
$response = Helper::httpClient()->get('https://api.example.com/data');
if ($response->successful()) {
$data = $response->json();
}
Image Processing
use Migunov\Services\ImageService;
// Resize image
ImageService::resize('/path/to/image.jpg', 800, 600, '/path/to/resized.jpg');
// Create cover image
ImageService::cover('/path/to/image.jpg', 1200, 600, '/path/to/cover.jpg');
Meta Tags Extraction
use Migunov\Services\Helper;
$meta = Helper::getMetaTags('https://example.com');
// Returns array with title, description, og:image and other meta tags
Standardized API Responses
use Migunov\Services\ResponseService;
return ResponseService::json(
['users' => $users],
['page' => 1, 'limit' => 10],
200
);
API Reference
Helper - Universal Utilities
httpClient(?string $userAgent = null): PendingRequest
Returns a configured HTTP client with default User-Agent.
$client = Helper::httpClient('CustomBot/1.0');
$response = $client->get('https://api.example.com');
sanitizeData(array $data): array
Cleans string values in array from whitespace.
$input = ['name' => ' John Doe ', 'age' => 30];
$sanitized = Helper::sanitizeData($input);
// ['name' => 'John Doe', 'age' => 30]
stringToArray(string $value, string $separator = ','): array
Splits string into array with whitespace cleanup.
$tags = Helper::stringToArray('tag1, tag2, tag3');
// ['tag1', 'tag2', 'tag3']
fileExtension(string $path): string
Extracts file extension from path.
$ext = Helper::fileExtension('/path/to/image.jpg?v=1');
// '.jpg'
imageExtensionFromMimeType(string $contentType): string
Determines image extension from MIME type.
$ext = Helper::imageExtensionFromMimeType('image/jpeg');
// '.jpeg'
host(string $url, bool $onlyDomain = true): ?string
Extracts host from URL.
$host = Helper::host('https://example.com:8080/path');
// 'example.com:8080'
timeFormat(int $seconds): string
Formats seconds into human-readable time format.
$time = Helper::timeFormat(3661);
// '1 hr. 1 min.'
downloadFile(string $url, string $path): bool
Downloads file from URL to specified path.
$success = Helper::downloadFile('https://example.com/image.jpg', '/path/to/save.jpg');
getMetaTags(string $urlOrContent, bool $noThrow = true): array
Extracts meta tags from URL or HTML content.
$meta = Helper::getMetaTags('https://example.com');
// ['title' => 'Example', 'description' => '...', 'og:image' => '...']
socialName(string $url): string
Determines social network name from URL.
$network = Helper::socialName('https://twitter.com/user');
// 'x-twitter'
MetaTagsService
get(string $urlOrContent, bool $noThrow = true): array
Main method for extracting metadata from HTML.
$service = new MetaTagsService();
$meta = $service->get('https://example.com');
ResponseService
json(array $data, array $params = [], int $status = 200): JsonResponse
Creates standardized JSON response with metadata.
return ResponseService::json(
['users' => $users],
['page' => 1, 'total' => 100]
);
Response will have format:
{
"meta": {
"timestamp": 1234567890,
"params": {
"page": 1,
"total": 100
}
},
"data": {
"users": [...]
}
}
ImageService - Image Processing
cover(string $path, int $width = 1200, int $height = 600, ?string $targetPath = null): void
Creates cover image with cropping to specified dimensions.
ImageService::cover('/path/to/image.jpg', 1200, 600, '/path/to/cover.jpg');
getManager(string $driver = 'gd'): ImageManager
Returns Intervention image manager with specified driver.
$manager = ImageService::getManager('imagick');
$image = $manager->make('/path/to/image.jpg');
isImageFile(string $path): bool
Checks if file is an image by extension.
$isImage = ImageService::isImageFile('/path/to/file.jpg');
saveImageFromUrlPage(string $url, string $path, array $params): ?array
Saves main image from web page and creates additional sizes.
$params = [
[
'width' => 1200,
'height' => 600,
],
[
'postfix' => '.preview.jpg',
'width' => 400,
'height' => 400,
],
[
'postfix' => '.mini.jpg',
'width' => 64,
'height' => 64,
]
];
$savedPaths = ImageService::saveImageFromUrlPage(
'https://example.com/page',
'/storage/images',
$params
);
Traits - Static Methods
WithImageResize
use Migunov\Services\Traits\WithImageResize;
// Resize image
WithImageResize::resize('/path/to/image.jpg', 800, 600, '/path/to/resized.jpg');
// Resize down maintaining aspect ratio
WithImageResize::resizeDown('/path/to/large.jpg', 1200, 800);
WithImageCrop
use Migunov\Services\Traits\WithImageCrop;
use Intervention\Image\ImageManager;
$image = ImageManager::gd()->make('/path/to/image.jpg');
$cropped = WithImageCrop::cropNoSave($image, 800, 400, 100, 50);
Use Cases
Download and Process Image from Web Page
use Migunov\Services\Helper;
use Migunov\Services\ImageService;
// 1. Get page meta tags
$meta = Helper::getMetaTags('https://example.com/article');
$ogImage = $meta['og:image'] ?? null;
if ($ogImage) {
// 2. Save image with multiple sizes
$paths = ImageService::saveImageFromUrlPage(
$ogImage,
storage_path('app/public/images'),
[
['width' => 1200, 'height' => 800],
['postfix' => '.medium.jpg', 'width' => 600, 'height' => 400],
['postfix' => '.thumb.jpg', 'width' => 150, 'height' => 150],
]
);
}
Standardized API Controller
use Migunov\Services\ResponseService;
class UserController extends Controller
{
public function index()
{
$users = User::paginate(10);
return ResponseService::json(
['users' => $users->items()],
[
'page' => $users->currentPage(),
'limit' => $users->perPage(),
'total' => $users->total()
]
);
}
}
Testing
The package uses Pest for testing and PHPStan for static analysis.
# Run tests
composer test
# Static analysis
composer analyse
License
MIT License
Author
Dmitri Migunov dmitri.migunov@gmail.com"