PHP code example of devmahmoudmustafa / laravel-imagekit
1. Go to this page and download the library: Download devmahmoudmustafa/laravel-imagekit 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/ */
devmahmoudmustafa / laravel-imagekit example snippets
return [
// Storage disk (local, public, s3, gcs, azure, etc.)
'disk' => 'public',
// Default image path (relative to disk root)
'default_saved_path' => 'uploads/images',
// Allowed file extensions
'allowed_extensions' => ['jpg', 'jpeg', 'png', 'webp'],
// Maximum file size in KB (null = no limit)
'max_file_size' => null,
// Maximum image dimensions in pixels (null = no limit)
'max_dimensions' => [
'width' => null,
'height' => null,
],
// File naming strategy (default, uuid, hash, timestamp, or callable)
'naming_strategy' => 'default',
// Enable multi-size resizing globally
'enable_multi_size' => false,
// Multi-size options (size names to use when enable_multi_size is true)
'multi_size_options' => ['small', 'medium', 'large'],
// Multi-size dimensions configuration (actual width and height for each size)
'multi_size_dimensions' => [
'small' => ['width' => 300, 'height' => 300],
'medium' => ['width' => 600, 'height' => 600],
'large' => ['width' => 1024, 'height' => 1024],
],
// Default dimensions for single resize
'dimensions' => [
'width' => null,
'height' => null,
],
// Maintain aspect ratio when resizing
'aspectRatio' => true,
// Default compression quality (null = dynamic)
'compression_quality' => null,
// Enable watermark globally
'enable_watermark' => false,
// Watermark settings
'watermark' => [
'image' => 'watermark.png',
'position' => 'bottom-right',
'opacity' => 50,
'x' => 10,
'y' => 10,
'width' => null,
'height' => null,
],
// Watermark storage path (for uploaded watermarks)
'watermark_storage_path' => 'watermarks',
// Return keys - specify which data to return after saving
'return_keys' => ['name'],
];
use DevMahmoudMustafa\ImageKit\Facades\ImageKit;
// Simple upload
$imageName = ImageKit::image($request->file('image'))
->save();
// With resizing
$imageName = ImageKit::image($request->file('image'))
->resize(800, 600)
->save();
// With compression
$imageName = ImageKit::image($request->file('image'))
->compress(85)
->save();
// With watermark
$imageName = ImageKit::image($request->file('image'))
->watermark('watermark.png', 'bottom-right', 50)
->save();
// Save to S3
$imageName = ImageKit::setDisk('s3')
->image($request->file('image'))
->save();
// Save to Google Cloud Storage
$imageName = ImageKit::setDisk('gcs')
->image($request->file('image'))
->save();
// Method 1: Set disk globally
ImageKit::setDisk('s3')
->image($image)
->save();
// Method 2: Using config
// Set in config/imagekit.php: 'disk' => 's3'
// Method 3: Dynamic switching
ImageKit::setDisk('public')->image($image1)->save();
ImageKit::setDisk('s3')->image($image2)->save();
// Save to public disk
$imageName = ImageKit::setDisk('public')
->image($request->file('image'))
->save();
// Get URL (pg
// Upload watermark image from request
$imageName = ImageKit::image($request->file('image'))
->watermark($request->file('watermark'), 'bottom-right', 50)
->save();
// The watermark will be automatically saved to the watermark_storage_path configured in config
// Resize watermark to specific dimensions
$imageName = ImageKit::image($request->file('image'))
->watermark('watermark.png', 'bottom-right', 50, null, null, null, 100, 100)
->save();
// With UploadedFile and dimensions
$imageName = ImageKit::image($request->file('image'))
->watermark($request->file('watermark'), 'bottom-right', 50, null, null, null, 150, 150)
->save();
// Configure S3 in config/filesystems.php first
$imageName = ImageKit::setDisk('s3')
->image($request->file('image'))
->resize(1024, 1024)
->compress(90)
->save();
// Using UUID naming strategy
// First, set in config: 'naming_strategy' => 'uuid'
$imageName = ImageKit::image($request->file('image'))
->save();
// Or use custom name
$imageName = ImageKit::image($request->file('image'))
->name('product-123')
->save();
// Save some images to local storage
ImageKit::setDisk('public')
->images($localImages)
->saveGallery();
// Save others to S3
ImageKit::setDisk('s3')
->images($cloudImages)
->saveGallery();
// In your controller
public function showImage($path)
{
return ImageKit::response('uploads/images/' . $path);
}
// With cache (cache for 1 hour)
public function showImage($path)
{
return ImageKit::response('uploads/images/' . $path, null, [
'cache' => 3600
]);
}
// From S3
public function showImage($path)
{
return ImageKit::response('uploads/images/' . $path, 's3');
}
// Download image
public function downloadImage($path)
{
return ImageKit::download('uploads/images/' . $path, 'my-image.jpg');
}
// Get URL for displaying in view
$imageUrl = ImageKit::getImageUrl('uploads/images/test.jpg');
// Use in blade: <img src="{{ $imageUrl }}" />
// Get URL from S3
$imageUrl = ImageKit::getImageUrl('uploads/images/test.jpg', 's3');
// Get temporary URL that expires in 1 hour
$tempUrl = ImageKit::temporaryUrl('uploads/images/test.jpg', now()->addHour());
// Useful for private S3 files
$tempUrl = ImageKit::temporaryUrl('uploads/images/private.jpg', 3600, 's3');
$content = ImageKit::getImage('uploads/images/test.jpg');
// From specific disk
$content = ImageKit::getImage('uploads/images/test.jpg', 's3');
// Simple response
return ImageKit::response('uploads/images/test.jpg');
// With cache control
return ImageKit::response('uploads/images/test.jpg', null, [
'cache' => 3600 // Cache for 1 hour
]);
// No cache
return ImageKit::response('uploads/images/test.jpg', null, [
'cache' => false
]);
// From S3
return ImageKit::response('uploads/images/test.jpg', 's3');
return ImageKit::download('uploads/images/test.jpg', 'my-image.jpg');
// From specific disk
return ImageKit::download('uploads/images/test.jpg', 'my-image.jpg', 's3');